​GTID复制模式保错

发布时间 2023-07-13 17:46:25作者: w'dwd

环境

主库:192.168.1.144:5543
从库:192.168.1.144:5544

概念

gtid_executed等价Executed_Gtid_Set参数,已经执行的gtid集合(gtid-sets)。
gtid_purged,已经清除的gtid集合。
Retrieved_Gtid_Set:从库已经接收到主库的事务编号(从库的IO线程已经接受到了)
Executed_Gtid_Set:已经执行的事务编号(从库的执行sql线程已经执行了的sql)

主库uuid:

 MySQL  localhost:5543 ssl  SQL > show variables like '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | a6b40c9b-197d-11ee-ae87-52540017cf67 |
+---------------+--------------------------------------+
1 row in set (0.0102 sec)

从库uuid:

 MySQL  localhost:5544 ssl  SQL > show variables like '%uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 43cd27b1-197e-11ee-b2dc-52540017cf67 |
+---------------+--------------------------------------+
1 row in set (0.0153 sec)

主从状态:

在从库上使用SHOW REPLICA STATUS 看到如下结果:

 MySQL  localhost:5544 ssl  SQL > SHOW REPLICA STATUS \G;
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 127.0.0.1
                  Source_User: mysql_innodb_rs_1050515673
                  Source_Port: 5543
                Connect_Retry: 60
              Source_Log_File: localhost-bin.000002
          Read_Source_Log_Pos: 45804
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 7626
        Relay_Source_Log_File: localhost-bin.000002
           Replica_IO_Running: Yes
          Replica_SQL_Running: Yes
						......
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: a6b40c9b-197d-11ee-ae87-52540017cf67:66-79
            Executed_Gtid_Set: 43cd27b1-197e-11ee-b2dc-52540017cf67:1-3,
a6b40c9b-197d-11ee-ae87-52540017cf67:1-79
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 1
            Network_Namespace: 
1 row in set (0.0026 sec)

复制状态是双Yes,但是可以看到Executed_Gtid_Set的值有2个,Retrieved_Gtid_Set值有一个。
再来主库通过show master status查看主库上的Executed_Gtid_Set,

 MySQL  localhost:5543 ssl  SQL > show master status\G
*************************** 1. row ***************************
             File: localhost-bin.000002
         Position: 45804
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: a6b40c9b-197d-11ee-ae87-52540017cf67:1-79

其中主库的Executed_Gtid_Set为:a6b40c9b-197d-11ee-ae87-52540017cf67:1-79

分析

1、可以看见主库产生了79个事务,从库接收了79个事务,并且已经执行;
2、已执行的事务Executed_Gtid_Set: 43cd27b1-197e-11ee-b2dc-52540017cf67:1-3,
a6b40c9b-197d-11ee-ae87-52540017cf67:1-79 中,
a6b40c9b-197d-11ee-ae87-52540017cf67:1-79是执行主库1-79的事务,
43cd27b1-197e-11ee-b2dc-52540017cf67:1-3是从库自己写入的数据

解决方法

第一步、stop slave;
并记录对应的Executed_Gtid_Set值:a6b40c9b-197d-11ee-ae87-52540017cf67:1-79

第二步、show master statusG
查看对应Executed_Gtid_Set值是否和上面保持一致,一般情况下是一致的。

第三步:reset master;
重置从库上的binlog,该操作会清空从库上的mysql.gtid_executed表中的内容

第四步:set global gtid_purged='a6b40c9b-197d-11ee-ae87-52540017cf67:1-79';
重置从库的gtid_purged的值

第五步:start slave; 重启复制关系

第六步:show slave statusG   查看最终结果
				Retrieved_Gtid_Set: a6b40c9b-197d-11ee-ae87-52540017cf67:66-79
            	Executed_Gtid_Set: a6b40c9b-197d-11ee-ae87-52540017cf67:1-79
                Auto_Position: 1