利用pt-table-sync進行資料不一致修復

君落塵發表於2015-03-18

【前言】mysql由於軟體本身的靈活性導致了可能主從資料庫已經不一致了,但是資料庫的主從監控卻是正常的,前面已經介紹過pt-table-checksum工具可以檢驗出主從表中不一致的資料。

解決問題的能力比發現問題的能力更重要。本文件將介紹通過pt-table-sync處理主從資料庫不一致的問題;

 

【說明】實驗的環境和上次進行校驗的環境和測試關聯,測試之前請部署好環境;

image

 

【注意事項】pt-table-sync需要在從資料庫上面進行執行

 

【使用說明】使用print引數,他會在螢幕顯示修復的SQL語句。然後可以手工確認並執行。

指令碼【1】  基於checksums資訊對Slave 192.168.47.165同步testdb.john表資料

pt-table-sync --print --replicate=test.checksums --databases=test --tables=john --sync-to-master h=192.168.47.165,u=root,p=123456

percona-toolkit src_db:test src_tbl:john src_dsn:P=3306,h=192.168.47.167,p=...,u=root dst_db:test dst_tbl:john dst_dsn:P=3306,h=192.168.47.165,p=...,u=root lock:1 transaction:1 changing_src:1 replicate:0 bidirectional:0 pid:28054 user:root host:mysql02*/;

 

指令碼【2】 全量同步修復Slave的testdb.john表資料

[root@mysql02 bin]# pt-table-sync --print  --sync-to-master h=192.168.47.165,P=3306,u=root,p=123456 --database test --tables=john

REPLACE INTO `test`.`john`(`id`) VALUES ('5') /*

percona-toolkit src_db:test src_tbl:john src_dsn:P=3306,h=192.168.47.167,p=...,u=root dst_db:test dst_tbl:john dst_dsn:P=3306,h=192.168.47.165,p=...,u=root lock:1 transaction:1 changing_src:1 replicate:0 bidirectional:0 pid:28054 user:root host:mysql02*/;

 

指令碼【3】直接同步主從資料庫test

[root@mysql02 bin]# pt-table-sync --print  --sync-to-master h=192.168.47.165,P=3306,u=root,p=123456 --database test


REPLACE INTO `test`.`checksums`(`db`, `tbl`, `chunk`, `chunk_time`, `chunk_index`, `lower_boundary`, `upper_boundary`, `this_crc`, `this_cnt`, `master_crc`, `master_cnt`, `ts`) VALUES ('test', 'john', '1', 0.004643, NULL, NULL, NULL, '42981178', '5', '42981178', '5', '2015-03-10 17:57:56') /*percona-toolkit src_db:test src_tbl:checksums src_dsn:P=3306,h=192.168.47.167,p=...,u=root dst_db:test dst_tbl:checksums dst_dsn:P=3306,h=192.168.47.165,p=...,u=root lock:1 transaction:1 changing_src:1 replicate:0 bidirectional:0 pid:28166 user:root host:mysql02*/;

 

以上的指令碼中可以把--print替換成—execute,直接進行主從的資料庫修復;

 

【注】用pt-table-sync修復的時候,必須保證被replace的表上有主鍵或唯一鍵,否則就會報以下的錯誤

Can't make changes on the master because no unique index exists at /usr/bin/pt-table-sync line 10655.  while doing test.john on 192.168.47.165

 

總結:pt-table-checksum 和 pt-table-sync 一起構成了mysql主從資料一致監控的兩把利器,pt-table-sync在進行--execute的時候需要小心,一般情況都是進行print相應的sql語句,然後再到從資料庫執行。

........................................................................................................................................................................

本文作者:JOHN,某上市公司DBA,業餘時間專注於資料庫的技術管理,從管理的角度去運用技術。

ORACLE技術部落格:ORACLE 獵人筆記               資料庫技術群:367875324 (請備註ORACLE管理 ) 

........................................................................................................................................................................

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29819001/viewspace-1464124/,如需轉載,請註明出處,否則將追究法律責任。

相關文章