GTID是5.6时加入的,在5.7中被进一步完善,生产环境建议在5.7版本中使用.
GTID全称为Global Transaction Identifiers,全局事务标识符.
GTID的复制完全是基于事务的,每一个事务对应一个GTID.因此事务执行具有唯一ID,
主从复制时,无需再指定POS位置,只要对比ID有没被执行过,并且每个ID仅执行一次.
GTID复制限制:
不支持涉及非事务存储引擎的更新
不支持CREATE TABLE … SELECT语句
不支持针对临时表的操作: CREATE TEMPORARY TABLE和DROP TEMPORARY TABLE
配置tips
notice:主从都需要开启gtid_mode模式.
使用GITD复制模式官方建议使用row复制模式,具有最高性能.
一主多从模式下更能体现出多线程的性能;
MySQL5.7.x编译安装方法,参考https://www.aliang.org/MySQL/Centos7-3-install-MySQL5-7-17.html
Master my.cnf配置片段
[mysqld] server-id = 1 #服务器id gtid_mode = on #开启gtid模式 log-bin = /data/mysql/binlog/master-binlog #开启binlog enforce_gtid_consistency = 1 #强制gtid一致性,开启后对于特定create table不被支持 log_slave_update = 1 #开启从库写入binlog binlog_format = row #binlog开启row模式 relay_log_recovery = 1 #开启中继日志完整性(当slave从库宕机后,假如relay-log损坏了,导致一部分中继日志没有处理,则自动放弃所有未执行的relay-log,并且重新从master上获取日志,这样就保证了relay-log的完整性。) sync-binlog = 1 #强制将binlog_cache写入磁盘,一致性要求不高的场景下设置为0可关闭,性能会大幅提速数倍 skip_slave_start = 1 #使slave在mysql启动时不启动复制进程,使用 start slave启动
Slave my.cnf配置片段
[mysqld] server-id = 5 #服务器id gtid_mode = on #开启gtid模式 log-bin = /data/mysql/binlog/slave-binlog #开启binlog enforce_gtid_consistency = 1 #强制gtid一致性,开启后对于特定create table不被支持 log_slave_update = 1 #开启从库写入binlog binlog_format = row #binlog开启row模式 relay_log_recovery = 1 #开启中继日志完整性(当slave从库宕机后,假如relay-log损坏了,导致一部分中继日志没有处理,则自动放弃所有未执行的relay-log,并且重新从master上获取日志,这样就保证了relay-log的完整性。) sync-binlog = 1 #强制将binlog_cache写入磁盘,一致性要求不高的场景下设置为0可关闭,性能会大幅提速数倍 skip_slave_start = 1 #使slave在mysql启动时不启动复制进程,使用 start slave启动
基于GTID的复制
在Master上创建主从复制账号
grant replication slave on *.* to 'repl'@'192.168.121.%' identified by '12345678'; flush privileges;
在Slave上执行
CHANGE MASTER TO MASTER_HOST='192.168.121.163',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='12345678',MASTER_AUTO_POSITION=1; START SLAVE; SHOW SLAVE STATUS\G;
查看主要参数
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
是否一致,然后可以测试主从同步了,master上创建库,查看slave是否同步·
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.121.163 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mybinlog.000002 Read_Master_Log_Pos: 620 Relay_Log_File: c69-164-relay-bin.000002 Relay_Log_Pos: 723 Relay_Master_Log_File: mybinlog.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 620 Relay_Log_Space: 932 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 91a08937-7b0b-11e7-9575-52540061843d Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 91a08937-7b0b-11e7-9575-52540061843d:2-3 Executed_Gtid_Set: 415de1ab-7b4b-11e7-b279-525400f4de41:1, 91a08937-7b0b-11e7-9575-52540061843d:1-3 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
实例配置:
从库my.cnf:
[root@gpu010 rds04]$cat backup-my.cnf # This MySQL options file was generated by innobackupex. # The MySQL server [mysqld] port = 3308 #innodb_checksum_algorithm=innodb #innodb_log_checksum_algorithm=innodb innodb_data_file_path=ibdata1:200M:autoextend innodb_log_files_in_group=2 innodb_log_file_size=1572864000 #innodb_fast_checksum=false #innodb_page_size=16384 #innodb_log_block_size=512 innodb_undo_directory=. innodb_undo_tablespaces=0 server-id = 17 binlog-format = ROW gtid-mode = ON enforce-gtid-consistency = true log-bin = hostname-bin relay-log = hostname-relay-bin relay_log_recovery = 1 sync-binlog = 0 skip_slave_start = 1 #rds_encrypt_data=false #innodb_encrypt_algorithm=aes_128_ecb
xtrabackup_slave_info 文件内容
[root@gpu010 rds04]$cat xtrabackup_slave_info SET GLOBAL gtid_purged='0c0b921a-5808-11e8-9b90-6c92bf623e12:1-589397257, f13130e5-5807-11e8-9b8f-7cd30ae4341e:1-725361734'; CHANGE MASTER TO MASTER_AUTO_POSITION=1;
主从配置过程:
mysql> stop slave; Query OK, 0 rows affected (0.05 sec) mysql> SET GLOBAL gtid_purged='0c0b921a-5808-11e8-9b90-6c92bf623e12:1-589397257, f13130e5-5807-11e8-9b8f-7cd30ae4341e:1-725361734'; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_host='rm-bp1i0***********cs.com',master_user='zh****in',master_port=3306,master_password='8*******',master_auto_position=1; mysql> start slave; Query OK, 0 rows affected (0.16 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Queueing master event to the relay log Master_Host: rm-b**************cs.com Master_User: zh******in Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.024340 Read_Master_Log_Pos: 284315203 Relay_Log_File: hostname-relay-bin.000002 Relay_Log_Pos: 171740 Relay_Master_Log_File: mysql-bin.024340 Slave_IO_Running: Yes Slave_SQL_Running: Yes