天天看點

MySQL資料庫主從資料對比及不一緻資料修複

作者:資料庫幹貨鋪

使用percona-Toolkit進行資料對比,是以先進行此工具的安裝

官方位址:

https://www.percona.com/downloads/percona-toolkit

安裝依賴包

yum install -y perl-DBI  perl-DBD-MySQL   perl-Time-HiRes  perl-IO-Socket-SSL perl-TermReadKey           

安裝

rpm  -ivh  percona-toolkit-3.0.4-1.el6.x86_64.rpm           

2.資料對比

pt-table-checksum  --host=127.0.0.1--port 3306 --databases=test1 -uroot -ppass   --no-check-binlog-format           

過程如下:

[root@mysql1 ~]# pt-table-checksum  --host=127.0.0.1 --port 3346 --databases=test1 -uroot -ppass   --no-check-binlog-format
Diffs cannot be detected because no slaves were found.  Please read the --recursion-method documentation for information.
# A software update is available:
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-19T00:05:20      0      0     9759       4       0   0.742 test1.COST_RISKFREE
08-19T00:05:20      0      0    17862       1       0   0.349 test1.FFUT_QUOTATION
08-19T00:05:27      0      0   461012      12       0   6.714 test1.STK_MKT_QUOTATION
08-19T00:05:27      0      0       20       1       0   0.177 test1.e
08-19T00:05:27      0      0        1       1       0   0.171 test1.heartbeat
08-19T00:05:28      0      0        1       1       0   0.175 test1.test_int
You have new mail in /var/spool/mail/root           

簡介:

TS            :完成檢查的時間。
ERRORS        :檢查時候發生錯誤和警告的數量。
DIFFS         :0表示一緻,1表示不一緻。當指定--no-replicate-check時,會一直為0,當指定--replicate-check-only會顯示不同的資訊。
ROWS          :表的行數。
CHUNKS        :被劃分到表中的塊的數目。
SKIPPED       :由于錯誤或警告或過大,則跳過塊的數目。
TIME          :執行的時間。
TABLE         :被檢查的表名           

如出現DIFFS不為0 的情況,即對應表主從資料不一緻

3.資料修複

修複時使用pt-table-sync工具

pt-table-sync --execute  --databases=test1  --tables=COST_RISKFREE   --replicate percona.checksums --sync-to-master  h=106.14.184.46,u=root,p=123456
# TableChecksum:4998 5214 SELECT db, tbl, CONCAT(db, '.', tbl) AS `table`, chunk, chunk_index, lower_boundary, upper_boundary, COALESCE(this_cnt-master_cnt, 0) AS cnt_diff, COALESCE(this_crc <> master_crc OR ISNULL(master_crc) <> ISNULL(this_crc), 0) AS crc_diff, this_cnt, master_cnt, this_crc, master_crc FROM percona.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)
# TableChecksum:4998 5214 SELECT db, tbl, CONCAT(db, '.', tbl) AS `table`, chunk, chunk_index, lower_boundary, upper_boundary, COALESCE(this_cnt-master_cnt, 0) AS cnt_diff, COALESCE(this_crc <> master_crc OR ISNULL(master_crc) <> ISNULL(this_crc), 0) AS crc_diff, this_cnt, master_cnt, this_crc, master_crc FROM percona.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)
# SchemaIterator:7923 5214 Table heartbeat is not in --tables list, ignoring
# pt_table_sync:10262 5214 No checksum differences
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x2c229e0)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c18e0)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c1e80)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c1610)           

繼續閱讀