东瑜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  135 随笔 :: 0 文章 :: 11 评论 :: 21万 阅读

作者:@张扶摇
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhangshengdong/p/9171643.html


目录

GTID的概念
何为GITD

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 status\G;
*************************** 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表。



感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接
posted on   东瑜  阅读(191)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
\\页脚html代码
点击右上角即可分享
微信分享提示