Percona-Toolkit系列(30) --- pt-table-sync

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章