溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql中如何使用pt-table-checksum和pt-table-sync

發(fā)布時間:2021-11-06 11:42:27 來源:億速云 閱讀:102 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章給大家分享的是有關(guān)mysql中如何使用pt-table-checksum和pt-table-sync的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

pt-table-checksum和pt-table-sync是percona-toolkit工具中的功能,用來檢測主從數(shù)據(jù)一致性和修復(fù)主從不一致。

下面通過一組實驗學(xué)習(xí)這兩個功能的使用

1、主庫創(chuàng)建表并插入數(shù)據(jù)

CREATE TABLE `NewTable` (

`id`  int(8) NULL ,

`name`  varchar(32) NULL ,

PRIMARY KEY (`id`)

) ;

insert into newtable values(1,'leo');

insert into newtable values(2,'mike');

insert into newtable values(3,'jack');

2、從庫執(zhí)行下面語句,讓主從不一致

insert into newtable values(4,'lucy');

insert into newtable values(5,'petter');

update newtable set name='john' where id =1

3、測試數(shù)據(jù)如下

主庫:

mysql> select * from test.newtable;

+----+------+

| id | name |

+----+------+

|  1 | leo  |

|  2 | mike |

|  3 | jack |

+----+------+

3 rows in set (0.00 sec)

從庫:

mysql> select * from test.newtable;

+----+--------+

| id | name   |

+----+--------+

|  1 | john   |

|  2 | mike   |

|  3 | jack   |

|  4 | lucy   |

|  5 | petter |

+----+--------+

5 rows in set (0.00 sec)

4、pt-table-checksum參數(shù)介紹、使用

--port=              主庫端口

--host=              主庫IP

--databases=      校驗的數(shù)據(jù)庫

--tables=            校驗 的表名,只指定數(shù)據(jù)庫不指定表名,校驗數(shù)據(jù)庫下所有表

--user=               用戶名(該用戶在從庫上也要有,同時需要權(quán)限)

--password=       用戶密碼

--replicate           指定checksum存儲的庫和表

--no-check-binlog-format      忽略binlog的格式,如果binlog是ROW或者M(jìn)IXED不加這個參數(shù)都會報錯

--no-check-replication-filters  忽略復(fù)制過濾,如binlog_ignore_db、slave-skip-errors等選項

--help                  更多更詳細(xì)的參數(shù)請見--help幫助文檔

校驗:

[root@trcloud ~]# pt-table-checksum --port=3306 --host=192.168.129.15 --databases=test --tables=newtable --user=root --password='123456' --replicate=test.check --no-check-binlog-format --no-check-replication-filters

            TS          ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME    TABLE

12-13T13:44:07      0           1           3             1           0            0.059  test.newtable

注:檢查的時候會向表加S鎖

TS:         完成的檢查時間

ERRORS:檢查時候發(fā)生的錯誤和告警數(shù)量

DIFFS:       0表示一致,1表示不一致

ROWS:    表行數(shù)

CHUNKS:被劃分到表中的塊數(shù)量

SKIPPED: 由于錯誤跳過的數(shù)目

TIME:       執(zhí)行時間

TABLE:      表名

5、使用pt-table-sync修復(fù)不一致

--replicate=   指定pt-table-checksum得到的表

h=                 主庫IP

u=                  用戶名

p=                 密碼

--execute       執(zhí)行修復(fù)

--print            打印出sql語句

--help            更多更詳細(xì)的參數(shù)請見--help幫助文檔

[root@trcloud ~]# pt-table-sync --replicate=test.check h=192.168.129.15,u=root,p='123456' --execute --print

DELETE FROM `test`.`newtable` WHERE `id`='4' LIMIT 1 /*percona-toolkit src_db:test src_tbl:newtable src_dsn:h=192.168.129.15,p=...,u=root dst_db:test dst_tbl:newtable dst_dsn:h=172.30.249.5,p=...,u=root lock:1 transaction:1 changing_src:test.check replicate:test.check bidirectional:0 pid:19211 user:root host:trcloud*/;

DELETE FROM `test`.`newtable` WHERE `id`='5' LIMIT 1 /*percona-toolkit src_db:test src_tbl:newtable src_dsn:h=192.168.129.15,p=...,u=root dst_db:test dst_tbl:newtable dst_dsn:h=172.30.249.5,p=...,u=root lock:1 transaction:1 changing_src:test.check replicate:test.check bidirectional:0 pid:19211 user:root host:trcloud*/;

REPLACE INTO `test`.`newtable`(`id`, `name`) VALUES ('1', 'leo') /*percona-toolkit src_db:test src_tbl:newtable src_dsn:h=192.168.129.15,p=...,u=root dst_db:test dst_tbl:newtable dst_dsn:h=172.30.249.5,p=...,u=root lock:1 transaction:1 changing_src:test.check replicate:test.check bidirectional:0 pid:19211 user:root host:trcloud*/;

從打印的sql來看,如果從庫數(shù)據(jù)多了使用delete刪除,其他情況修復(fù)主從不一致使用的是REPLACE INTO。所以表上一定要有主鍵,不然不僅會報錯,也會導(dǎo)致主從同步失敗(因為在從庫執(zhí)行REPLACE INTO執(zhí)行不過去)

6、檢查主從一致

[root@trcloud ~]# pt-table-checksum --port=3306 --host=192.168.129.15 --databases=test --tables=newtable --user=root --password='123456' --replicate=test.check --no-check-binlog-format --no-check-replication-filters

            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE

12-13T13:48:18      0      0        3       1       0   0.108 test.newtable
數(shù)據(jù)已經(jīng)正常

感謝各位的閱讀!關(guān)于“mysql中如何使用pt-table-checksum和pt-table-sync”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI