深入浅出mysql gtid_深入理解MySQL GTID
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表。
總結
以上是生活随笔為你收集整理的深入浅出mysql gtid_深入理解MySQL GTID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 表单构建html的页面代码,网页设计表单
- 下一篇: 方舟生存进化刷精英恐龙代码