基于GTIDs的MySQL Replicationhtml
一、GTIDs(Global transaction identifiers)全局事务标识符,是mysql 5.6新加入的一项技术mysql
二、当使用GTIDs时,每个事务均可以被识别而且跟踪sql
三、添加新的slave或者当发生故障须要将master身份或者角色迁移到slave上时,都无需考虑是哪个二进制日志以及哪一个position值,极大简化了相关操做vim
四、GTIDs是彻底基于事务的,所以不支持MYISAM存储引擎服务器
五、GTID由source_id和transaction_id组成:
1>source_id来自于server_uuid,能够在auto.cnf中看到
2>transation_id是一个序列数字,自动生成session
一、不支持非事务引擎(MYisam),由于可能会致使多个gtid分配给同一个事务
二、create table ... select 语句不支持(主库语法报错)
三、create/drop temporary table 语句不支持
四、必须使用enforce-gtid-consistency参数
五、sql-slave-skip-counter不支持(传统的跳过错误方式)
六、GTID复制环境中必需要求统一开启和GTID或者关闭GTID
七、在mysql 5.6.7以前,使用mysql_upgrade命令会出现问题app
1. A transaction is executed and committed on the master.
This transaction is assigned a GTID using the master's UUID and the smallest nonzero transaction sequence number not yet used on this server; the GTID is written to the master's binary log (immediately preceding the transaction itself in the log).
2. After the binary log data is transmitted to the slave and stored in the slave's relay log, the slave reads the GTID and sets the value of its gtid_next system variable as this GTID. This tells the slave that the next transaction must be logged using this GTID.It is important to note that the slave sets gtid_next in a session context.
3. The slave verifies that this GTID has not already been used to log a transaction in its own binary log. If this GTID has not been used, the slave then writes the GTID, applies the transaction, and writes the transaction to its binary log. By reading and checking the transaction's GTID first, before processing the transaction itself, the slave guarantees not only that no previous transaction having this GTID has been applied on the slave, but also that no other session has already read this GTID but has not yet committed the associated transaction. In other words, multiple clients are not permitted to apply the same transaction concurrently.
4. Because gtid_next is not empty, the slave does not attempt to generate a GTID for this transaction but instead writes the GTID stored in this variable—that is, the GTID obtained from the master—immediately preceding the transaction in its binary log.
总结:有了GTID大大的简化了复制的过程,下降了维护的难度ide
在生产环境中,大多数状况下使用的MySQL5.6基本上都是从5.5或者更低的版本升级而来,这就意味着以前的mysql replication方案是基于传统的方式部署,而且已经在运行,所以,接下来咱们就利用已有的环境升级至基于GITDs的Replication测试
传统的方案部署参考:http://www.javashuo.com/article/p-fnkusxgp-bc.htmlui
注意:
一、开启GITDs须要在master和slave上都配置gtid-mode,log-bin,log-slave-updates,enforce-gtid-consistency(该参数在5.6.9以前是--disable-gtid-unsafe-statement)
二、其次,slave还须要增长skip-slave-start参数,目的是启动的时候,先不要把slave起来,须要作一些配置
详细操做步骤:
当前环境是传统的AB复制转换成GTID模式
master:192.168.1.166
slave:192.168.1.114
一、将master和slave服务器都设置为read-only
mysql>set @@global.read_only=ON;
二、中止两台服务器的mysql服务
三、配置master
master: [root@master ~]# vim /etc/my.cnf log-bin=mysql-bin gtid-mode=on log-slave-updates enforce-gtid-consistency [root@master ~]# service mysqld restart
四、配置slave
slave: [root@slave1 ~]# vim /etc/my.cnf gtid-mode=on log-bin log-slave-updates enforce-gtid-consistency skip-slave-start [root@slave1 ~]# service mysqld restart mysql> change master to master_host='192.168.1.166',master_port=3306,master_user='slave',master_password='123',master_auto_position=1; mysql> start slave; mysql> show slave status \G; Auto_Position: 1
五、关闭read-only模式
mysql> set @@global.read_only=OFF;
六、测试
master查看: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | +------+------+ 6 rows in set (0.07 sec) slave查看: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | +------+------+ 6 rows in set (0.07 sec) master插入数据并查看: mysql> insert into db01.table03 values(5,'ouou'); Query OK, 1 row affected (0.04 sec) mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | | 5 | ouou | +------+------+ 7 rows in set (0.00 sec) slave查看: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | | 5 | ouou | +------+------+ 7 rows in set (0.00 sec) 而且master上面操做后查看slave的状态,下面就会有事务产生 mysql> show slave status\G; Retrieved_Gtid_Set: 5624c184-5b55-11e8-b117-000c293dfd08:1 Executed_Gtid_Set: 5624c184-5b55-11e8-b117-000c293dfd08:1 Auto_Position: 1