MySQL多源复制使复制从服务器可以同时接收来自多个来源的事务。多源复制可用于将多台服务器备份到单台服务器,合并表分片并将多台服务器的数据合并到单台服务器。多源复制在应用事务时不会执行任何冲突检测或解决方案,并且如果需要,这些任务将留给应用程序。在多源复制拓扑中,从服务器为每个主服务器创建一个复制通道,以便从中接收事务。
maseter1:192.168.1.32:3306 redhat6.7
maseter2:192.168.1.33:3306 redhat6.7
slave1:192.168.1.32:3406 redhat6.7
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
Master1 参数文件修改
server_id = 32
log-bin = master1-bin
gtid-mode=on
enforce-gtid-consistency=true
Master2 参数文件修改
server_id = 33
log-bin = master2-bin
gtid-mode=on
enforce-gtid-consistency=true
Slave 参数文件修改
server_id = 34
relay-log = relay-bin
master_info_repository=TABLE
relay_log_info_repository=TABLE
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
master1 创建数据库
mysql> create database test32;
Query OK, 1 row affected (0.10 sec)
mysql> use test32
Database changed
mysql> create table t1(id int(5));
Query OK, 0 rows affected (0.11 sec)
mysql> insert into t1 values(1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into t1 values(2);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
Master2 创建数据库
mysql> create database test33;
Query OK, 1 row affected (0.01 sec)
mysql> use test33
Database changed
mysql> create table t2(id int(5));
Query OK, 0 rows affected (0.10 sec)
mysql> insert into t2 values(1);
Query OK, 1 row affected (0.06 sec)
mysql> insert into t2 values(2);
Query OK, 1 row affected (0.05 sec)
mysql> select * from t2;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
分别在master1和master2上导出需要同步的数据库
在 master1 上备份要同步的数据库
mysqldump -uroot -p --master-data=2 --single-transaction --add-drop-database --set-gtid-purged=OFF --databases test32 >/backup/test32.sql
在master2 上备份要同步的数据库
mysqldump -uroot -p --master-data=2 --single-transaction --add-drop-database --set-gtid-purged=OFF --databases test33 >/backup/test33.sql
分别把备份scp到slave上
[[email protected] backup]# scp /backup/test32.sql 192.168.1.34:/backup/
[[email protected] ~]# scp /backup/test33.sql 192.168.1.34:/backup/
这个操作跟MySQL 5.7之前版本一样:
在master1
mysql> grant replication slave on *.* to 'rep1'@'192.168.1.34' identified by 'rep1';
Query OK, 0 rows affected, 1 warning (0.30 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在master2
mysql> grant replication slave on *.* to 'rep2'@'192.168.1.34' identified by 'rep2';
Query OK, 0 rows affected, 1 warning (0.30 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
分别Slave上把master1和master2的数据导入Slave服务器
在导入前先确认是否修改MySQL存储master-info和relay-info的方式即从文件存储改为表存储,在my.cnf里添加以下选择:
master_info_repository=TABLE
relay_log_info_repository=TABLE
也可以在线修改,灰常方便:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SET GLOBAL master_info_repository = 'TABLE';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL relay_log_info_repository = 'TABLE';
Query OK, 0 rows affected (0.00 sec)
在slave 上恢复两个主库要同步的数据库
[[email protected] ~]# mysql -uroot -p < /backup/test32.sql
Enter password:
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty
从库执行
mysql> reset master;
Query OK, 0 rows affected (0.04 sec)
[[email protected] backup]# mysql -uroot -p < /backup/test32.sql
[[email protected] backup]# mysql -uroot -p < /backup/test33.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test32 |
| test33 |
+--------------------+
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.32', MASTER_USER='rep1', MASTER_PORT=3306, MASTER_PASSWORD='rep1', MASTER_AUTO_POSITION = 1 FOR CHANNEL 'Master_1';
Query OK, 0 rows affected, 2 warnings (0.06 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.33', MASTER_USER='rep2', MASTER_PORT=3306, MASTER_PASSWORD='rep2', MASTER_AUTO_POSITION = 1 FOR CHANNEL 'Master_2';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
默认开启所有的复制通道
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
也可以指定复制通道开启
mysql> start slave for CHANNEL 'Master_1';
Query OK, 0 rows affected (0.01 sec)
mysql> start slave for CHANNEL 'Master_2';
Query OK, 0 rows affected (0.04 sec)
注意:结果中Slave_IO_Running和Slave_SQL_Running必须为Yes,如果不是,需要根据提示的错误修改。
show slave status/G;
或者单独查看每个通道的复制状态
mysql> SHOW SLAVE STATUS FOR CHANNEL 'Master_1'/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.32
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master1-bin.000012
Read_Master_Log_Pos: 640
Relay_Log_File: relay-bin-master_1.000002
Relay_Log_Pos: 420
Relay_Master_Log_File: master1-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 640
Relay_Log_Space: 670
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 32
Master_UUID: b6799e3a-4220-11e8-a595-08002703bb35
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 921a9952-4220-11e8-89a0-080027686323:1-5,
b6799e3a-4220-11e8-a595-08002703bb35:1-6
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: master_1
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> SHOW SLAVE STATUS FOR CHANNEL 'Master_2'/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.33
Master_User: rep2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master2-bin.000012
Read_Master_Log_Pos: 640
Relay_Log_File: relay-bin-master_2.000002
Relay_Log_Pos: 420
Relay_Master_Log_File: master2-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 640
Relay_Log_Space: 670
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 33
Master_UUID: 921a9952-4220-11e8-89a0-080027686323
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 921a9952-4220-11e8-89a0-080027686323:1-5,
b6799e3a-4220-11e8-a595-08002703bb35:1-6
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: master_2
Master_TLS_Version:
1 row in set (0.00 sec)
--master1 插入数据
mysql> use test32
Database changed
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> insert into t1 values(4);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
--master2 插入数据
mysql> use test33
Database changed
mysql> select * from t2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> insert into t2 values(4);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
slave 查看数据被同步过来
mysql> select * from test32.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> select * from test33.t2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)