MySQL多源复制
MySQL多源复制,它使您能够从多个直接主服务器并行复制。介绍多源复制的配置、监控和故障排除方法。
MySQL多源复制概述
MySQL多源复制允许复制从服务器同时接收来自多个源的事务。多源复制可以将多台服务器备份到一台服务器,可以合并表分片,也可以将多台服务器的数据合并到一台服务器。在应用事务时,多源复制不实现任何冲突的检测或解决,如果需要,这些任务将留给应用程序。在多源复制拓扑中,从节点为应该从其接收事务的每个主节点创建复制通道。
配置多源复制
介绍多源复制拓扑的配置方法,以及配置主从拓扑的详细信息。这样的拓扑至少需要配置两个主节点和一个从节点。
可以将多源复制拓扑中的主机配置为使用基于全局事务标识符(GTID)的复制或基于二进制日志位置的复制。
多源复制拓扑中的从服务器需要基于TABLE的存储库。多源复制与基于文件的存储库不兼容。mysqld使用的存储库类型可以在启动时配置,也可以动态配置。
多源复制
主库1:10.18.30.25
主库2:10.18.30.43
从库: 10.18.30.39
1.在主库中创建用于复制的用户
主库1:10.18.30.25
[root@localhost mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'slavepass'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) 主库2:10.18.30.43 [root@localhost mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'slavepass'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)
2.对主库进行复制相关参数的设置
2.1 配置二进制日志和服务器ID选项
log-bin=mysql-bin server-id=1
2.2 要配置复制从服务器启动时使用的存储库类型,请使用以下选项启动mysqld:
master-info-repository=TABLE relay-log-info-repository=TABLE
要修改一个使用FILE存储库的复制从库,使其使用TABLE存储库,需要执行以下命令动态转换现有的复制存储库:
STOP SLAVE; SET GLOBAL master_info_repository = 'TABLE'; SET GLOBAL relay_log_info_repository = 'TABLE'; mysql> stop slave; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 5 Current database: undo Query OK, 0 rows affected (0.10 sec) mysql> SET GLOBAL master_info_repository = 'TABLE'; Query OK, 0 rows affected (0.01 sec) mysql> SET GLOBAL relay_log_info_repository = 'TABLE'; Query OK, 0 rows affected (0.00 sec)
2.3 如果使用基于全局事务标识符(GTID)的复制,还要检查全局事务标识符(GTID)的设置
mysql> show variables like '%server%id%'; +----------------+--------------------------------------+ | Variable_name | Value | +----------------+--------------------------------------+ | server_id | 1 | | server_id_bits | 32 | | server_uuid | f044fd89-6b6c-11ef-9f9f-005056a390e6 | +----------------+--------------------------------------+ 3 rows in set (0.01 sec) mysql> show variables like '%server%id%'; +----------------+--------------------------------------+ | Variable_name | Value | +----------------+--------------------------------------+ | server_id | 2 | | server_id_bits | 32 | | server_uuid | 3f706e9b-6b6b-11ef-a6e3-005056a3a162 | +----------------+--------------------------------------+ 3 rows in set (0.01 sec) mysql> show variables like '%server%id%'; +----------------+--------------------------------------+ | Variable_name | Value | +----------------+--------------------------------------+ | server_id | 3 | | server_id_bits | 32 | | server_uuid | b064fda1-6b68-11ef-8226-005056b9a980 | +----------------+--------------------------------------+ 3 rows in set (0.01 sec)
3.向多源复制从端添加基于GTID的主端
假设您已经使用gtid_mode= on在主服务器上启用了基于GTID的事务,启用了复制用户,并确保从服务器使用基于TABLE的复制存储库。使用CHANGE MASTER TO语句通过使用FOR channel通道子句向通道添加一个新的主通道。
mysql> CHANGE MASTER TO MASTER_HOST='10.18.30.25',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-1'; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> CHANGE MASTER TO MASTER_HOST='10.18.30.43',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-2'; Query OK, 0 rows affected, 2 warnings (0.01 sec)
启动多源复制从服务器
添加了想要用作复制主机的所有通道后,使用START SLAVE thread_types语句启动复制。当您在从属服务器上启用了多个通道时,您可以选择启动所有通道,或者选择要启动的特定通道。
.启动所有当前配置的复制区域通道
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
从库日志信息显示如下:
2024-09-09T09:16:09.641054Z 11 [Note] Slave I/O thread for channel 'master-1': connected to master 'repl@10.18.30.25:3306',replication started in log 'binlog.000002' at position 1841 2024-09-09T09:16:09.645067Z 13 [Note] Slave I/O thread for channel 'master-2': connected to master 'repl@10.18.30.43:3306',replication started in log 'binlog.000002' at position 1841
主库日志信息显示如下:
主库1:10.18.30.25
2024-09-10T08:40:48.526865Z 4 [Warning] IP address '10.18.30.39' could not be resolved: Temporary failure in name resolution 2024-09-10T08:40:48.534271Z 4 [Note] Start binlog_dump to master_thread_id(4) slave_server(3), pos(binlog.000004, 194)
主库2:10.18.30.43
2024-09-10T08:33:01.936331Z 7 [Warning] IP address '10.18.30.39' could not be resolved: Temporary failure in name resolution 2024-09-10T08:33:01.943408Z 7 [Note] Start binlog_dump to master_thread_id(7) slave_server(3), pos(binlog.000003, 194)
查看从库日志发现出错了。
2024-09-10T07:55:34.529867Z 4 [ERROR] Slave SQL for channel 'master-1': Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'', Error_code: 1396 2024-09-10T07:55:34.529945Z 4 [Warning] Slave: Operation ALTER USER failed for 'root'@'localhost' Error_code: 1396 2024-09-10T07:55:34.529980Z 4 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'binlog.000002' position 154. 2024-09-10T07:55:34.530177Z 6 [ERROR] Slave SQL for channel 'master-2': Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'', Error_code: 1396 2024-09-10T07:55:34.530231Z 6 [Warning] Slave: Operation ALTER USER failed for 'root'@'localhost' Error_code: 1396 2024-09-10T07:55:34.530249Z 6 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'binlog.000002' position 154. 2024-09-10T07:55:34.529867Z 4 [ERROR] Slave SQL for channel 'master-1': Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'', Error_code: 1396 2024-09-10T07:55:34.529945Z 4 [Warning] Slave: Operation ALTER USER failed for 'root'@'localhost' Error_code: 1396 2024-09-10T07:55:34.529980Z 4 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'binlog.000002' position 154. 2024-09-10T07:55:34.530177Z 6 [ERROR] Slave SQL for channel 'master-2': Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'', Error_code: 1396 2024-09-10T07:55:34.530231Z 6 [Warning] Slave: Operation ALTER USER failed for 'root'@'localhost' Error_code: 1396 2024-09-10T07:55:34.530249Z 6 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'binlog.000002' position 154. mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.18.30.25 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 194 Relay_Log_File: localhost-relay-bin-master@002d1.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: binlog.000002 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1396 Last_Error: Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Skip_Counter: 0 Exec_Master_Log_Pos: 154 Relay_Log_Space: 3214 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1396 Last_SQL_Error: Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: bb8b95d1-6f47-11ef-9592-005056a390e6 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 240910 15:55:34 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: bb8b95d1-6f47-11ef-9592-005056a390e6:1-8 Executed_Gtid_Set: ca006ef3-6f46-11ef-8203-005056b9a980:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master-1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.18.30.43 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000003 Read_Master_Log_Pos: 194 Relay_Log_File: localhost-relay-bin-master@002d2.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: binlog.000002 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1396 Last_Error: Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Skip_Counter: 0 Exec_Master_Log_Pos: 154 Relay_Log_Space: 2766 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1396 Last_SQL_Error: Error 'Operation ALTER USER failed for 'root'@'localhost'' on query. Default database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Replicate_Ignore_Server_Ids: Master_Server_Id: 2 Master_UUID: 684e1f7d-6f47-11ef-a6d5-005056a3a162 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 240910 15:55:34 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 684e1f7d-6f47-11ef-a6d5-005056a3a162:1-8 Executed_Gtid_Set: ca006ef3-6f46-11ef-8203-005056b9a980:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master-2 Master_TLS_Version: 2 rows in set (0.00 sec)
因为使用基于gtid事务复制,于是尝试使用基于二进制日志的复制
mysql> stop slave; Query OK, 0 rows affected (0.02 sec) mysql> reset slave; Query OK, 0 rows affected (0.02 sec) mysql> change master to master_auto_position=0; ERROR 3079 (HY000): Multiple channels exist on the slave. Please provide channel name as an argument. mysql> change master to master_auto_position=0 for channel 'master-1'; Query OK, 0 rows affected (0.01 sec) mysql> change master to master_auto_position=0 for channel 'master-2'; Query OK, 0 rows affected (0.01 sec) mysql> CHANGE MASTER TO MASTER_HOST='10.18.30.25',MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='binlog.000004',MASTER_LOG_POS=194 FOR CHANNEL 'master-1'; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> CHANGE MASTER TO MASTER_HOST='10.18.30.43',MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='binlog.000003',MASTER_LOG_POS=194 FOR CHANNEL 'master-2'; Query OK, 0 rows affected, 2 warnings (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
查看从库日志
2024-09-10T08:24:19.657675Z 2 [Note] 'CHANGE MASTER TO FOR CHANNEL 'master-1' executed'. Previous state master_host='10.18.30.25', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='10.18.30.25', master_port= 3306, master_log_file='binlog.000004', master_log_pos= 194, master_bind=''. 2024-09-10T08:24:32.746649Z 2 [Note] 'CHANGE MASTER TO FOR CHANNEL 'master-2' executed'. Previous state master_host='10.18.30.43', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='10.18.30.43', master_port= 3306, master_log_file='binlog.000003', master_log_pos= 194, master_bind=''. 2024-09-10T08:24:40.193771Z 7 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information. 2024-09-10T08:24:40.200216Z 8 [Note] Slave SQL thread for channel 'master-1' initialized, starting replication in log 'binlog.000004' at position 194, relay log './localhost-relay-bin-master@002d1.000001' position: 4 2024-09-10T08:24:40.200505Z 9 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information. 2024-09-10T08:24:40.203240Z 10 [Note] Slave SQL thread for channel 'master-2' initialized, starting replication in log 'binlog.000003' at position 194, relay log './localhost-relay-bin-master@002d2.000001' position: 4 2024-09-10T08:24:47.987545Z 9 [Note] Slave I/O thread for channel 'master-2': connected to master 'repl@10.18.30.43:3306',replication started in log 'binlog.000003' at position 194 2024-09-10T08:24:47.987835Z 7 [Note] Slave I/O thread for channel 'master-1': connected to master 'repl@10.18.30.25:3306',replication started in log 'binlog.000004' at position 194
测试同步
主库1:10.18.30.25
mysql> create table t_cs(id int ,name varchar(30)); Query OK, 0 rows affected (0.03 sec) mysql> insert into t_cs values(1,'cs'); Query OK, 1 row affected (0.01 sec) mysql> select * from t_cs; +------+------+ | id | name | +------+------+ | 1 | cs | +------+------+ 1 row in set (0.00 sec)
从库:
mysql> select * from t_cs; +------+------+ | id | name | +------+------+ | 1 | cs | +------+------+ 1 row in set (0.00 sec)
主库2:10.18.30.43
mysql> create table t_repl(t_id int,t_name varchar(50)); Query OK, 0 rows affected (0.19 sec) mysql> insert into t_repl values(1,'jy'); Query OK, 1 row affected (0.01 sec) mysql> select * from t_repl; +------+--------+ | t_id | t_name | +------+--------+ | 1 | jy | +------+--------+ 1 row in set (0.00 sec)
从库:
mysql> select * from t_repl; +------+--------+ | t_id | t_name | +------+--------+ | 1 | jy | +------+--------+ 1 row in set (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.18.30.25 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 662 Relay_Log_File: localhost-relay-bin-master@002d1.000002 Relay_Log_Pos: 785 Relay_Master_Log_File: binlog.000004 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: 662 Relay_Log_Space: 1009 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: 1 Master_UUID: bb8b95d1-6f47-11ef-9592-005056a390e6 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: bb8b95d1-6f47-11ef-9592-005056a390e6:9-10 Executed_Gtid_Set: 684e1f7d-6f47-11ef-a6d5-005056a3a162:9-10, bb8b95d1-6f47-11ef-9592-005056a390e6:9-10, ca006ef3-6f46-11ef-8203-005056b9a980:1-5 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master-1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.18.30.43 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000003 Read_Master_Log_Pos: 669 Relay_Log_File: localhost-relay-bin-master@002d2.000002 Relay_Log_Pos: 792 Relay_Master_Log_File: binlog.000003 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: 669 Relay_Log_Space: 1016 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: 2 Master_UUID: 684e1f7d-6f47-11ef-a6d5-005056a3a162 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: 684e1f7d-6f47-11ef-a6d5-005056a3a162:9-10 Executed_Gtid_Set: 684e1f7d-6f47-11ef-a6d5-005056a3a162:9-10, bb8b95d1-6f47-11ef-9592-005056a390e6:9-10, ca006ef3-6f46-11ef-8203-005056b9a980:1-5 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master-2 Master_TLS_Version: