0. 概述
自MySQL5.7.6 版本开始,就支持多源复制。本文简要记录下两主一从的多源复制部署过程,及其中的要点。
1. 分别在两个主库中模拟需要同步的数据:
主库1:在mm数据库中创建测试表mm,并插入几条数据。
root@localhost [(none)]>create database mm;
root@localhost [(none)]>use mm;
root@localhost [mm]>create table mm(id int primary key);
root@localhost [mm]>insert into mm values(1);
root@localhost [mm]>insert into mm values(2);
root@localhost [mm]>insert into mm values(3);
root@localhost [mm]>insert into mm values(4);
root@localhost [mm]>insert into mm values(5);
主库2:在mm数据库中创建测试表nn,并插入几条数据。
root@localhost [(none)]>create database mm;
root@localhost [(none)]>use mm;
root@localhost [mm]>create table nn(id int primary key);
root@localhost [mm]>insert into nn values(1);
2. 分别两个主库上创建复制用户:
create user repuser@'192.168.56.%' identified by 'welcome1';
grant replication slave, replication client on *.* to repuser@'192.168.56.%';
3. 在备库,针对主库1创建第一个主从复制通道:
主库1导出数据。
[root@mysqldb01 ~]# mysqldump -uroot -pwelcome1 --single-transaction --master-data=2 --triggers --routines --events mm > source1.sql
从库导入数据。
[root@mysqldb03 ~]# mysql -uroot -pwelcome1 -e "create database mm"
[root@mysqldb03 ~]# mysql -uroot -pwelcome1 -e "reset master" ##这个操作可以将从库的GTID_EXECUTED值置空,否则,在执行导入脚本中的SET @@GLOBAL.GTID_PURGED语句会报错。
[root@mysqldb03 ~]# mysql -uroot -pwelcome1 -Dmm < /root/source1.sql
从库创建针对主库1的复制通道。
root@localhost [(none)]>change master to master_host='192.168.56.131', master_port=3306, master_auto_position = 1, master_user='repuser', master_password='welcome1' for channel 'channel-131';
root@localhost [(none)]>start slave for channel 'channel-131';
4. 在备库,针对主库2创建第二个主从复制通道:
主库2导出数据。
[root@mysqldb02 ~]# mysqldump -uroot -pwelcome1 --single-transaction --master-data=2 --triggers --routines --events mm > source2.sql
从库导入数据。
[root@mysqldb03 ~]# mysql -uroot -pwelcome1 -Dmm < /root/source2.sql
此时,导入过程会报错。"ERROR 1840 (HY000) at line 61: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty."这个报错可以忽略,实际上数据已经导入成功。我们也可以修改source2.sql文件,将SET @@GLOBAL.GTID_PURGED=语句注释掉。
从库创建针对主库2的复制通道。
root@localhost [(none)]>change master to master_host='192.168.56.132', master_port=3306, master_auto_position = 1, master_user='repuser', master_password='welcome1' for channel 'channel-132';
5. 针对第二个主从复制通道,设置GTID_EXECUTED:
查看主节点2一致性备份导出时的GTID_PURGED。
[root@mysqldb03 ~]# grep -i gtid source2.sql
-- GTID state at the end of the backup
SET @@GLOBAL.GTID_PURGED='e70ccc56-a744-11f0-8b7e-0800279f252e:1-6';
设置针对第二个主从复制通道的GTID_EXECUTED(方法一)
stop slave;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:1';
BEGIN;
COMMIT;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:2';
BEGIN;
COMMIT;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:3';
BEGIN;
COMMIT;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:4';
BEGIN;
COMMIT;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:5';
BEGIN;
COMMIT;
SET GTID_NEXT='e70ccc56-a744-11f0-8b7e-0800279f252e:6';
BEGIN;
COMMIT;
SET GTID_NEXT='AUTOMATIC';
设置针对第二个主从复制通道的GTID_EXECUTED(方法二)
stop slave;
reset master;
SET @@GLOBAL.GTID_PURGED='e6800c46-a744-11f0-b24e-080027fec62c:1-10,e70ccc56-a744-11f0-8b7e-0800279f252e:1-6';
6. 分别在两个主节点模拟数据变化:
主节点1
root@localhost [mm]>insert into mm values(6);
root@localhost [mm]>insert into mm values(7);
root@localhost [mm]>insert into mm values(8);
root@localhost [mm]>
主节点2
root@localhost [(none)]>insert into mm.nn values(2);
root@localhost [(none)]>insert into mm.nn values(3);
root@localhost [(none)]>insert into mm.nn values(4);
root@localhost [(none)]>insert into mm.nn values(5);
root@localhost [(none)]>
7. 在从节点查看数据是否复制至从节点:
root@localhost [mm]>start slave;
root@localhost [(none)]>show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.131
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 2998
Relay_Log_File: relay-bin-channel@002d131.000003
Relay_Log_Pos: 1085
Relay_Master_Log_File: mysql-bin.000002
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: 2998
Relay_Log_Space: 1542
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: 131
Master_UUID: e6800c46-a744-11f0-b24e-080027fec62c
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: e6800c46-a744-11f0-b24e-080027fec62c:11-13
Executed_Gtid_Set: e6800c46-a744-11f0-b24e-080027fec62c:1-13,
e70ccc56-a744-11f0-8b7e-0800279f252e:1-10
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: channel-131
Master_TLS_Version:
*************************** 2. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.132
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2207
Relay_Log_File: relay-bin-channel@002d132.000002
Relay_Log_Pos: 1314
Relay_Master_Log_File: mysql-bin.000001
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: 2207
Relay_Log_Space: 1523
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: 132
Master_UUID: e70ccc56-a744-11f0-8b7e-0800279f252e
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: e70ccc56-a744-11f0-8b7e-0800279f252e:7-10
Executed_Gtid_Set: e6800c46-a744-11f0-b24e-080027fec62c:1-13,
e70ccc56-a744-11f0-8b7e-0800279f252e:1-10
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: channel-132
Master_TLS_Version:
root@localhost [(none)]>
root@localhost [mm]>select * from mm.nn;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+
root@localhost [mm]>select * from mm.mm;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+----+
可以看出,一个简单的两主一从的多源主从复制配置完毕。
总结 :
整个部署过程的关键点在于如果在从节点设置GTID_PURGED。