天天看點

牛刀小試MySQL--GTID

牛刀小試MySQL--GTID

GTID的概念

何為GITD

GTID(global transaction identifier)是全局事務辨別符,在MySQL5.6版本中作為一個超級特性被推出。事務辨別不僅對于Master(起源)的伺服器來說是惟一的,而且在整個複制拓撲架構來說,也是全局唯一的。

1.GTID的格式

GTID分為兩部分,source_id和transaction_id。source_id是通過使用MySQL服務的server_uuid來表示 。transaction_id 是在事務送出的時候由系統順序配置設定的一個序列号。

使用show master status檢視目前執行個體執行過的GTID事務資訊。如下:

([email protected]) [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的查詢方式。

([email protected]) [(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中表結構如下:

([email protected]) [(none)]> use mysql
Database changed
([email protected]) [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)                

檢視目前已經執行過的事務,語句如下:

([email protected]) [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表的情況:
([email protected]) [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表的情況:
([email protected]) [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表的情況

([email protected]) [mysql]> flush logs;
Query OK, 0 rows affected (0.01 sec)

([email protected]) [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 2018-06-12 09:50 東瑜 閱讀( ...) 評論( ...) 編輯 收藏

轉載于:https://www.cnblogs.com/zhangshengdong/p/9171643.html