閱讀681 返回首頁    go 微軟 go windows


GTID跳過單個、多個事務的方法

[toc]

1、概述

使用gtid跳過事務有兩種方法:
1. set gtid_next,可以跳過單個事務
2. set GTID_PURGED,可以跳過多個事務

2、跳過單個事物

情景:

主新建了test.t1 表,從誤操作刪掉了test.t1;
主往test.t1中插入一條數據,從庫報錯
  • 主:

    mysql> begin;
    mysql> insert into test.t1 values (1,1);
    mysql> commit;
    mysql> show master status \G
    *************************** 1. row ***************************
            File: mysql-bin.000002
        Position: 3286
    Binlog_Do_DB: 
    Binlog_Ignore_DB: 
    Executed_Gtid_Set: 59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-17
    1 row in set (0.00 sec)
    
  • 從:

    mysql> show slave status \G
    *************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.234.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 3286
              Relay_Log_File: ser2-relay-bin.000006
                Relay_Log_Pos: 1141
        Relay_Master_Log_File: mysql-bin.000002
            Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
          Replicate_Do_Table:
      Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:
                  Last_Errno: 1146
                  Last_Error: Worker 3 failed executing transaction '59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:17' at master log mysql-bin.000002, end_log_pos 3255; Error executing row event: 'Table 'test.t1' doesn't exist'
                Skip_Counter: 0
          Exec_Master_Log_Pos: 3045
              Relay_Log_Space: 2699
              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: NULL
    Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
              Last_SQL_Errno: 1146
              Last_SQL_Error: Worker 3 failed executing transaction '59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:17' at master log mysql-bin.000002, end_log_pos 3255; Error executing row event: 'Table 'test.t1' doesn't exist'
    Replicate_Ignore_Server_Ids:
            Master_Server_Id: 1
                  Master_UUID: 59fe7a3e-9dd6-11e7-9d6c-000c29e57c69
            Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
          Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
    Last_SQL_Error_Timestamp: 170922 00:30:21
              Master_SSL_Crl:
          Master_SSL_Crlpath:
          Retrieved_Gtid_Set: 59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-17
            Executed_Gtid_Set: 29bd8c99-9e4d-11e7-a072-000c29d00b2d:1,
    59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-16
                Auto_Position: 1
    1 row in set (0.00 sec)
    

通過查看我們得知:
從庫在執行59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:17 這個事務時,因為從庫沒有這個表而報錯了。

2. 解決思路:

  1. root用戶手動重建test.t1表
  2. root用戶手動在59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:17 這個事務上執行一個空事務

3. 解決步驟:

  • 在從庫上重建 test.t1 表

    主庫備份
    # mysqldump -uroot -p123123  -h127.0.0.1  --single-transaction --set-gtid-purged=off  --triggers --routines --events  test t1 >/tmp/t1.sql
    從庫恢複
    # cat t1.sql |mysql -uroot -p123123 test
    
  • 從庫在衝突事務號執行空事務

    > stop slave;
    > SET @@SESSION.GTID_NEXT= '59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:17'/*!*/;
    > show variables like '%gtid%_next';
    > BEGIN;COMMIT;
    > SET gtid_next = 'AUTOMATIC';
    > START SLAVE;
    

3、批量跳過

1. 情景模擬

由於數據不一致嚴重,跳過單個事務不能繼續正常複製。
從庫刪掉單表,然後跳過批量sql,繼續複製

  • 從:

    mysql> truncate table test.t1;
    
  • 主:

    mysql>
    mysql> begin;
    mysql> update test.t1 set age=11 where id=1;        //如果隻跳過這個事務,下個事務還是會出錯
    mysql> commit;
    mysql> begin;
    mysql> update test.t1 set age=33 where id=3;
    mysql> commit;
    

2. 操作步驟

  • 主庫備份表
    set-gtid-purged ==> SQL_LOG_BIN= 0、SET @@GLOBAL.GTID_PURGED

    # mysqldump -uroot -p123123  -h127.0.0.1  --single-transaction --set-gtid-purged=on  --triggers --routines --events  test t1 >/tmp/t1.sql
    # cat /tmp/t1.sql |egrep SET |egrep -v "^/"
    SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
    SET @@SESSION.SQL_LOG_BIN= 0;
    SET @@GLOBAL.GTID_PURGED='59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-22';
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
    
  • 從庫恢複

    > reset master;        //清空GTID_EXECUTED 
    # cat t1.sql |mysql -uroot -p123123 test
    會執行備份文件中的SET @@GLOBAL.GTID_PURGED。 @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
    > show slave status \G
          Retrieved_Gtid_Set: 59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-22
          Executed_Gtid_Set: 59fe7a3e-9dd6-11e7-9d6c-000c29e57c69:1-22
    > start slave;
    

最後更新:2017-09-27 09:33:09

  上一篇:go  2017杭州·雲棲大會---大數據workshop:《雲數據·大計算:海量日誌數據分析與應用》之《數據采集:日誌數據上傳》篇
  下一篇:go  喜訊!RocketMQ成為Apache軟件基金會頂級開源項目