GTID = source_id:sequence_id sourceid:主服務器的惟一標識,一般用server_uuid來表示。 sequence_id:事務提交時由系統順序分配的序列號,在Binlog中是遞增且連續有序。 show master status \G
server_id: 跟傳統的主從同樣,每一個實例不能同樣 gtid_mode: 設置爲ON,開啓GTID模式 enforce_gtid_consistency:設置爲ON,保證GTID一致性 log_bin: 跟傳統的主從同樣必須打開 log_slave_updates:建議開啓 能夠用來級聯複製 binlog_format:強烈建議設置爲ROW,不然會形成數據不一致 #skip-slave-start:設置爲1,SLAVE啓動時,不自動開啓複製
MySQL5.7.6以前,若是數據庫已經啓動,必須重啓數據庫。具體步驟以下:mysql
1.關閉MASTER的寫入 2.SLAVE設置參數 skip-slave-start=1,避免SLAVE啓動後,繼續使用傳統的複製模式。 3.按照上節所述,全部MySQL實例修改配置,開啓GTID模式 4.重啓全部MySQL數據庫
MySQL5.7.6以後,能夠在線調整,再無須重啓了。sql
分三種狀況:數據庫
CHANGE MASTER TO MASTER_HOST='***', MASTER_PORT=*** , MASTER_USER='***', MASTER_PASSWORD='***', MASTER_AUTO_POSITION=1;
環境服務器
CENTOS 7.2 MySQL5.7.22
1.下載安裝xtrabackup網絡
#下載XtraBackup wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm #安裝所需包 yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL rsync perl-Digest-MD5 libev #安裝XtraBackup rpm -ivh percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
2.全部MySQL修改配置,能夠在線修改,同時修改配置文件,以便下次生效app
server_id =101/102 gtid_mode=ON enforce_gtid_consistency=ON log_bin=ON log_slave_updates=ON binlog_format=ROW #skip_slave_start=1
3.備份數據庫,獲取MASTER數據以及GTID範圍ide
#由於主庫纔開啓GTID,模擬一些事務,使其產生一些binlog create table test(id int); insert into test (id)values(1),(2) #備份數據 innobackupex --user=root --password=root --port=3306 /MySQL/my3306/data/fullbackup/ #查看gtid範圍 [root@localhost 2018-07-26_23-23-00]# cat xtrabackup_binlog_info binlog.000012 579 9760cb92-693e-11e8-85bf-000c29b55cf0:1-2
4.COPY備份數據到SLAVE,搭建SLAVE實例ui
#COPY數據 scp -r 2018-07-26_23-23-00 root@192.168.2.138:/MySQL/my3306/fullbackup/ #中止SLAVE service mysqld stop #從庫將原有數據文件夾重命名到新位置,並建立原文件夾 mv /MySQL/my3306/data /MySQL/my3306/databak mkdir -p /MySQL/my3306/data #innobackupex apply-log innobackupex --apply-log --user=root --password=root --port=3606 /MySQL/my3306/fullbackup/2018-07-26_23-23-00/ #innobackupex copy 恢復的文件到原來的數據位置 innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back /MySQL/my3306/fullbackup/2018-07-26_23-23-00/ #給目錄並賦權 chown -R mysql:mysql /MySQL/my3306/data
5.創建同步用戶,跳過GTID,搭建主從線程
#主從庫創建同步用戶 grant replication slave,replication client on *.* to 'rep'@'%' identified by 'rep'; #啓動SLAVE實例,設置gtid_purged的值,跳過GTID範圍 service mysqld start SET @@GLOBAL.GTID_PURGED='9760cb92-693e-11e8-85bf-000c29b55cf0:1-2' #利用CHANGE MASTER語句,配置主從複製 CHANGE MASTER TO MASTER_HOST='192.168.2.144', MASTER_PORT=3306 , MASTER_USER='rep', MASTER_PASSWORD='rep', MASTER_AUTO_POSITION=1; #啓動SLAVE複製,SLAVE會自動跳過GTID範圍,拉取最新的GTID信息 START SLAVE; #查看主從狀況 SHOW SLAVE STATUS \G
6.驗證主從同步code
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.144 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000012 Read_Master_Log_Pos: 1561 Relay_Log_File: relaylog.000002 Relay_Log_Pos: 1387 Relay_Master_Log_File: binlog.000012 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: 1561 Relay_Log_Space: 1587 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: 101 Master_UUID: 9760cb92-693e-11e8-85bf-000c29b55cf0 Master_Info_File: /MySQL/my3306/data/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: 9760cb92-693e-11e8-85bf-000c29b55cf0:3-6 Executed_Gtid_Set: 9760cb92-693e-11e8-85bf-000c29b55cf0:1-6 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)