当前位置:网站首页>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、如果同名库中的表名相同且结构相同,则数据会合并到一起;如果结构不同,则先建的有效。
边栏推荐
- 基础图表解读“东方甄选”爆火出圈数据
- Implementation steps of mysql start log in docker
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- MySQL connected vscode successfully, but this error is reported
- B 站弹幕 protobuf 协议还原分析
- Design a red envelope grabbing system
- Please help xampp to do sqlilab is a black
- pinia 模块划分
- AVL树到底是什么?
- The same job has two sources, and the same link has different database accounts. Why is the database list found in the second link the first account
猜你喜欢

js对JSON数组的增删改查

koa2对Json数组增删改查

The problem of ASP reading Oracle Database

快手的新生意,还得靠辛巴吆喝?

Gradle知识概括

Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming

Experiment 4: installing packages from Gui

11 preparations for Web3 and Decentralization for traditional enterprises

基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】

Koa2 addition, deletion, modification and query of JSON array
随机推荐
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
Without CD, I'll teach you a trick to restore the factory settings of win10 system
ArrayExpress数据库里的细胞只有两个txt是不是只能根据Line到ENA下载测序跑矩阵?
PDF文档签名指南
How to find out if the U disk file of the computer reinstallation system is hidden
Close unregistering application XXX with Eureka with status down after Eureka client starts
士大夫哈哈哈
传统企业要为 Web3 和去中心化做的 11 个准备
MVC and MVVM
[212] what are three methods for PHP to send post requests
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
快手的新生意,还得靠辛巴吆喝?
Daily question brushing record (XV)
Experiment 5: common automation libraries
Summary of three methods for MySQL to view table structure
三句话简要介绍子网掩码
浅谈现在的弊端与未来的发展
DAY FOUR
基于SSM框架实现的房屋租赁管理系统
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)