pt-table-sync
pt-table-sync:官方地址
說明
用途:同步MySQL表數據。
語法:
pt-table-sync [OPTIONS] DSN [DSN]
在使用該工具之前,最好先做好備份。在同步主從環境時,通常使用--replicate
或 --sync-to-master
選項,只對主庫進行修改操作,不會對從庫進行任何修改操作。
工具運行的邏輯僞代碼:
if DSN has a t part, sync only that table: --如果DSN指定了`t`,那麼只同步指定的表。
if 1 DSN: --- 如果只有一個DSN,並且指定了`--sync-to-master`,那麼該DSN指的是從庫,連接其主庫同步數據
if --sync-to-master:
The DSN is a slave. Connect to its master and sync.
if more than 1 DSN: --- 如果不止一個DSN,第一個DSN是源庫,依次同步後面每個DSN。
The first DSN is the source. Sync each DSN in turn.
else if --replicate: --- 指定 `--replicate`
if --sync-to-master: --- 如果指定 `--sync-to-master`,那麼DSN指的是從庫,連接其主庫並檢查不同的記錄並修復。
The DSN is a slave. Connect to its master, find records
of differences, and fix.
else: 如果沒有指定 `--sync-to-master`,那麼DSN指的是主庫,查找從庫,並連接其主庫並檢查不同的記錄並修復。
The DSN is the master. Find slaves and connect to each,
find records of differences, and fix.
else: ---其他情況,即DSN沒有指定`t`,也沒有指定`--replicate`
if only 1 DSN and --sync-to-master: -- 只有一個DSN並且指定了`--sync-to-master`,那麼DSN指的是從庫,連接其主庫查找表並同步數據
The DSN is a slave. Connect to its master, find tables and
filter with --databases etc, and sync each table to the master.
else: -- 其他情況,查找表,例如通過`--databases`過濾數據庫等,並將每個DSN同步到第一個DSN。
find tables, filtering with --databases etc, and sync each
DSN to the first.
具體例子
同步單個表
pt-table-sync --execute h=192.168.20.6,u=root,p=Sanshi_408,P=3306,D=lei,t=test h=192.168.20.66,p=123,u=root,P=3306
同步某個數據庫中所有表
將192.168.20.66服務器上的數據庫lei中的所有表數據同步到192.168.20.6中lei數據中.
多個DSN,第一DSN爲源庫,第二個爲被同步數據
pt-table-sync --execute h=192.168.20.66,u=root,p=123,P=3306 h=192.168.20.5,p=Sanshi_408 --databases lei
同步所有表
pt-table-sync --execute h=192.168.20.66,u=root,p=Sanshi_408,P=3306 h=192.168.20.5,p=123
同步從庫,使其數據和主庫保持一致
pt-table-sync --execute --sync-to-master h=192.168.20.6,p=Sanshi_408,u=root,P=3306
解決pt-table-checksum
工具檢查到的數據不一致的表
同步percona.checksums
中記錄的數據不一致的表。該表中的數據是由pt-table-checksum
工具檢測出來的。
pt-table-sync --execute --replicate percona.checksums h=192.168.20.5,p=Sanshi_408,u=root,P=3306
同步從庫,使其數據和主庫保持一致(只同步數據不一致的表)
pt-table-sync --execute --replicate percona.checksums --sync-to-master h=192.168.20.6,p=Sanshi_408,u=root,P=3306
同步主主
複製的集羣
在主主複製的集羣中,將test.students數據同步到master2中
pt-table-sync --execute --sync-to-master h=master2,p=Sanshi_408,u=root,P=3306,D=test,t=students
注:不要在主主複製集羣中,執行下面命令,因爲會直接修改maste2上的數據,而不會將數據複製到master1上。
pt-table-sync --execute h=master1,D=db,t=tbl master2