mk-table-sync是maatkit裡的一個同步主從資料庫的利器,執行同步過程中,會同步DELETE,REPLACE,INSERT,UPDATE語句,mk-table-sync把包含前面幾個的語句都執行一遍,舉個例子來說,主從庫上都有:a表,主庫上的資料如下:
id name
1 aa
2 bb
3 cc
4 dd
5 ee
6 ff
從庫上的資料如下:
id name
1 aa
2 bb
3 hh
4 gg
5 ee
那麼mk-table-sync會執行3條語句,同步"6 ff"添加到從庫,更新"3 cc","4 gg"到從庫,一共3條語句,而不是我們看到的從庫只比主庫少1條資料,其實同步過程中,執行了3條;
執行同步操作一般:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t a \
h=localhost,u='root',p='123456' \
h=192.168.0.24,u='root',p='123456'
注意事項:
1.記得加上--charset選項,否則會造成從庫亂碼;
2.我們如果想要看下到底哪些資料不同步,可以這樣做:
mk-table-sync --charset=utf8 --print --no-check-slave -d test -t a \
h='127.0.0.1',u='root',p='123456' \
h='192.168.0.24',u='root',p='123456'>result
3.如果有好幾個從庫的話,建議分開同步,除非幾個從庫的checksum一樣;因為每個從庫同步的步調不一定都一致,如果幾個從庫一塊同步的話,很容易造成主鍵衝突,導致主從同步中斷,舉個例子來說,建議:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t a \
h='127.0.0.1',u='root',p='123456' \
h='192.168.0.24',u='root',p='123456'>result
不建議:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t a \
h='127.0.0.1',u='root',p='123456' \
h='192.168.0.24',u='root',p='123456' \
h='192.168.0.25',u='root',p='123456' \
h='192.168.0.26',u='root',p='123456'>result
4.為了減少重複操作,我們也可以一次同步好幾個表,比如:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t=a,b,c....