牛刀小试MySQL--GTID小结

it2022-05-05  178

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数据表中。 3.实战例子: 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 ,另外一个是1         其中 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) 复制代码 备注:这里可以看到他们并不是,马上回写入至gtid_executed数据表中。 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) 复制代码 备注:看到只要日志轮询之后,事务就会被写入至gtid_executed数据表中,而且会数据表的压缩技术,控制压缩的变量参数为: [size=14.256px]gtid_executed_compression_period [size=14.256px] ,默认值为1000。 [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!!!

最新回复(0)