1. 程式人生 > >MySQL5.7.18基於GTID的主從複製過程實現

MySQL5.7.18基於GTID的主從複製過程實現

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:
[[email protected] 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 檔案內容
[[email protected] 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