GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出。事务标识不仅对于Master(起源)的服务器来说是惟一的,而且在整个复制拓扑架构来说,也是全局唯一的。
1)GTID的格式为:
- GTID = source_id: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数据表中。
3.实战例子:
1)关闭数据库:
- usr/local/mysql/bin/mysqladmin -uroot -p'zsd@7101' shutdown
- 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;
- (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)
其中4160e9b3-58d9-11e8-b174-005056af6f24就是server的uuid
1就是序列号。一直排序下去的。
*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)
6)开始继续插入数据
- 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;
7)查看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)
8)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)
[size=14.256px]
知识点小总结:由于是否开启了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一个简单的介绍和运用,下一篇帖子会讲一个基于GTID的replication。
和innodb引擎关于innodb_flush_log_at_trx_commit和sync_binlog两个参数的内部机理的总结。be continue!!!