MySQL5.7 的GTID复制
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
在MySQL5.6之后其官方推出了GTID复制方式,和传统的基于bin log复制方式有所不同,接下来我们一起来了解一下它!
一.什么是GTID(global transaction identitifiers)
GTID复制是完全基于事务的复制,即每个在主库上执行的事务都会被分配到一个唯一的全局ID并记录和应用在主库上。
这种复制方式简化了简历slave和master/slave之间的切换工作,因为其完全不需要找当前执行的bin log和log中的位置完成切换。
一个GTID是master上执行的任何commit事务所分配的全局唯一ID标识,其由两部分组成。即GTID = source_id:transaction_id。source_id代表主库的server_uuid,transaction_id代表事务按顺序提交的ID,比如第一个提交则是1,第十个提交的事物则是10.
GTID集合代表一组GTID.
二.MySQL基于GTID的复制
1>.MySQL基于GTID复制的原理
当一个事务在主库提交时,该事务就被赋予了一个GTID,并记录在主库的binary log
主库的binary log会被传输到从库的relay log中,从库读取此GTID并生成gtid_next系统参数
从库验证此GTID并没有在自己的binary log 中使用,则应用此事物在从库上。
2>.MySQL5.6和MySQL5.7GTID复制模式之间的区别
MySQL5.6的GTID复制模式,slave必须开启binary log和log_slave_updates参数,否则启动就报错,因为需要在binary log找到同步复制的信息(UUID:事务号),注意,开启log_slave_updates参数,是把relay-log里的日志内容再记录到本地的binary log里。
但在MySQL5.7里,官方卫做了调整,用一张gtid_executed系统表记录同步复制的信息(UUID:事务号),这样就可以不用开启log_slave_updates参数,减少了从库的压力。
从MySQL5.7.4版本开始,GTID会存在在mysql系统库的gtid_executed表中。
[root@node102 ~]# mysql -uroot -pyinzhengjie mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> mysql> mysql> USE mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> mysql> desc gtid_executed; +----------------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------+------+-----+---------+-------+ | source_uuid | char(36) | NO | PRI | NULL | | | interval_start | bigint(20) | NO | PRI | NULL | | | interval_end | bigint(20) | NO | | NULL | | +----------------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> mysql> SELECT * FROM gtid_executed; Empty set (0.00 sec) mysql> mysql>