pt-table-sync 使用方法【轉】


28. pt-table-sync
28.1 pt-table-sync 作用

使用對兩個庫不一致的數據進行同步,他能夠自動發現兩個實例間不一致的數據,然后進行sync操作,pt-table-sync無法同步表結構,和索引等對象,只能同步數據。

可能大家多數使用該工具來解決主從數據不一致的問題,其實他的功能不止於此,也可以用來對兩個不在一個主從拓撲實例,進行數據sync。

NOTE1:如果是sync主從數據,只有當需要sync的表都有唯一鍵(主鍵或唯一索引),才能使用--sync-to-master and/or --replicate。(沒有唯一鍵,則只能在desitination上直接修改,而指定--sync-to-master and/or –replicate時只能在主庫上修改),如果sync主從時沒有指定--replicate或者--sync-to-master則所有修改都在從庫上執行(不論表上是否有唯一鍵)
28.2 常用參數

 常用參數請參考:pt-table-sync 中文使用說明
http://blog.csdn.net/shaochenshuo/article/details/53285439
28.3 使用示例
28.3.1  sync兩個獨立數據庫(非主從)

1)以75為source,76為desitination,同步所有的庫和表

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 --execute --print

 


2) 同步指定庫或者指定表

只對指定的庫進行sync

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona --databases=test1 --no-check-slave dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 --execute --print

 


只對指定的表進行sync(也可以--tables=test5.test_nu)

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona --databases=test5 --tables=test_nu --no-check-slave dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 --execute --print

 


 

1)       忽略某些庫或者忽略某些表

忽略庫

--ignore-databases=指定要忽略的庫

 


 

忽略表

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona --ignore-tables=test5.test_nu --no-check-slave dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 --execute --print

 


 
28.3.2  sync主從數據

1)沒有唯一鍵(主鍵或唯一索引)

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona --no-check-slave dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 --execute --print

 


##只比對76同75的數據,對差異數據進行sync

可以指定多個從庫

pt-table-sync --charset=utf8--ignore-databases=mysql,sys,percona --no-check-slave dsn=u=root,p=root,h=172.172.178.75,P=3306 dsn=u=root,p=root,h=172.172.178.76,P=3306 dsn=u=root,p=root,h=172.172.178.77,P=3306 --execute –print

 


##比對76同75;77同75的數據,對差異進行sync

 

NOTE1:在執行sync之前,我們可以指定—print(不要指定--execute)來查看pt-table-sync會進行哪些修改

NOTE2:執行sync操作時,指定忽略mysql等系統數據庫(information_schema,performance_schema會自動被忽略)

NOTE3:指定--no-check-slave不檢查desitination是否為從庫,否則報如下錯誤:

Can't make changes onA=utf8,P=3306,h=172.172.178.76,p=... because it's a slave. See thedocumentation section 'REPLICATION SAFETY'

NOTE4:此種情況下修改都在從庫上進行(不論表是否有唯一鍵)

 

2) 有唯一鍵(有唯一鍵時,可以使用--sync-to-master and/or –replicate進行主從sync)

pt-table-sync --execute --sync-to-master--charset=utf8 --ignore-databases=mysql,sys,percona --no-check-slave dsn=u=root,p=root,h=172.172.178.76,P=3306 --print

 

NOTE1:在執行sync之前,我們可以指定--print來查看pt-table-sync會進行哪些修改

NOTE2:執行sync操作時,指定忽略mysql等系統數據庫(information_schema,performance_schema會自動被忽略)

NOTE3:如果表上沒有唯一索引,則無法在主庫執行replace操作,會報如下錯此種情況不能指定--replicate或者--sync-to-master

 Can'tmake changes on the master because no unique index exists at/usr/bin/pt-table-sync line 10663.  whiledoing test1.test_concat on 172.172.178.76

NOTE4:指定了--replicate(指定dsn為主)或者--sync-to-master(指定dsn為從)只能為命令行指定一個dsn



相關鏈接:

pt-table-sync 中文使用說明
http://blog.csdn.net/shaochenshuo/article/details/53285439


pt-table-checksum 中文使用說明
http://blog.csdn.net/shaochenshuo/article/details/53098224


pt-table-checksum 使用方法
http://blog.csdn.net/shaochenshuo/article/details/56009092
---------------------

轉自
作者:database_shaofei
來源:CSDN
原文:https://blog.csdn.net/shaochenshuo/article/details/56009234
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 

鎖表參數

--lock

          type: int

 

          Lock tables: 0=none, 1=per sync cycle, 2=per table, or 3=globally.

 

          This uses "LOCK TABLES". This can help prevent tables being changed while you’re examiningthem.

                  是否鎖表:0不鎖;

1,只鎖檢查的塊,檢查完畢解鎖,為下個塊加鎖

                                   2,  加鎖和解鎖每單個要檢查的表

                                   3,  使用命令FLUSH TABLES WITH READ LOCK進行全局讀鎖定;
---------------------
作者:老王筆記
來源:CSDN
原文:https://blog.csdn.net/JSWANGCHANG/article/details/79501452
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

pt-table-sync筆記 - 默默前行 - CSDN博客 https://blog.csdn.net/JSWANGCHANG/article/details/79501452?utm_source=blogxgwz6


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM