MySQL GTID (三)

五.如何跳過一個GTID

環境見系列一mysql

5.1 建立表,模擬數據

#主機上
create table t_test (id int primary key ,name varchar(10));

insert into t_test values(1,'aa'),(2,'bb'),(3,'cc');

#備機上插入一條,模擬衝突
insert into t_test values(4,'dd');

5.2 模擬衝突

#主機上
insert into t_test values(4,'dd');

#備機上查看複製狀態
show slave status \G;

5.3 經過上圖,能夠定位到衝突的位置,mysqlbinlog查看具體的語句

#主庫上mysqlbinlog 查看相關語句
mysqlbinlog --start-position=930 --stop-position=1193 -d test --base64-output=DECODE-ROWS -v /MySQL/my3306/log/binlog/binlog.000018

5.4 基於GTID模式的複製,跳過一個事務,須要利用一個空事務。

stop slave;
set GTID_NEXT='9760cb92-693e-11e8-85bf-000c29b55cf0:11';

#開啓一個空事務
begin;commit;

SET GTID_NEXT='AUTOMATIC';
start slave ;

#查看複製是否正常
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.000019
          Read_Master_Log_Pos: 194
               Relay_Log_File: relaylog.000017
                Relay_Log_Pos: 357
        Relay_Master_Log_File: binlog.000019
             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: 194
              Relay_Log_Space: 804
              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:8-11
            Executed_Gtid_Set: 790ff8a6-918a-11e8-87db-000c29c27768:1,
9760cb92-693e-11e8-85bf-000c29b55cf0:1-11
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

六.利用GTID模式快速改變主從複製關係

原架構:主:192.168.2.144
從:192.168.2.138/192.168.2.147sql

先架構:改成級聯模式架構

基於GTID複製,DBA能夠快速調整複製的拓撲結構,只須要調整複製節點的基本信息,不須要手動尋找複製點app

6.1 中止192.168.2.147實例的複製

STOP SLAVE;

6.2 調整192.168.2.147實例的複製關係,修改複製源爲138,MASTER_AUTO_POSITION爲1

CHANGE MASTER TO MASTER_HOST='192.168.2.138',
                 MASTER_PORT=3306,
                 MASTER_AUTO_POSITION=1;

6.3 啓動192.168.2.147

START SLAVE;

6.4 觀察複製的狀況

SHOW SLAVE STATUS \G;

6.5 START SLAVE後,節點138 與節點 147的交互以下

1.147節點向138節點發起一個Dump Binlg請求,並將自身已經執行的GTID集合信息一塊兒發送給138節點。
2.138節點經過對比接收到147節點發送過來的GTID集合,將147節點未執行的Binlog信息發送給C節點。
3.147節點獲取未執行的Binlog信息,並應用這些Binlog,在這個過程當中,138節點還會不斷的發送最新的Binlog到147.
4.147節點不斷的apply Binlog,最終實現147節點與138節點的同步。

正常來講,到這一步級聯複製就創建起來了,可是因爲本文以前在192.168.2.138上跳過一個GTID,致使報錯
code

解決方案:

RESET MASTER;  

#在 192.168.2.138上 查詢Executed_Gtid_Set
show MASTER status \G;
*************************** 1. row ***************************
             File: binlog.000007
         Position: 1385
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 790ff8a6-918a-11e8-87db-000c29c27768:1,
9760cb92-693e-11e8-85bf-000c29b55cf0:1-11
1 row in set (0.00 sec)

#在192.168.2.147跳過這些GTID
SET GLOBAL GTID_PURGED='9760cb92-693e-11e8-85bf-000c29b55cf0:1-11';
START SLAVE; 

#此時複製正常
show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.138
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000007
          Read_Master_Log_Pos: 1385
               Relay_Log_File: relaylog.000003
                Relay_Log_Pos: 445
        Relay_Master_Log_File: binlog.000007
             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: 1385
              Relay_Log_Space: 1203
              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: 102
                  Master_UUID: 790ff8a6-918a-11e8-87db-000c29c27768
             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: 790ff8a6-918a-11e8-87db-000c29c27768:1
            Executed_Gtid_Set: 9760cb92-693e-11e8-85bf-000c29b55cf0:1-11
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
相關文章
相關標籤/搜索