Preface
 
    The master-slave replication is commonly used in our product evironment.On account of network lag or replicaton mode,slaves probablly be delayed with master.How to solve the consistency of tables between master and slaves?pt-table-checksum is a suitable tool which you can use efficiently.I'm gonna use the tool to do some tests today.
 
Introduce
 
    pt-table-checksum is a memeber of Percona-Toolkit,it's used to check consistency of tables in online replication environment by execute querries on master(put results into a replica called chckesum table).It will indicate whether there're some inconsistent tables bewteen master and slaves by output on screen with parameter "--print".
 
Procedure
 
1.Comman parameter introduce.
 Connection relevent:
--ask-pass -- Ask user to input a password when executing.
-h hostname/ip
-u username
-p password(specify the password in command line)
-P port
-S socket
-D database
-t tables Important parameter:
--create-replicate-table -- Create database and table(checksum table) mentioned in "--replicate".
--no-check-binlog-format -- Don't check binlog format on all server.
--recursion-method -- Specify the prefferd mode to find slaves if you've got multiple slaves.
--replicate -- Specify the table(default is "percona.checksums") into which the results will be write.
--replicate-check-only -- Check consistency on replica without executing checksum queries on master.Furthermore,it only checks riplica for differences found by previous checksuming. Output relevent:
--explain -- Show without really execute checksum querries.
--progress -- Print progress report(default seconds).
--quite -- Print only important informations on screen(will disable "--progress"). Safety relevent:
--no-check-slave-tables -- Only if you're confirmed that all tables on slave is the same with master's.Then you can set it to avoid breaking of replication when executing pt-table-checksum.
--check-replication-filters -- Don't checksum if any replication filters are set on target replica.
--chunk-size-limit -- Limit the chunk size to avoid performance issues.

2.Examples.

 Master:
(root@localhost mysql3306.sock)[zlm]::>show tables;
+----------------+
| Tables_in_zlm |
+----------------+
| test_ddl |
| test_ddl_no_pk |
| test_innodb |
| test_myisam |
+----------------+
rows in set (0.00 sec) Slave:
(root@localhost mysql3306.sock)[zlm]::>show tables;
+----------------+
| Tables_in_zlm |
+----------------+
| t1 |
| t2 |
| t3 |
| test_ddl |
| test_ddl_no_pk |
| test_innodb |
| test_myisam |
+----------------+
rows in set (0.00 sec) ###My pt-table-checksum is installed on master server.Execute the command below on master.### [root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --check-slave-tables -hzlm2 -P3306 -uroot --ask-pass
Enter MySQL password:
-21T10:: DBI connect(';host=zlm2;port=3306;mysql_read_default_group=client','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at /usr/bin/pt-table-checksum line . [root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --check-slave-tables -hlocalhost -P3306 -uroot --ask-pass
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
Cannot connect to P=,h=zlm3,p=...,u=root -- It's due to the "root" user cannot login with TCP/IP mode.
Diffs cannot be detected because no slaves were found. Please read the --recursion-method documentation for information.
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /usr/bin/pt-table-checksum line .
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /usr/bin/pt-table-checksum line . # A software update is available:
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
-21T10:: 0.006 mysql.columns_priv
-21T10:: 0.012 mysql.db
-21T10:: 0.010 mysql.engine_cost
-21T10:: 0.008 mysql.event
-21T10:: 0.009 mysql.func
-21T10:: 0.008 mysql.help_category
-21T10:: 0.010 mysql.help_keyword
-21T10:: 0.011 mysql.help_relation
-21T10:: 0.032 mysql.help_topic
-21T10:: 0.008 mysql.ndb_binlog_index
-21T10:: 0.011 mysql.plugin
-21T10:: 0.012 mysql.proc
-21T10:: 0.009 mysql.procs_priv
-21T10:: 0.010 mysql.proxies_priv
-21T10:: 0.009 mysql.server_cost
-21T10:: 0.010 mysql.servers
-21T10:: 0.010 mysql.tables_priv
-21T10:: 0.009 mysql.time_zone
-21T10:: 0.009 mysql.time_zone_leap_second
-21T10:: 0.009 mysql.time_zone_name
-21T10:: 0.009 mysql.time_zone_transition
-21T10:: 0.008 mysql.time_zone_transition_type
-21T10:: 0.007 mysql.user
-21T10:: 0.011 sys.sys_config
-21T10:: 0.008 zlm.test_ddl
-21T10:: 0.008 zlm.test_ddl_no_pk
-21T10:: 0.009 zlm.test_innodb
-21T10:: 0.009 zlm.test_myisam ###See the detail of checksum table.###
(root@localhost mysql3306.sock)[zlm]::>show tables;
+----------------+
| Tables_in_zlm |
+----------------+
| checksums | -- the table checksums was created.
| test_ddl |
| test_ddl_no_pk |
| test_innodb |
| test_myisam |
+----------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]::>show create table checksums\G
*************************** . row ***************************
Table: checksums
Create Table: CREATE TABLE `checksums` (
`db` char() NOT NULL,
`tbl` char() NOT NULL,
`chunk` int() NOT NULL,
`chunk_time` float DEFAULT NULL,
`chunk_index` varchar() DEFAULT NULL,
`lower_boundary` text,
`upper_boundary` text,
`this_crc` char() NOT NULL,
`this_cnt` int() NOT NULL,
`master_crc` char() DEFAULT NULL,
`master_cnt` int() DEFAULT NULL,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`db`,`tbl`,`chunk`),
KEY `ts_db_tbl` (`ts`,`db`,`tbl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
row in set (0.00 sec) ###Check slave hosts information.###
(root@localhost mysql3306.sock)[zlm]::>show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| | | | | 5c77c31b-4add-11e8-81e2-080027de0e0e |
+-----------+------+------+-----------+--------------------------------------+
row in set (0.00 sec) ###Change another user 'repl'@'192.168.1.%' and grant all privileges to it.###
(root@localhost mysql3306.sock)[zlm]::>grant all privileges on *.* to 'repl'@'192.168.1.%';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[zlm]::>show grants for 'repl'@'192.168.1.%';
+-----------------------------------------------------+
| Grants for repl@192.168..% |
+-----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.1.%' |
+-----------------------------------------------------+
row in set (0.00 sec) ###Execute pt-table-checksum again.###
[root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --check-slave-tables -h192.168.1. -P3306 -urepl --ask-pass
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /usr/bin/pt-table-checksum line .
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /usr/bin/pt-table-checksum line . # A software update is available:
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
-21T11:: 0.018 mysql.columns_priv
-21T11:: 0.020 mysql.db
-21T11:: 0.016 mysql.engine_cost
-21T11:: 0.017 mysql.event
-21T11:: 0.014 mysql.func
-21T11:: 0.018 mysql.help_category
-21T11:: 0.016 mysql.help_keyword
-21T11:: 0.015 mysql.help_relation
-21T11:: 0.019 mysql.help_topic
-21T11:: 0.013 mysql.ndb_binlog_index
-21T11:: 0.012 mysql.plugin
-21T11:: 0.015 mysql.proc
-21T11:: 0.012 mysql.procs_priv
-21T11:: 0.016 mysql.proxies_priv
-21T11:: 0.015 mysql.server_cost
-21T11:: 0.015 mysql.servers
-21T11:: 0.014 mysql.tables_priv
-21T11:: 0.013 mysql.time_zone
-21T11:: 0.013 mysql.time_zone_leap_second
-21T11:: 0.015 mysql.time_zone_name
-21T11:: 0.015 mysql.time_zone_transition
-21T11:: 0.012 mysql.time_zone_transition_type
-21T11:: 0.013 mysql.user
-21T11:: 0.015 sys.sys_config
-21T11:: 0.014 zlm.test_ddl
-21T11:: 0.015 zlm.test_ddl_no_pk
-21T11:: 0.021 zlm.test_innodb
-21T11:: 0.016 zlm.test_myisam ###Check whether there're records in "checksums" table or not on slave.###
(root@localhost mysql3306.sock)[zlm]::>select * from checksums where this_cnt<>master_cnt;
+-------+------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| db | tbl | chunk | chunk_time | chunk_index | lower_boundary | upper_boundary | this_crc | this_cnt | master_crc | master_cnt | ts |
+-------+------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| mysql | proc | | 0.001277 | NULL | NULL | NULL | 9e5a007c | | 4e0f05d9 | | -- :: |
| mysql | user | | 0.000667 | NULL | NULL | NULL | 7de55b47 | | 587dfc7 | | -- :: |
+-------+------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
rows in set (0.00 sec)
Supplement
 
1.Drop a table "testl_ddl" on slave when master-slave replication is normally running.
 (root@localhost mysql3306.sock)[zlm]::>show tables;
+----------------+
| Tables_in_zlm |
+----------------+
| checksums |
| t1 |
| t2 |
| t3 |
| test_ddl |
| test_ddl_no_pk |
| test_innodb |
| test_myisam |
+----------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]::>drop table test_ddl;
Query OK, rows affected (0.00 sec)

2.Execute pt-table-checksum again.

 [root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --check-slave-tables -h192.168.1. -P3306 -urepl --ask-pass
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
-22T04:: 0.012 mysql.columns_priv
-22T04:: 0.015 mysql.db
-22T04:: 0.013 mysql.engine_cost
-22T04:: 0.016 mysql.event
-22T04:: 0.014 mysql.func
-22T04:: 0.011 mysql.help_category
-22T04:: 0.015 mysql.help_keyword
-22T04:: 0.017 mysql.help_relation
-22T04:: 0.018 mysql.help_topic
-22T04:: 0.014 mysql.ndb_binlog_index
-22T04:: 0.013 mysql.plugin
-22T04:: 0.013 mysql.proc
-22T04:: 0.013 mysql.procs_priv
-22T04:: 0.014 mysql.proxies_priv
-22T04:: 0.012 mysql.server_cost
-22T04:: 0.012 mysql.servers
-22T04:: 0.014 mysql.tables_priv
-22T04:: 0.014 mysql.time_zone
-22T04:: 0.013 mysql.time_zone_leap_second
-22T04:: 0.012 mysql.time_zone_name
-22T04:: 0.014 mysql.time_zone_transition
-22T04:: 0.014 mysql.time_zone_transition_type
-22T04:: 0.015 mysql.user
-22T04:: 0.012 sys.sys_config
-22T04:: Skipping table zlm.test_ddl because it has problems on these replicas:
Table zlm.test_ddl does not exist on replica zlm3
This can break replication. If you understand the risks, specify --no-check-slave-tables to disable this check.
-22T04:: Error checksumming table zlm.test_ddl: Error getting row count estimate of table zlm.test_ddl on replica zlm3: DBD::mysql::db selectrow_hashref failed: Table 'zlm.test_ddl' doesn't exist [for Statement "EXPLAIN SELECT * FROM `zlm`.`test_ddl` WHERE 1=1"] at /usr/bin/pt-table-checksum line 6823. -22T04:: 0.002 zlm.test_ddl
-22T04:: 0.014 zlm.test_ddl_no_pk
-22T04:: 0.014 zlm.test_innodb
-22T04:: 0.014 zlm.test_myisam ###This will always lead to "ERROR 1146" what is shown below.###
Last_Errno:
Last_Error: Error 'Table 'zlm.test_ddl' doesn't exist' on query. Default database: 'zlm'. Query: 'REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT 'zlm', 'test_ddl', '', NULL, NULL, NULL, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `id`, convert(`name` using utf8mb4), `tel`, CONCAT(ISNULL(`tel`)))) AS UNSIGNED)), , )), ) AS crc FROM `zlm`.`test_ddl` /*checksum table*/'
...
Last_SQL_Errno:
Last_SQL_Error: Error 'Table 'zlm.test_ddl' doesn't exist' on query. Default database: 'zlm'. Query: 'REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT 'zlm', 'test_ddl', '', NULL, NULL, NULL, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `id`, convert(`name` using utf8mb4), `tel`, CONCAT(ISNULL(`tel`)))) AS UNSIGNED)), , )), ) AS crc FROM `zlm`.`test_ddl` /*checksum table*/'

3.Use parameter "--no-check-slave-tables" when execute pt-table-checksum.

 [root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --no-check-slave-tables -h192.168.1. -P3306 -urepl --ask-pass
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
-22T04:: 0.017 mysql.columns_priv
-22T04:: 0.015 mysql.db
-22T04:: 0.013 mysql.engine_cost
-22T04:: 0.011 mysql.event
-22T04:: 0.010 mysql.func
-22T04:: 0.011 mysql.help_category
-22T04:: 0.015 mysql.help_keyword
-22T04:: 0.012 mysql.help_relation
-22T04:: 0.015 mysql.help_topic
-22T04:: 0.013 mysql.ndb_binlog_index
-22T04:: 0.012 mysql.plugin
-22T04:: 0.012 mysql.proc
-22T04:: 0.012 mysql.procs_priv
-22T04:: 0.015 mysql.proxies_priv
-22T04:: 0.013 mysql.server_cost
-22T04:: 0.014 mysql.servers
-22T04:: 0.013 mysql.tables_priv
-22T04:: 0.012 mysql.time_zone
-22T04:: 0.013 mysql.time_zone_leap_second
-22T04:: 0.013 mysql.time_zone_name
-22T04:: 0.011 mysql.time_zone_transition
-22T04:: 0.012 mysql.time_zone_transition_type
-22T04:: 0.012 mysql.user
-22T04:: 0.011 sys.sys_config
-22T04:: Error checksumming table zlm.test_ddl: Error getting row count estimate of table zlm.test_ddl on replica zlm3: DBD::mysql::db selectrow_hashref failed: Table 'zlm.test_ddl' doesn't exist [for Statement "EXPLAIN SELECT * FROM `zlm`.`test_ddl` WHERE 1=1"] at /usr/bin/pt-table-checksum line 6823. -22T04:: 0.001 zlm.test_ddl
-22T04:: 0.013 zlm.test_ddl_no_pk
-22T04:: 0.011 zlm.test_innodb
-22T04:: 0.014 zlm.test_myisam ###If slave has already down,then it will wait until you handle the issue and restart slave.###
[root@zlm2 :: ~]
#pt-table-checksum --no-check-binlog-format --replicate=zlm.checksums --no-check-slave-tables -h192.168.1. -P3306 -urepl --ask-pass
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
Replica zlm3 is stopped. Waiting.
Replica zlm3 is stopped. Waiting.
Replica zlm3 is stopped. Waiting.
Summary
  • pt-table-checksum is a light tool with less influence of performance.
  • pt-table-checksum use CRC arlgorism instead of MD5 and SHA1 to reduce consumption of CPU.
  • Parameter "--no-check-binlog-format" is necessary while replications are using "row" binlog format.
  • You can implement pt-table-checksum tool on either master or slave even other third server which is not belongs to replicaitons at all.

Percona-Tookit工具包之pt-table-checksum的更多相关文章

  1. NXP ARM Vector Table CheckSum

    Signature Creator for NXP Cortex-M Devices Algorithm for creating the checksum The reserved Cortex-M ...

  2. DROP TABLE 恢复【一】

    当DROP TABLE指令敲下的时候,你很爽,你有考虑过后果么?如果该表真的没用,你DROP到无所谓,如果还有用的,这时你肯定吓惨了吧,如果你有备份,那么恭喜你,逃过一劫,如果没有备份呢?这时就该绝望 ...

  3. IAR EWARM Checksum Technical Note

    IELFTOOL Checksum - Basic actions EW targets: ARM, RH850, RX, SH, STM8 EW component: General issues ...

  4. Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程

    一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...

  5. pt-table-checksum

    pt-table-checksum是percona公司提供的一个用于在线比对主从数据一致性的工具. 实现原理 将一张大表分成多个chunk,每次针对一个chunk进行校验,同时将校验的结果通过REPL ...

  6. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  7. pt-table-checksum解读

    pt-table-checksum是目前可以说是最好的查看主从一致性的工具 先来个使用例子,有助快速上手使用 在主库执行: mysql>GRANT SELECT, PROCESS, SUPER, ...

  8. mysql数据一致性检查及修复

    percona-toolkit-2.2.20-1.noarchmysql 5.6.29-logmaster:192.168.166.129slave:192.168.166.131 一.创建数据库校验 ...

  9. MySQL 错误集-汇总

    Q&A: MySQl报错之@@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_MODE = ON 导入的时候加入-f参数即可 原因分 ...

随机推荐

  1. mysql七:索引原理与慢查询优化(待完整)

    一.介绍 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引优化应该是对查询性能优 ...

  2. 【数据库】7.0 MySQL入门学习(七)——MySQL基本指令:帮助、清除输入、查询等

    1.0 help == ? 帮助指令,查询某个指令的解释.用法.说明等.详情参考博文: [数据库]6.0 MySQL入门学习(六)——MySQL启动与停止.官方手册.文档查询 https://www. ...

  3. js中的this问题

    this this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上 this的最终指向的是那个调用它的对象(这里其实并不完全对,this的指向有时候会很微妙,得 ...

  4. .NET开源工作流RoadFlow-表单设计-组织机构选择

    组织机构选择即在表单中添加组织机构选择框. 选择范围: 1.发起者部门:只能在发起者同一个部门中选择. 2.处理者部门:只能在当前处理者同一个部门中选择. 3.自定义:自己指定一个选择范围. 选择类型 ...

  5. CSS3中的变形与动画(一)

    CSS3变形篇 旋转rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中 ...

  6. 基本类型int强转short时发生了什么?

    我们知道java中一个int类型占32bits(4字节),一个short占16bits(2字节)强制转换时只取低16位(short类型占的那16位),高16位(去掉低位多出来的那部分)属于溢出不计算, ...

  7. 1977年提出的OSI七层模型

    OSI七层模型:   7应用层            数据用户接口,提供人操作软件的接口 6表示层            数据的表现形式,特定的功能实现,比如数据加密.数据传输的编码等,一般由软件完成 ...

  8. 【NLP_Stanford课堂】最小编辑距离

    一.什么是最小编辑距离 最小编辑距离:是用以衡量两个字符串之间的相似度,是两个字符串之间的最小操作数,即从一个字符转换成另一个字符所需要的操作数,包括插入.删除和置换. 每个操作数的cost: 每个操 ...

  9. Servlet 2.5为cookie配置HTTPOnly属性

    cookie的HTTPOnly属性,主要是用来防止JavaScript来读取cookie,默认情况下,JavaScript可以通过document.cookie来读取cookie,这样是很不安全的.通 ...

  10. JS支持可变参数(任意多个)

    <script type="text/javascript"> function abc(){ //js中有个变量arguments,可以访问所有传入的值 for(va ...