利用pt-table-checksum 檢查主從的一致性,pt-table-sync實現主從數據一致性修復html
一.percona-toolkit的下載安裝:
須要先安裝其它依賴環境包...
shell> perl -MCPAN -e 'install DBI'
shell> perl -MCPAN -e 'install DBD::mysql'
shell> perl -MCPAN -e 'install Term::ReadKey'
順便說一下,我在安裝某些Perl模塊的時候,出現相似下面的錯誤提示:
Can’t locate object method 「install」 via package 「…」
若是你也遇到了相似的問題,能夠進入到Perl命令行安裝:
yum install YAML
shell> perl -MCPAN -e shell
cpan> install ...mysql
下載地址:wget http://www.percona.com/downloads/percona-toolkit/2.2.14/tarball/percona-toolkit-2.2.14.tar.gz
安裝方法:perl Makefile.PL;make;make installsql
二.在master上作受權操做:(IP爲主庫地址)
mysql> grant select ,process,super,replication slave on *.* to 'user'@'192.168.9.140' identified by 'userpw';
mysql> flush privileges;
注:select:查看全部庫的表;process:執行show processlist ;super:設置binlog_format=‘statement’
replication slave:show slavehosts
數據庫主從結構:
主:192.168.9.140 (binlog_format=mixed)
從:192.168.9.142 (binlog_format=mixed)shell
三.主從庫執行數據一致檢查
[root@DB1:/]# pt-table-checksum h='192.168.9.140',u='user',p='userpw',P=3306 --databases db1 --tables t1 --max-load="Threads_running=25" --nocheck-replication-filters --create-replicate-table --replicate=test.checksums --no-check-binlog-format --no-check-slave-tables
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
05-13T15:42:58 0 1 8 1 0 0.030 db1.t1
查詢結果顯示 diffs =1 表示db1.t1表主從庫數據存在異常.數據庫
執行結果顯示參數意義:
TS :完成檢查的時間.
ERRORS :檢查時候發生錯誤和警告的數量.
DIFFS :0表示一致,1表示不一致.當指定--no-replicate-check時,會一直爲0,當指定--replicate-check-only會顯示不一樣的信息.
ROWS :表的行數.
CHUNKS :被劃分到表中的塊的數目.
SKIPPED :因爲錯誤或警告或過大,則跳過塊的數目.
TIME :執行的時間.
TABLE :被檢查的表名服務器
參數詳解:
[root@DB1:/]# pt-table-checksum --help 查看全部的參數信息.
h='192.168.9.140',u='user',p='userpw',P=3306 鏈接主數據庫的IP,用戶名與密碼,端口.
--create-replicate-table:這個參數只在第一次運行時添加就能夠,用於創建checksums表;後續不須要添加此參數,若是添加了會從新建立checksums表.
--nocheck-replication-filters:不檢查複製過濾器(即參數文件裏設置的repliacte-do-waild-table等規則)
--no-check-binlog-format :不檢查複製的binlog格式(這個參數在,binlog_format=row時,必定要加,否則報錯)
--replicate=kz.checksums:把checksum的信息寫入指定庫,指定表,建議直接寫到被檢查的庫裏.
--ignore-tables=mysql.user:對某個表忽略檢查
--tables 指定被檢查的表名;能夠多個表,按逗號分開. 例如:--tables t1,t2
----databases 指定被檢查的庫名,若是沒有此參數將檢查全部庫;也能夠指定多個庫名,例如:--databases db1,db2ide
更多參數使用方法:
--recursion-method processlist,hostsspa
要是在執行命令的過程遇到找不到從服務器的錯誤:
Diffs cannot be detected because no slaves were found. Please read the --recursion-method documentation for information.
上面的提示信息很清楚,由於找不到從,因此執行失敗.用參數--recursion-method 能夠指定模式解決,關於--recursion-method參數的設置有:命令行
METHOD USES
=========== =============================================
processlist SHOW PROCESSLIST
hosts SHOW SLAVE HOSTS
cluster SHOW STATUS LIKE 'wsrep\_incoming\_addresses'
dsn=DSN DSNs from a table
none Do not find slavesorm
默認是經過show processlist 找到host的值;當--recursion-method=hosts 時會取show slave hosts 找到host的值.
使用--recursion-method=hosts 參數也同步能夠解決,因從庫比較多,而是跨機房從庫檢查慢的問題.可指定其中一個從庫進行檢查.
使用解決方法:
在從庫的配置文件里加: report_host = 192.168.9.142 #設置成從庫本機IP地址,並重啓數據庫
而後在主庫上執行 show slave hosts
>show slave hosts;
+-----------+----------------+------+----------+------+-------------------+-----------+
| Server_id | Host | User | Password | Port | Rpl_recovery_rank | Master_id |
+-----------+----------------+------+----------+------+-------------------+-----------+
| 2 | 192.168.9.142 | | | 3306 | 0 | 140|
+-----------+----------------+------+----------+------+-------------------+-----------+
1 row in set (0.00 sec)
最後再執行以上命令(多加--recursion-method=hosts 參數)
pt-table-checksum --recursion-method=hosts h='192.168.9.140',u='user',p='userpw',P=3306 --databases db1 --tables t1 --max-load="Threads_running=25" --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format --no-check-slave-tables
四.對主從數據不一致性進行修復
語法結構: pt-table-sync [OPTIONS] DSN [DSN]
pt-table-sync: 高效的同步MySQL表之間的數據,他能夠作單向和雙向同步的表數據.他能夠同步單個表,也能夠同步整個庫.它不一樣步表結構、索引、或任何其餘模式對象.因此在修復一致性以前須要保證他們表存在.
pt-table-sync --help 查看詳細參數.
1.根據pt-table-checksum 檢查的結果數據庫進行合併. 主要指定--replicate=test.checksums
2.直接進行表數據合併,從主庫上面同步到從庫. (從庫不存在主庫上面的表)
推薦根據pt-table-checksum 檢查的結果數據庫進行合併.
接着上面的複製狀況,主和從的test1數據不一致,須要修復,要是有中文的則須要加上:--charset=utf8,防止亂碼.
1>指定庫,表進行數據合併:
pt-table-sync --recursion-method=hosts --replicate test.checksums --databases=db1 --tables=t1 --sync-to-master h=192.168.9.142,P=3306,u=user,p=userpw --charset=utf8 --print; //打印主從存在異常的數據
pt-table-sync --recursion-method=hosts --replicate test.checksums --databases=db1 --tables=t1 --sync-to-master h=192.168.9.142,P=3306,u=user,p=userpw --charset=utf8 --execute; //執行數據一致性同步
2> 只根據test.checksums的結果進行數據合併.
pt-table-sync --replicate test.checksums --sync-to-master h=192.168.9.142,P=3306,u=user,p=userpw --charset=utf8 --print; //打印主從存在異常的數據
pt-table-sync --replicate test.checksums --sync-to-master h=192.168.9.142,P=3306,u=user,p=userpw --charset=utf8 --execute;//執行數據一致性同步
pt-table-sync --replicate=test.checksums h=192.168.9.140 --user=user --password=userpw h=192.168.9.142,u=user,p='userpw' --charset=utf8 --print //打印主從存在異常的數據
pt-table-sync --replicate=test.checksums h=192.168.9.140 --user=user --password=userpw h=192.168.9.142,u=user,p='userpw' --charset=utf8 --excute //執行數據一致性同步
將主的test數據庫同步到192.168.9.142,使從上具備同樣的數據
pt-table-sync --execute --sync-to-master --user=user --password=userpw h=192.168.9.142 --database test
只同步指定的表(aaa 表)
pt-table-sync --execute --sync-to-master --user=user --password=userpw h=192.168.9.142 D=test,t=aaa
參數的解釋:
--replicate= :指定經過pt-table-checksum獲得的表.
--databases= : 指定執行同步的數據庫,多個庫用逗號隔開.
--tables= :指定執行同步的表,多個表用逗號隔開.
--sync-to-master :指定一個DSN,即從的IP,他會經過show processlist或show slave status 去自動的找主.
h=127.0.0.1 :服務器地址,命令裏有2個ip,第一次出現的是M的地址,第2次是Slave的地址.
u=root :賬號.
p=123456 :密碼.
--print :打印,但不執行命令.
--execute :執行命令.
更多的參數請見官網https://www.percona.com/doc/percona-toolkit/2.2/pt-table-checksum.htmlhttps://www.percona.com/doc/percona-toolkit/2.2/