一、配置mysql gtid下主从复制
master库192.195.1.131操作:
[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操作:
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"
二、mysql gtid主从复制报错解决
同步报错:
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库上又创建相同的库名,从而导致的报错。
分析解决过程:
主库查看binlog文件mysql-bin.000004内容,发现是一开始配置主从库时,主从server_id相同导致的复制失败,于是修改主库的server_id,记录到mysql-bin.000004文件中(从提交的事物的Gtid可以看出来).
[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库上跳过这个事物:
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)
注入空事物:
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)
到此处主从复制报错解决:
[root@localhost data]# mysql -e "show slave status\G" |grep -i "yes"
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@localhost data]#