GTID的概念
何为GITD
GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出。事务标识不仅对于Master(起源)的服务器来说是惟一的,而且在整个复制拓扑架构来说,也是全局唯一的。
1.GTID的格式
GTID = source_id:transaction_id
GTID分为两部分,source_id和transaction_id。source_id是通过使用MySQL服务的server_uuid来表示 。transaction_id 是在事务提交的时候由系统顺序分配的一个序列号。
使用show master status查看当前实例执行过的GTID事务信息。如下:
(root@localhost) [Ztest]> show master statusG;
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 1959
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 4160e9b3-58d9-11e8-b174-005056af6f24:1-10
1 row in set (0.00 sec)
可以看出,本实例的source_id为4160e9b3-58d9-11e8-b174-005056af6f24,transaction_id为1-10,说明是提交了10个事务。
MySQL数据库服务的uuid的查询方式。
(root@localhost) [(none)]> show GLOBAL VARIABLES like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 4160e9b3-58d9-11e8-b174-005056af6f24 |
+---------------+--------------------------------------+
1 row in set (0.02 sec)
2.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)
3.GTID的管理
MySQL库中新增了gtid_exectued表,在MySQL 8.0中表结构如下:
(root@localhost) [(none)]> use mysql
Database changed
(root@localhost) [mysql]> show create table gtid_executed G;
*************************** 1. row ***************************
Table: gtid_executed
Create Table: CREATE TABLE `gtid_executed` (
`source_uuid` char(36) NOT NULL COMMENT 'uuid of the source where the transaction was originally executed.',
`interval_start` bigint(20) NOT NULL COMMENT 'First number of interval.',
`interval_end` bigint(20) NOT NULL COMMENT 'Last number of interval.',
PRIMARY KEY (`source_uuid`,`interval_start`)
) /*!50100 TABLESPACE `mysql` */ ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
查看目前已经执行过的事务,语句如下:
(root@localhost) [mysql]> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 26 |
+--------------------------------------+----------------+--------------+
1 row in set (0.01 sec)
可以分析
- 当未开启binlog时,每个事务会记录到gitd_executed表中。
- 当开启binlog时,事务不会立即写入gitd_executed表中,只有当Binlog rotate轮询时亦或者数据库服务关闭时,会把事务写入至gtid_executed表中。
实验效果如下:
1.插入数据前的gtid_executed表的情况:
(root@localhost) [mysql]> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 26 |
+--------------------------------------+----------------+--------------+
1 row in set (0.01 sec)
2.插入准备数据:
insert into ztest.zstudent(stu_name,sex) values('hrd30','M');
insert into ztest.zstudent(stu_name,sex) values('hrd31','M');
commit;
3.插入数据后的gtid_executed表的情况:
(root@localhost) [mysql]> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 26 |
+--------------------------------------+----------------+--------------+
1 row in set (0.00 sec)
如上情况,没有任何改变,Binlog rotate后,查看gtid_executed表的情况
(root@localhost) [mysql]> flush logs;
Query OK, 0 rows affected (0.01 sec)
(root@localhost) [mysql]> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 28 |
+--------------------------------------+----------------+--------------+
1 row in set (0.00 sec)
可以看到,上述提交的两个事务,在binlog刷新之后,写入到了gitd_executed表中。
知识点备注:
RESET MASTER会清空gitd_executed表。