当前位置:网站首页>MySQL主从之多源复制(3主1从)搭建及同步测试
MySQL主从之多源复制(3主1从)搭建及同步测试
2022-07-06 16:23:00 【华为云】
一、MySQL主从复制简介
1.1、MySQL主从复制简介
MySQL主从复制(MySQL Replication)是指从一个MySQL主服务器(master)将数据拷贝到另一台或多台MySQL从服务器(slaves)的过程。将主数据库的DDL和DML操作通过二进制日志(binlog)传到从服务器(slave)上,然后在从服务器上对这些日志重新执行,从而使得主从服务器的数据保持同步。
MySQL从3.23版本开始提供复制的功能。
MySQL的Replication是一个多MySQL数据库做主从同步的方案,广泛用在各种对MySQL有更高性能、更高可靠性要求的场合。
1.2、主从复制的好处
主从复制有以下几方面的好处:
- 数据备份(Data Backup)
只是简单的对数据库进行备份,降低数据丢失的风险, - 线下统计
用于报表等对数据时效性要求不高的场合。 - 负载均衡(Load Balance)、读写分离
主要用在MySQL集群,解决单点故障或做故障切换;以降低单台服务器的负载和风险,如实现读写分离,可以使得服务器访问负荷比较均衡。 - 数据分发(Data DistributIOn)、灾备
主要用于多数据中心或异地备份,实现数据分发与同步。 - 高可用和数据容错(High Availability and Failover)
MySQL自带的健康监控和检测,根据配置的时间间隔,可以检测主库是否正常工作,一旦发现主库宕机或无法正常工作,就会选择到最好的一个备库上。
1.3、MySQL主从复制流程
1.4、主从拓扑结构
二、3主1从多源复制搭建
2.1、MySQL环境初始化
-- 搭建3主1从-- 配置网络环境docker pull mysql:5.7.30docker network create --subnet=172.72.0.0/24 mysql-network-- 创建参数目录mkdir -p /lhrmysqltest2/master1/conf.dmkdir -p /lhrmysqltest2/master2/conf.dmkdir -p /lhrmysqltest2/master3/conf.dmkdir -p /lhrmysqltest2/slave/conf.d-- 删除之前的docker rm -f mysql5730M33265 mysql5730M33266 mysql5730M33267 mysql5730S33268-- 申请主库1docker run -d --name mysql5730M33265 \ -h master1 -p 33265:3306 --net=mysql-network --ip 172.72.0.10 \ -v /lhrmysqltest2/master1/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30-- 申请主库2docker run -d --name mysql5730M33266 \ -h master2 -p 33266:3306 --net=mysql-network --ip 172.72.0.11 \ -v /lhrmysqltest2/master2/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30-- 申请主库3docker run -d --name mysql5730M33267 \ -h master3 -p 33267:3306 --net=mysql-network --ip 172.72.0.12 \ -v /lhrmysqltest2/master3/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30-- 申请从库docker run -d --name mysql5730S33268 \ -h slave1 -p 33268:3306 --net=mysql-network --ip 172.72.0.13 \ -v /lhrmysqltest2/slave/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30-- 配置主库1的参数cat > /lhrmysqltest2/master1/conf.d/my.cnf << "EOF"[mysqld]port=3306character_set_server=utf8mb4secure_file_priv=server-id = 573033265log-bin =binlog_format=rowexpire_logs_days = 30max_binlog_size = 100Mbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemabinlog-ignore-db = performance_schemabinlog-ignore-db = sysreplicate_ignore_db=information_schemareplicate_ignore_db=performance_schemareplicate_ignore_db=mysqlreplicate_ignore_db=sysgtid-mode=ONenforce-gtid-consistency=onskip_name_resolvereport_host=172.72.0.10EOF-- 配置主库2的参数cat > /lhrmysqltest2/master2/conf.d/my.cnf << "EOF"[mysqld]port=3306character_set_server=utf8mb4secure_file_priv=server-id = 573033266log-bin = binlog_format=rowexpire_logs_days = 30max_binlog_size = 100Mbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemabinlog-ignore-db = performance_schemabinlog-ignore-db = sysreplicate_ignore_db=information_schemareplicate_ignore_db=performance_schemareplicate_ignore_db=mysqlreplicate_ignore_db=sysgtid-mode=ONenforce-gtid-consistency=ONskip_name_resolvereport_host=172.72.0.11EOF-- 配置主库3的参数cat > /lhrmysqltest2/master3/conf.d/my.cnf << "EOF"[mysqld]port=3306character_set_server=utf8mb4secure_file_priv=server-id = 573033267log-bin = binlog_format=rowexpire_logs_days = 30max_binlog_size = 100Mbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemabinlog-ignore-db = performance_schemabinlog-ignore-db = sysreplicate_ignore_db=information_schemareplicate_ignore_db=performance_schemareplicate_ignore_db=mysqlreplicate_ignore_db=sysgtid-mode=ONenforce-gtid-consistency=ONskip_name_resolvereport_host=172.72.0.12EOF-- 配置从库的参数cat > /lhrmysqltest2/slave/conf.d/my.cnf << "EOF"[mysqld]port=3306character_set_server=utf8mb4secure_file_priv=server-id = 573033268log-bin = binlog_format=rowexpire_logs_days = 30max_binlog_size = 100Mbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemabinlog-ignore-db = performance_schemabinlog-ignore-db = sysreplicate_ignore_db=information_schemareplicate_ignore_db=performance_schemareplicate_ignore_db=mysqlreplicate_ignore_db=sysgtid-mode=ONenforce-gtid-consistency=ONskip_name_resolvereport_host=172.72.0.13master-info-repository = tablerelay-log-info-repository = tableEOF-- 重启主机docker restart mysql5730M33265docker restart mysql5730M33266docker restart mysql5730M33267docker restart mysql5730S33268docker psdocker exec -it mysql5730M33265 bashdocker exec -it mysql5730M33265 mysql -uroot -plhrmysql -uroot -plhr -h192.168.66.35 -P33265 -e "select @@hostname,@@server_id,@@server_uuid"mysql -uroot -plhr -h192.168.66.35 -P33266 -e "select @@hostname,@@server_id,@@server_uuid"mysql -uroot -plhr -h192.168.66.35 -P33267 -e "select @@hostname,@@server_id,@@server_uuid"mysql -uroot -plhr -h192.168.66.35 -P33268 -e "select @@hostname,@@server_id,@@server_uuid"
2.2、主库配置
-- 在3台主库分别创建复制用户mysql -uroot -plhr -h192.168.66.35 -P33265mysql -uroot -plhr -h192.168.66.35 -P33266mysql -uroot -plhr -h192.168.66.35 -P33267grant replication slave on *.* to repl@'%' identified by 'lhr';select user,host,grant_priv,password_last_changed,authentication_string from mysql.user; show master status \G;show slave hosts;select @@hostname,@@server_id,@@server_uuid;
2.3、从库配置
-- 从库做以下配置mysql -uroot -plhr -h192.168.66.35 -P33268-- 配置主库1到从库的复制路径change master to master_host='172.72.0.10',master_port=3306,master_user='repl',master_password='lhr',master_auto_position=1 FOR CHANNEL 'Master1';-- 配置主库2到从库的复制路径change master to master_host='172.72.0.11',master_port=3306,master_user='repl',master_password='lhr',master_auto_position=1 FOR CHANNEL 'Master2';-- 配置主库3到从库的复制路径change master to master_host='172.72.0.12',master_port=3306,master_user='repl',master_password='lhr',master_auto_position=1 FOR CHANNEL 'Master3';-- 启动所有SLAVEmysql> START SLAVE;-- 也可以单独启动需要同步的通道mysql> START SLAVE FOR CHANNEL 'master1';mysql> START SLAVE FOR CHANNEL 'master2';mysql> START SLAVE FOR CHANNEL 'master3';
2.4、查询多源复制
-- 在从库查询show slave status \G;-- 如果要查看单一信道的复制的详细状态,可以使用以下命令:mysql> SHOW SLAVE STATUS FOR CHANNEL 'master1'\G;-- 通过表查询select a.master_log_pos,a.host,a.user_name,a.user_password,a.port,a.uuid,a.channel_name from mysql.slave_master_info a;select * from mysql.slave_relay_log_info;select * from mysql.slave_worker_info;select * from mysql.gtid_executed;-- 在 performance_schema 库中,提供了复制相关的一些视图,可供查看复制相关的信息。select * from performance_schema.replication_applier_configuration;select * from performance_schema.replication_applier_status;select * from performance_schema.replication_applier_status_by_coordinator;select * from performance_schema.replication_applier_status_by_worker;select * from performance_schema.replication_connection_configuration;select * from performance_schema.replication_connection_status;select * from performance_schema.replication_group_member_stats;select * from performance_schema.replication_group_members;-- 合并SQLselect rcc.CHANNEL_NAME,rcc.`HOST`,rcc.`PORT`,rcc.`USER`,rcc.CONNECTION_RETRY_COUNT,rcc.CONNECTION_RETRY_INTERVAL,rcs.SOURCE_UUID,rcs.THREAD_ID,rcs.SERVICE_STATE,rcs.COUNT_RECEIVED_HEARTBEATS,rcs.LAST_HEARTBEAT_TIMESTAMP,rcs.LAST_ERROR_NUMBER,rcs.LAST_ERROR_MESSAGE,rcs.LAST_ERROR_TIMESTAMPfrom performance_schema.replication_connection_configuration rcc, performance_schema.replication_connection_status rcswhere rcc.CHANNEL_NAME=rcs.CHANNEL_NAME;-- 线程查询SELECT *FROM performance_schema.threads aWHERE a.`NAME` IN ( 'thread/sql/slave_IO', 'thread/sql/slave_sql' ) or a.PROCESSLIST_COMMAND in ('Binlog Dump','Binlog Dump GTID') ;SELECT * FROM information_schema.`PROCESSLIST` a where a.USER='system user' or a.command in ('Binlog Dump','Binlog Dump GTID') ;MySQL [(none)]> SELECT * -> FROM performance_schema.threads a -> WHERE a.`NAME` IN ( 'thread/sql/slave_IO', 'thread/sql/slave_sql' ) or a.PROCESSLIST_COMMAND in ('Binlog Dump','Binlog Dump GTID') ;+-----------+----------------------+------------+----------------+------------------+------------------+----------------+---------------------+------------------+--------------------------------------------------------+------------------+------------------+------+--------------+---------+-----------------+--------------+| THREAD_ID | NAME | TYPE | PROCESSLIST_ID | PROCESSLIST_USER | PROCESSLIST_HOST | PROCESSLIST_DB | PROCESSLIST_COMMAND | PROCESSLIST_TIME | PROCESSLIST_STATE | PROCESSLIST_INFO | PARENT_THREAD_ID | ROLE | INSTRUMENTED | HISTORY | CONNECTION_TYPE | THREAD_OS_ID |+-----------+----------------------+------------+----------------+------------------+------------------+----------------+---------------------+------------------+--------------------------------------------------------+------------------+------------------+------+--------------+---------+-----------------+--------------+| 29 | thread/sql/slave_io | FOREGROUND | 4 | root | 172.72.0.1 | NULL | Connect | 252 | Waiting for master to send event | NULL | 28 | NULL | YES | YES | NULL | 75 || 30 | thread/sql/slave_sql | FOREGROUND | 5 | root | 172.72.0.1 | NULL | Connect | 325 | Slave has read all relay log; waiting for more updates | NULL | 28 | NULL | YES | YES | NULL | 76 || 31 | thread/sql/slave_io | FOREGROUND | 6 | root | 172.72.0.1 | NULL | Connect | 252 | Waiting for master to send event | NULL | 28 | NULL | YES | YES | NULL | 77 || 32 | thread/sql/slave_sql | FOREGROUND | 7 | root | 172.72.0.1 | NULL | Connect | 322 | Slave has read all relay log; waiting for more updates | NULL | 28 | NULL | YES | YES | NULL | 78 || 33 | thread/sql/slave_io | FOREGROUND | 8 | root | 172.72.0.1 | NULL | Connect | 252 | Waiting for master to send event | NULL | 28 | NULL | YES | YES | NULL | 79 || 34 | thread/sql/slave_sql | FOREGROUND | 9 | root | 172.72.0.1 | NULL | Connect | 320 | Slave has read all relay log; waiting for more updates | NULL | 28 | NULL | YES | YES | NULL | 80 |+-----------+----------------------+------------+----------------+------------------+------------------+----------------+---------------------+------------------+--------------------------------------------------------+------------------+------------------+------+--------------+---------+-----------------+--------------+6 rows in set (0.09 sec)
2.5、测试多源复制
-- 测试多源-- mysql -uroot -plhr -h192.168.66.35 -P33265create database master1;use master1;CREATE TABLE `test1` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);insert into test1 values(1,1);-- mysql -uroot -plhr -h192.168.66.35 -P33266create database master2;use master2;CREATE TABLE `test2` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);insert into test2 values(2,2);-- mysql -uroot -plhr -h192.168.66.35 -P33267create database master3;use master3;CREATE TABLE `test3` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);insert into test3 values(3,3);-- 从库查询-- mysql -uroot -plhr -h192.168.66.35 -P33268show databases;SELECT * FROM master1.test1;SELECT * FROM master2.test2;SELECT * FROM master3.test3;
从库查询:
MySQL [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || master1 || master2 || master3 || mysql || performance_schema || sys |+--------------------+7 rows in set (0.05 sec)MySQL [(none)]> SELECT * FROM master1.test1;+------+-------+| id | count |+------+-------+| 1 | 1 |+------+-------+1 row in set (0.06 sec)MySQL [(none)]> SELECT * FROM master2.test2;+------+-------+| id | count |+------+-------+| 2 | 2 |+------+-------+1 row in set (0.05 sec)MySQL [(none)]> SELECT * FROM master3.test3;+------+-------+| id | count |+------+-------+| 3 | 3 |+------+-------+1 row in set (0.05 sec)MySQL [(none)]>
2.6、注意的点
1、初次配置耗时较长,需要将各个master的数据dump下来,再source到slave上。
2、需要考虑各master数据增长频率,slave的数据增长频率是这些数据的总和。如果太高,会导致大量的磁盘IO,造成数据更新延迟,最严重的是会影响正常的查询。
3、如果多个主数据库实例中存在同名的库,则同名库的表都会放到一个库中;
4、如果同名库中的表名相同且结构相同,则数据会合并到一起;如果结构不同,则先建的有效。
边栏推荐
- The method of reinstalling win10 system is as simple as that
- 【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- okcc呼叫中心的订单管理时怎么样的
- 快讯 l Huobi Ventures与Genesis公链深入接洽中
- Knowledge * review
- js对JSON数组的增删改查
- The problem of ASP reading Oracle Database
- Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
- koa2对Json数组增删改查
猜你喜欢
Gradle知識概括
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
MySQL implementation of field segmentation from one line to multiple lines of example code
若依请求url中带有jsessionid的解决办法
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
Yaduo Sangu IPO
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
Daily question brushing record (XV)
STM32通过串口进入和唤醒停止模式
Stop saying that microservices can solve all problems
随机推荐
Laravel8 uses passport authentication to log in and generate a token
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
Wind chime card issuing network source code latest version - commercially available
Compile logisim
[system analyst's road] Chapter 7 double disk system design (service-oriented development method)
Daily question brushing record (XV)
pinia 模块划分
The problem of ASP reading Oracle Database
基础图表解读“东方甄选”爆火出圈数据
A novice asks a question. I am now deployed on a single machine. I submitted an SQL job and it runs normally. If I restart the service job, it will disappear and I will have to
[212] what are three methods for PHP to send post requests
Yaduo Sangu IPO
2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
How to implement Lua entry of API gateway
【系统分析师之路】第七章 复盘系统设计(面向服务开发方法)
Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
The method of reinstalling win10 system is as simple as that
Talking about the current malpractice and future development