GTID是什么


GTID(Global Transaction Identifier)是事务在第一次提交时获取到的唯一标识,该标识在整个的复制拓扑中具有唯一性。



GTID组成


GTID = source_id:transaction_id(c4e5d4a0-a9be-11e9-b444-080027f22add:7)


直观看,GTID有两部分组成,source_id和transaction_id。source_id代表事务提交时所在的Master实例,一般是由该实例的全局系统变量server_uuid表示;transaction_id代表事务在该实例上提交的顺序,其为大于或等于1的正向递增序列。



GTID集合


集合,是由一个或多个确定的元素所构成的整体。GTID集合,顾名思义,这其中的元素就是GTID,由单个,或多个GTID,又或由一个范围的GTID组成。如,3E11FA47-71CA-11E1-9E33-C80AA9429562:1-3:11:47-49。在实例中全局系统变量gtid_executed和gtid_purged往往存储的就是GTID集合。


GTID集合也是有格式的,如下:


gtid_set:

    uuid_set [, uuid_set] ...

    | ''


uuid_set:

    uuid:interval[:interval]...


uuid:

    hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh


h:

    [0-9|A-F]


interval:

    n[-n]


    (n >= 1)



GTID存储


GTID是存储在系统表mysql.gtid_executed中的,该表一行记录代表单个GTID,或GTID集合。


+--------------------------------------+----------------+--------------+

| source_uuid                          | interval_start | interval_end |

+--------------------------------------+----------------+--------------+

| aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa |              1 |           72 |

| c4e5d4a0-a9be-11e9-b444-080027f22add |         101005 |       188707 |

+--------------------------------------+----------------+--------------+


GTID何时被写入mysql.gtid_executed表呢…,这取决于二进制日志是否开启。


  • 当禁用二进制日志时(该情况一般出现在Slave上),MySQL在执行(回放)完GTID(事务)后,同时将该GTID写入mysql.gtid_executed表。在5.7版本中,对于DML语句,该过程是原子性的,但对于DDL语句不是原子性的(在8.0版本中,DML和DDL语句都是原子性的了。)。


  • 当开启二进制日志时,在二进制日志轮换,或实例关闭时,MySQL会将之前二进制日志中全部GTID写入mysql.gtid_executed表。若MySQL意外关闭了,在恢复(recovery)阶段,没写入的GTID会再次被写入mysql.gtid_executed(当然关闭了二进制日志,恢复时,没写入的GTID是不能恢复的,之后复制也是无法继续的)。


不难看出启用二进制日志时,mysql.gtid_executed表中的GTID不能代表全部的事务,该信息则是由全局系统变量@@GLOBAL.gtid_executed提供的。


[root@mysql.sock][(none)]> select * from mysql.gtid_executed;

+--------------------------------------+----------------+--------------+

| source_uuid                          | interval_start | interval_end |

+--------------------------------------+----------------+--------------+

| aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa |              1 |           72 |

| c4e5d4a0-a9be-11e9-b444-080027f22add |         101005 |       188707 |

+--------------------------------------+----------------+--------------+

4 rows in set (0.00 sec)


[root@mysql.sock][(none)]> select @@global.gtid_executed;

+-----------------------------------------------------------------------

| @@global.gtid_executed                                               |

+-----------------------------------------------------------------------

| aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-76:,

c4e5d4a0-a9be-11e9-b444-080027f22add:101005-338847 |

+-----------------------------------------------------------------------

1 row in set (0.00 sec)



注意,reset master会将mysql.gtid_executed表清空。


[root@mysql.sock][(none)]> select * from mysql.gtid_executed;

+--------------------------------------+----------------+--------------+

| source_uuid                          | interval_start | interval_end |

+--------------------------------------+----------------+--------------+

| c4e5d4a0-a9be-11e9-b444-080027f22add |              1 |       188708 |

+--------------------------------------+----------------+--------------+

1 row in set (0.00 sec)


[root@mysql.sock][(none)]> select @@global.gtid_executed;

+-----------------------------------------------+

| @@global.gtid_executed                        |

+-----------------------------------------------+

| c4e5d4a0-a9be-11e9-b444-080027f22add:1-888712 |

+-----------------------------------------------+

1 row in set (0.00 sec)


[root@mysql.sock][(none)]> select @@global.gtid_purged;

+-----------------------------------------------+

| @@global.gtid_purged                          |

+-----------------------------------------------+

| c4e5d4a0-a9be-11e9-b444-080027f22add:1-101004 |

+-----------------------------------------------+

1 row in set (0.00 sec)


[root@mysql.sock][(none)]> reset master;

Query OK, 0 rows affected (0.04 sec)


[root@mysql.sock][(none)]> select * from mysql.gtid_executed;

Empty set (0.00 sec)


[root@mysql.sock][(none)]> select @@global.gtid_executed;

+------------------------+

| @@global.gtid_executed |

+------------------------+

|                        |

+------------------------+

1 row in set (0.00 sec)


[root@mysql.sock][(none)]> select @@global.gtid_purged;

+----------------------+

| @@global.gtid_purged |

+----------------------+

|                      |

+----------------------+

1 row in set (0.00 sec)