yanchuen 发表于 2018-10-3 07:10:53

MySQL FAQ系列 pt-table-checksum

  背景描述:
  因为主从复制导致主从数据不一致的问题,所以使用pt-table-checksum工具来进行主从数据的一致性校验,以下是使用工具时的流程、遇到的问题及解决方法,以期最大限度的模拟线上环境
  环境:
  backup为master主机(192.168.32.3),mydb为slave主机(192.168.32.2);
  backup为3316端口,mydb为3306端口;
  binlog_format = ROW
  pt-table-checksum 3.0.10 版本(percona-toolkit请自行安装)
  准备工作:
  1、安装依赖包:
  #yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL perl-IO-Socket-SSL
  2、创建用户:
  创建一个对bakup、mydb主机都有权限的非root用户,来进行数据查看、恢复操作,目的嘛,为了安全呗,注意:这个用户一定要相同,相同,相同!为了控制权限,可以只在各自的主机上授权
  backup主机:

  GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'monitor'@'127.0.0.1'>  mydb主机:

  GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'monitor'@'192.168.32.3'>  工具用法:
  # pt-table-checksum --help
  Usage: pt-table-checksum 这里注意一下:DSN连接的是主库的地址,这里指的是backup的地址
  校验注意点:
  1、被检测的表需要有主键(唯一索引),因为这是pt-table-checksum的工作原理;
  2、从库的IO、SQL进程要为YES状态,因为从库要同步主库的check信息;
  3、执行校验时的DSN为主库地址;
  4、校验时会加S锁;
  5、确保主库、从库使用相同的账号
  执行检验:(backup主机上执行)
  #pt-table-checksum --nocheck-replication-filters--databases=bailidb h=127.0.0.1,u=monitor,p='123456',P=3316 --replicate-check-only
  Checking if all tables can be checksummed ...
  Starting checksum ...
  Cannot connect to P=3306,h=,p=...,u=monitor
  Diffs cannot be detected because no slaves were found.Please read the --recursion-method documentation for information
  报错原因:
  主库连接不到从库,需要配置从库
  report_host=192.168.32.2
  report_port=3306
  当然了,因为report_host、report_port是read only变量,所以,需要重启数据库
  再次执行校验:
  # pt-table-checksum --nocheck-replication-filters--databases=bailidb h=127.0.0.1,u=monitor,p='123456',P=3316 --replicate-check-only
  Checking if all tables can be checksummed ...
  Starting checksum ...
  Replica mydb has binlog_format ROW which could cause pt-table-checksum to break replication.Please read "Replicas using row-based replication" in the LIMITATIONS section of the tool's documentation.If you understand the risks, specify --no-check-binlog-format to disable this check
  报错原因:
  如果是基于行的复制环境(即binlog_format=row),percona官方是不建议使用pt-table-checksum工具来进行数据的一致性检查的,但它又提供了一个选项来跳过此检查,--no-check-binlog-format
  再次执行校验:
  打印出所有的校验信息:(可以保存到文件做后期的处理)
  #pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --databases=bailidb h=127.0.0.1,u=monitor,p='123456',P=3316 >
  只打印有差异数据的校验信息:
  #pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --databases=bailidb h=127.0.0.1,u=monitor,p='123456',P=3316 --replicate-check-only >
  将校验数据写入到数据表中:
  #pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test.checksums --databases=bailidb h=127.0.0.1,u=monitor,p='123456',P=3316 [--recursion-method=hosts]
  --nocheck-replication-filters 不检查复制过滤器,此参数经常和--databases参数配合使用,对单个库进行校验
  --no-check-binlog-format 不检查复制的二进制日志文件格式,如果你的binlog_format=row,则要开启此参数,否则报错
  --replicate=test.checksums 将校验的信息写入到test库的checksums表中,如果这个表没有的话,会自动创建,且master、slave主机都会有
  --databases 指定检验的数据库,多个用 "," 隔开
  --replicate-check-only 只显示有不一致数据的信息
  --recursion-method=hosts 如果使用pt-table-checksum校验的时候,报错信息出现"no slaves were found",需要使用此参数
  h 主库ip
  u 主库、从库共同的那个用户名
  p 主库、从库共同的那个用户密码
  P 主库端口号,这里主库是3316端口
  特别注意:
  --replicate-check-only、--recursion-method=hosts 此类的参数,使用的时候是有顺序的要求的,必须在pt-table-checksum使用的DSN之后!
  # pt-table-checksum --help
  Options and values after processing arguments
  校验结果介绍:
  Checking if all tables can be checksummed ...
  Starting checksum ...
  TS         ERRORS      DIFFS   ROWS    DIFF_ROWSCHUNKS SKIPPED    TIME   TABLE
  06-20T11:58:16          0          0         69            0                   1         0         0.298   bailidb.bl_admin
  06-20T11:58:18          0          1         13            0                   1         0         0.022   bailidb.bl_block
  TS:完成检查的时间
  ERRORS:检查时候发生错误和警告的数量
  DIFFS:最重要的一列,显示校验结果是否一致,0表示一致,1表示不一致
  ROWS:表的行数,这里的指的是master主机的
  CHUNKS:被划分到表中的块的数目
  SKIPPED:由于错误或警告或过大,则跳过块的数目
  TIME:执行校验的时间
  TABLE:被校验的标明
  使用pt-table-sync工具查看差异数据的详细信息:
  #pt-table-sync --replicate=test.checksums h=127.0.0.1,u=monitor,P=3316 --ask-pass h=192.168.32.2,u=monitor,P=3306 --ask-pass -- --print
  Enter password for 127.0.0.1:
  Enter password for 192.168.32.2:
  也可以只查看其中一张表的信息:
  # pt-table-sync --replicate=test.checksums --tables=bl_major h=127.0.0.1,u=monitor,P=3316 --ask-pass h=192.168.32.2,u=monitor,P=3306 --ask-pass --charset=utf8 --print
  Enter password for 127.0.0.1:
  Enter password for 192.168.32.2:
  说明:为了最大限度的保障线上数据库的安全性,所以,我们每做一步都要考虑线上的实际应用环境,尽量做到:能不重启数据库就不重启,能不明文输密码,就不明文,一句话,为了安全,为了安全,为了安全
  执行差异数据同步:
  #pt-table-sync --replicate=test.checksums h=127.0.0.1,u=monitor,P=3316 --ask-pass h=192.168.32.2,u=monitor,P=3306 --ask-pass --execute
  # pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --databases=bailitop h=127.0.0.1,u=monitor,P=3316 --ask-pass
  Can't locate Term/ReadKey.pm
  yum -y installperl-TermReadKey

页: [1]
查看完整版本: MySQL FAQ系列 pt-table-checksum