1、在主库进行一次全备
mysqldump -uroot -p --single-transaction --master-data=2 --all-databases -R -E -e -q --triggers >/home/sa/all_databases_20190523.sql
切记: 不要加--set-gtid-purged=off 参数,不然导入从库后,报 error 1236的错误。
/usr/local/mysql/bin/mysqldump -uroot -p --set-gtid-purged=off -R -E --triggers --single-transaction --hex-blob --skip-opt --add-drop-table --create-options -e -q --set-charset --all-databases >/home/all_databases_20190318.sql
mysqldump -uroot -p --skip-opt --create-options --add-drop-table --set-charset --single-transaction -q -e --triggers -R -E --set-gtid-purged=OFF --hex-blob Tracker114 T1_Doctor XR_DoctorTitle T2_AppointmentOrderForService XR_DutySource XR_Schedule T1_AppointmentOrder DW_Order_ThirdParty DW_Doctor_ThirdParty >/home/sa/Tracker114_20191016_new.sql
在新的服务器上建立主从复制关系:
mysql> change master to master_user='repl', master_host='172.18.16.120',master_password='Repl123#',master_auto_position=1;
ERROR1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
然后根据提示,查看error log
[[email protected] log]# tail -f mysqld.log
2019-08-27T12:03:49.321764Z 6 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.321835Z 6 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.321885Z 6 [Warning] 'user' entry 'sysbench@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.321953Z 6 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.321963Z 6 [Warning] 'db' entry 'sbtest sysbench@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.321994Z 6 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:03:49.322400Z 6 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-08-27T12:12:13.148834Z 8 [Note] Access denied for user 'root'@'localhost'(using password: YES)2019-08-27T12:15:57.111498Z 9 [Note] Slave: MTS group recovery relay log info based on Worker-Id 1, group_relay_log_name /home/data/mysql57/relay/relay-bin.001715, group_relay_log_pos 247807655 group_master_log_name mysql-bin.000820, group_master_log_pos 247807442
2019-08-27T12:15:57.111528Z 9 [ERROR] Error looking for /home/data/mysql57/relay/relay-bin.001715.
解决方法:在新的从库上,重置relay log日志
mysql> reset slave all;
Query OK, 0 rows affected (0.00 sec)
接着继续,在新的服务器上继续建立主从复制关系:
mysql> change master to master_user='repl', master_host='172.18.16.120',master_password='Repl123#',master_auto_position=1;
Query OK,0 rows affected, 2 warnings (0.02sec)
mysql>start slave;
Query OK,0 rows affected (0.01 sec)
查看从库的复制状态:
mysql>show slave status\G;*************************** 1. row ***************************Slave_IO_State:
Master_Host:172.18.16.120Master_User: repl
Master_Port:3306Connect_Retry:60Master_Log_File:
Read_Master_Log_Pos:4Relay_Log_File: relay-bin.000001Relay_Log_Pos:4Relay_Master_Log_File:
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0Last_Error:
Skip_Counter:0Exec_Master_Log_Pos:0Relay_Log_Space:154Until_Condition: None
Until_Log_File:
Until_Log_Pos:0Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:1236Last_IO_Error: Got fatal error1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'Last_SQL_Errno:0Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:120Master_UUID: 0851a6d6-c8af-11e9-a81c-fa163efebf11
Master_Info_File: mysql.slave_master_info
SQL_Delay:0SQL_Remaining_Delay:NULLSlave_SQL_Running_State: Slave hasread all relay log; waiting formore updates
Master_Retry_Count:86400Master_Bind:
Last_IO_Error_Timestamp:190827 20:23:05Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: a14948ec-c8b4-11e9-935a-fa163ef7bf86:1-2Auto_Position:1Replicate_Rewrite_DB:
Channel_Name:
查看error log日志
2019-08-27T12:23:05.777385Z 10 [ERROR] Error reading packet from server for channel '': The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires. (server_errno=1236)2019-08-27T12:23:05.777424Z 10 [ERROR] Slave I/O for channel '': Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.', Error_code: 1236
2019-08-27T12:23:05.777431Z 10 [Note] Slave I/O thread exiting for channel '', read up to log 'FIRST', position 4
解决方法:
1、查看主库的master gtid_purged 2、show global variables like '%gtid%'; 找到gtid_purged的值
2、set @@global.gtid_purged='6b156871-8ae7-11e7-88bb-fa163e55e9ea:1-35268634';