使用mktablesync总结

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....

原文地址:https://www.cnblogs.com/sunss/p/1992546.html