MySQL集群一般部署为主从架构,主库专门用于写入数据,从库用于读数据。

异常情况下,在从库上写入数据,就会出现主从数据不一致问题,称为errant。

如果从库上的这些数据主库上已经有了,或者主库不需要这些数据,我们就可以使用空事务解决errant问题。

接下来,本文介绍如何使用空事务解决errant问题。

首先查看主库的GTID集合:

>show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 7685
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set: b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:1-17,
c84e52d2-c2fa-11e9-9b19-002211f26ad8:1,
dea2c515-c2fa-11e9-9eef-0022a6d40ab9:1-6
1 row in set (0.00 sec)

查看从库的GITD集合:

>show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 9783
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set: b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:1-23,
c84e52d2-c2fa-11e9-9b19-002211f26ad8:1,
dea2c515-c2fa-11e9-9eef-0022a6d40ab9:1-6
1 row in set (0.02 sec)

找到主库从库的差异:
b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:18-23
这些GTID是主库上没有的。

在主库上使用空事务进行补齐:

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:18';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.00 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:19';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.01 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:20';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.01 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:21';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.01 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:22';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.01 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)

>SET GTID_NEXT='b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:23';
Query OK, 0 rows affected (0.00 sec)

>BEGIN;
Query OK, 0 rows affected (0.01 sec)

>COMMIT;
Query OK, 0 rows affected (0.00 sec)


>SET GTID_NEXT='AUTOMATIC';
Query OK, 0 rows affected (0.00 sec)

最后,查看主库上的GTID集合:

>show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 9783
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set: b7a7c5c5-c2fa-11e9-8bd0-0022bf5315f2:1-23,
c84e52d2-c2fa-11e9-9b19-002211f26ad8:1,
dea2c515-c2fa-11e9-9eef-0022a6d40ab9:1-6
1 row in set (0.02 sec)

可以看到,事务集合已经补齐。

01-13 16:02