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