当前位置:网站首页>MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
2022-07-06 23:53:00 【Hua Weiyun】

One 、MySQL Introduction to master-slave replication
1.1、MySQL Introduction to master-slave replication
MySQL Master slave copy (MySQL Replication) Means from a MySQL master server (master) Copy data to another or more MySQL From the server (slaves) The process of . The main database's DDL and DML Operation through binary log (binlog) To the slave server (slave) On , Then re execute these logs on the slave server , So that the master and slave servers keep the data synchronized .
MySQL from 3.23 Version begins to provide the function of replication .
MySQL Of Replication It's a lot MySQL Database as master-slave synchronization scheme , It is widely used in all kinds of MySQL Higher performance 、 Higher reliability requirements .
1.2、 The benefits of master-slave replication
Master-slave replication has the following benefits :
- The data backup (Data Backup)
Simply back up the database , Reduce the risk of data loss , - Offline statistics
It is used in reports and other occasions that do not require high data timeliness . - Load balancing (Load Balance)、 Read / write separation
Mainly used in MySQL colony , Solve single point of failure or failover ; To reduce the load and risk of a single server , For example, realize the separation of reading and writing , It can make the server access load more balanced . - Data dissemination (Data DistributIOn)、 Disaster preparedness
It is mainly used for multi data center or remote backup , Realize data distribution and synchronization . - High availability and data fault tolerance (High Availability and Failover)
MySQL Self contained health monitoring and detection , According to the configured time interval , You can check whether the main library is working properly , Once the main database is found to be down or not working properly , You will choose the best standby database .
1.3、MySQL Master slave replication process

1.4、 Master slave Topology


Two 、3 Lord 1 Build from multi-source replication

2.1、MySQL Environment initialization
-- build 3 Lord 1 from -- Configure the network environment docker pull mysql:5.7.30docker network create --subnet=172.72.0.0/24 mysql-network-- Create parameter directory mkdir -p /lhrmysqltest2/master1/conf.dmkdir -p /lhrmysqltest2/master2/conf.dmkdir -p /lhrmysqltest2/master3/conf.dmkdir -p /lhrmysqltest2/slave/conf.d-- Delete the previous docker rm -f mysql5730M33265 mysql5730M33266 mysql5730M33267 mysql5730S33268-- Apply for master database 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-- Apply for master database 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-- Apply for master database 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-- Apply from the Library 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-- Configure the main library 1 Parameters of 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-- Configure the main library 2 Parameters of 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-- Configure the main library 3 Parameters of 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-- Configure the parameters of the slave Library 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-- Restart the host 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、 Main library configuration
-- stay 3 The master database respectively creates replication users 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、 Configuration from library
-- Make the following configuration from the Library mysql -uroot -plhr -h192.168.66.35 -P33268-- Configure the main library 1 Copy path to the slave Library 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';-- Configure the main library 2 Copy path to the slave Library 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';-- Configure the main library 3 Copy path to the slave Library 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';-- Start all SLAVEmysql> START SLAVE;-- You can also start the channel to be synchronized separately mysql> START SLAVE FOR CHANNEL 'master1';mysql> START SLAVE FOR CHANNEL 'master2';mysql> START SLAVE FOR CHANNEL 'master3';2.4、 Query multi-source replication
-- Query from the Library show slave status \G;-- If you want to view the detailed status of replication of a single channel , You can use the following command :mysql> SHOW SLAVE STATUS FOR CHANNEL 'master1'\G;-- Query through table 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;-- stay performance_schema In the library , Provides some views related to replication , You can view replication related information .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;-- Merge 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;-- Thread query 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、 Test multi-source replication
-- Test multiple sources -- 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);-- Query from the library -- mysql -uroot -plhr -h192.168.66.35 -P33268show databases;SELECT * FROM master1.test1;SELECT * FROM master2.test2;SELECT * FROM master3.test3;Query from the library :
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、 Points of attention
1、 The initial configuration takes a long time , You need to master The data of dump Come down , Again source To slave On .
2、 Each... Needs to be considered master Data growth frequency ,slave The data growth frequency of is the sum of these data . If it's too high , Will result in a large number of disks IO, Cause data update delay , The most serious problem is that it will affect the normal query .
3、 If a library with the same name exists in multiple primary database instances , Then the tables of the library with the same name will be put into one library ;
4、 If the table names in the library with the same name are the same and the structure is the same , Then the data will be merged ; If the structure is different , The first one is effective .
边栏推荐
- Penetration test --- database security: detailed explanation of SQL injection into database principle
- 数据运营平台-数据采集[通俗易懂]
- JS import excel & Export Excel
- (leetcode) sum of two numbers
- What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
- Knowledge * review
- openresty ngx_lua子请求
- 电脑重装系统u盘文件被隐藏要怎么找出来
- Talking about the current malpractice and future development
- 短链的设计
猜你喜欢

Design a red envelope grabbing system

The largest single investment in the history of Dachen was IPO today

Experiment 4: installing packages from Gui

2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
![[OFDM communication] OFDM system signal detection based on deep learning with matlab code](/img/a5/624860f6bd9be03ac8c1f61839fea2.png)
[OFDM communication] OFDM system signal detection based on deep learning with matlab code

The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance

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

Rider离线使用Nuget包的方法

STM32通过串口进入和唤醒停止模式

DAY FIVE
随机推荐
AVL树到底是什么?
谷歌百度雅虎都是中国公司开发的通用搜索引擎_百度搜索引擎url
在docker中快速使用各个版本的PostgreSQL数据库
app通用功能測試用例
Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
MySQL implementation of field segmentation from one line to multiple lines of example code
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
[212] what are three methods for PHP to send post requests
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
Interface joint debugging test script optimization v4.0
If the request URL contains jsessionid, the solution
Knowledge * review
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
openresty ngx_ Lua subrequest
Rider离线使用Nuget包的方法
数据运营平台-数据采集[通俗易懂]
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
使用yum来安装PostgreSQL13.3数据库