1.GTID的概念
GTID(global transaction identifier)是全局事務標識符,在MySQL5.6版本中作為一個超級特性被推出。事務標識不僅對於Master(起源)的服務器來說是惟一的,而且在整個復制拓撲架構來說,也是全局唯一的。
1)GTID的格式
GTID = source_id:transaction_id
其中source_id
:通過使用MySQL服務的server_uuid來表示 ,transaction_id
:是在事務提交的時候系統順序分配的一個序列號
2)mysql.gtid_executed表
GTIDs都存儲在gtid_executed數據表中,在mysql系統數據庫中。每一行的數據代表一個GTID或者一個GTID集合。包括source_uuid,集合開始的事務id和集合結束的事務id
表結構如下:
CREATE TABLE gtid_executed (
source_uuid CHAR(36) NOT NULL,
interval_start BIGINT(20) NOT NULL,
interval_end BIGINT(20) NOT NULL,
PRIMARY KEY (source_uuid, interval_start)
)
備注:事務並不是立馬寫進gtid_executed表。當啟用二進制日志的時候(log-bin = /data/mysqldata/3306/binlog/mysql-bin),只有日志被輪詢或者數據庫服務被關閉的時候,才會把所有的日志寫入到gtid_executed數據表中。
2.實戰例子
1)關閉數據庫
/usr/local/mysql/bin/mysqladmin -uroot -p'zsd@7101' shutdown
2)修改my.cnf文件
gtid_mode=ON
enforce-gtid-consistency=true
log-slave-updates=1
binlog_format= row
skip-slave-start=1
innodb_flush_log_at_trx_commit=2
sync_binlog=30
3)啟動數據庫
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
4)執行一條數據
insert into zstudent(stu_name,sex) values('hrd','M');
commit;
5)查看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
1 row in set (0.00 sec)
備注:可以看到GTID集中分為了兩段.
其中4160e9b3-58d9-11e8-b174-005056af6f24
就是server的uuid,1
就是序列號。一直排序下去的。
6)server的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)
7)開始繼續插入數據
insert into zstudent(stu_name,sex) values('hrd12','M');
insert into zstudent(stu_name,sex) values('hrd13','M');
insert into zstudent(stu_name,sex) values('hrd14','M');
insert into zstudent(stu_name,sex) values('hrd15','M');
insert into zstudent(stu_name,sex) values('hrd12','M');
commmit;
8)查看gtid_executed數據表
(root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 11 |
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 12 | 12 |
+--------------------------------------+----------------+--------------+
2 rows in set (0.00 sec)
如上記錄,可以看出它們並不是,馬上回寫入至gtid_executed數據表中。
9)flush log之后,再次查看gtid_executed數據表
(root@localhost) [(none)]> flush logs;
Query OK, 0 rows affected (0.01 sec)
(root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 4160e9b3-58d9-11e8-b174-005056af6f24 | 1 | 19 |
+--------------------------------------+----------------+--------------+
1 row in set (0.00 sec)
備注:看到日志輪詢之后,事務就會被寫入至gtid_executed數據表中,而且會用到數據表的壓縮技術,控制壓縮的變量參數為:
gtid_executed_compression_period
,默認值為1000。
知識點小總結:由於是否開啟了GTID,關鍵是上面提到的兩個參數
gtid_mode=ON
enforce-gtid-consistency=true
驗證上述參數,在MYSQL服務中是否生效,用如下命令:
(root@localhost) [(none)]> show variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 4160e9b3-58d9-11e8-b174-005056af6f24:1-19 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
9 rows in set (0.01 sec)
至此,對於GTID一個簡單的介紹和運用,