阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

深入理解MySQL GTID

123次阅读
没有评论

共计 3922 个字符,预计需要花费 10 分钟才能阅读完成。

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 表。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3922字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中