master庫192.195.1.131操做:mysql
[root@VM_82_178_centos ~]# mysql -e "grant replication slave on *.* to rep@'192.195.1.233' identified by '23458&34%'; flush privileges;" [root@VM_82_178_centos ~]# mysqldump -uroot -p'jianwuweirootmysql' -B -A -F --master-data=2 --single-transaction --events|gzip >/opt/test_$(date +%F).sql.gz mysqldump: [Warning] Using a password on the command line interface can be insecure. Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
slave庫192.195.1.233操做:sql
root@localhost [(none)]>show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000005 | 359 | | | 0427a054-e3df-11e8-94d7-bcaec502b1c7:1-4 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec) root@localhost [(none)]> [root@localhost ~]# mysql < test_2018-11-27.sql ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty. [root@localhost ~]#
root@localhost [(none)]>reset master; Query OK, 0 rows affected (0.03 sec) root@localhost [(none)]>show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) [root@localhost ~]# mysql < test_2018-11-27.sql
mysql -e "CHANGE MASTER TO MASTER_HOST='192.195.1.131',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='23458&34%',MASTER_AUTO_POSITION = 1;start slave;show slave status\G" |grep -i "yes"
同步報錯:centos
Last_Errno: 1007 Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'df8f817c-f215-11e8-83e4-525400950067:5' at master log mysql-bin.000004, end_log_pos 359. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
slave庫報錯1007,是slave庫上已經存在庫名,然而master庫上又建立相同的庫名,從而致使的報錯。
分析解決過程:session
主庫查看binlog文件mysql-bin.000004內容,發現是一開始配置主從庫時,主從server_id相同致使的複製失敗,因而修改主庫的server_id,記錄到mysql-bin.000004文件中(從提交的事物的Gtid能夠看出來).app
[root@VM_82_178_centos binlog]# /usr/local/mysql7/bin/mysqlbinlog -vv --base64-output=decode-rows mysql-bin.000004|grep -B 25 'end_log_pos 359' /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; #at 4 #181127 15:51:00 server id 2333306 end_log_pos 123 CRC32 0x4acc26d3 Start: binlog v 4, server v 5.7.24-log created 181127 15:51:00 #Warning: this binlog is either in use or was not closed properly. #at 123 #181127 15:51:00 server id 2333306 end_log_pos 194 CRC32 0x37fe34ae Previous-GTIDs #df8f817c-f215-11e8-83e4-525400950067:1-4 #at 194 #181127 16:09:58 server id 1313306 end_log_pos 259 CRC32 0x6d246cbf GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= 'df8f817c-f215-11e8-83e4-525400950067:5'/*!*/; #at 259 #181127 16:09:58 server id 1313306 end_log_pos 359 CRC32 0x37df96be Query thread_id=6 exec_time=0 error_code=0
slave庫上跳過這個事物:ide
root@localhost [(none)]>show variables like "%gtid%"; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 8 rows in set (0.00 sec)
注入空事物:this
root@localhost [(none)]>stop slave sql_thread; ###此步驟能夠忽略 Query OK, 0 rows affected (0.02 sec) root@localhost [(none)]>set gtid_next='df8f817c-f215-11e8-83e4-525400950067:5'; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>begin; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>commit; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>show master status\G *************************** 1. row *************************** File: mysql-bin.000001 Position: 356 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: df8f817c-f215-11e8-83e4-525400950067:5 1 row in set (0.01 sec) root@localhost [(none)]>set gtid_next='AUTOMATIC'; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>start slave sql_thread; Query OK, 0 rows affected (0.02 sec)
到此處主從複製報錯解決:3d
[root@localhost data]# mysql -e "show slave status\G" |grep -i "yes" Slave_IO_Running: Yes Slave_SQL_Running: Yes [root@localhost data]#