Preface
 
    Replication delay is a common issue in MySQL replications.Especially in those replications not using semi-synchronous method.Long term delay leads to inconsistency between master and slaves.Therefore,we should regard the replication delay as an important monitor item.To a certain extent,the value of "Seconds_Behind_Master" shows the situation of replication delay,but it's always inaccurate.Even if the value is "0" sometimes,the replication delay remains.
 
Introduce
 
    pt-heartbeat is the very tool can be used to monitor replication delay of MySQL or PostgreSQL.It provides a way to update a master or to monitor a replica.If you do not speicify the connection options,it will retrieve them in your default my.cnf file.
 
Procedure
 
Usage
 pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop
Common parameters
 --check //Specify the check mode to slave delay.It only checks once then exits.
--monitor //Specify the monitor mode to slave delay.
--update //Specify the update mode on master.
--check-read-only //It skips insert operations if the server is read-only status.
--create-table //Create a heartbeat table.
--table //Specify the heartbeat table(default "heartbeat").
--daemonize //Run it as background process.
--dbi-driver //Specify the drive of database(default "mysql",other value is "pg").
--file //Specify a output file when using --monitor.
--log //Print all output to the file when using --daemonize
--master-server-id //Calculate delay from this master server id.
--interval //Specify the frequency to update or check the heartbeat table(default "1.0").
--run-time //Specify the period of time to run(default suffix is "s",if you set 60,it means 60s). Notice:
Specify at least one of --stop, --update, --monitor, or --check.
--update, --monitor, and --check are mutually exclusive.
--daemonize and --check are mutually exclusive.
Examples
 
Execute pt-heartbeat to update the heartbeat of master.
 [root@zlm2 :: ~]
#pt-heartbeat --database=sysbench --create-table --table=hb --daemonize -h192.168.1. -P3306 -uzlm -pzlmzlm --update [root@zlm2 :: ~]
# (zlm@192.168.1.101 )[sysbench]>show tables like 'hb';
+-------------------------+
| Tables_in_sysbench (hb) |
+-------------------------+
| hb |
+-------------------------+
row in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>select * from hb;
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| --19T03::07.005490 | | mysql-bin. | | NULL | NULL |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
row in set (0.00 sec) //The operation above will create a heartbeat table "hb" in database "sysbench" if the target table not exist.
//It will update the table which contains the heartbeat info of master every time you execute with "--update".
Execute pt-heartbeat to check delay of slave.
 [root@zlm2 :: ~]
#pt-heartbeat --database=sysbench -h192.168.1. -P3306 -uzlm -pzlmzlm --check
DBD::mysql::db selectrow_arrayref failed: Table 'sysbench.heartbeat' doesn't exist [for Statement "SHOW CREATE TABLE `sysbench`.`heartbeat`"] at /usr/bin/pt-heartbeat line 5873. [root@zlm2 :: ~]
#pt-heartbeat --database=sysbench --create-table --table=hb -h192.168.1. -P3306 -uzlm -pzlmzlm --check
0.00 (zlm@192.168.1.102 )[sysbench]>show tables like 'hb';
+-------------------------+
| Tables_in_sysbench (hb) |
+-------------------------+
| hb |
+-------------------------+
row in set (0.00 sec) (zlm@192.168.1.102 )[sysbench]>select * from hb;
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| --19T03::53.005890 | | mysql-bin. | | NULL | NULL |
| -- :: | | NULL | NULL | NULL | NULL |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
rows in set (0.00 sec) //The connection option refers to slave.
//It will also create the same heartbeat table on the slave.
//The output will show the delay time.
Execute pt-heartbeat to monitor delay of slave.
 [root@zlm2 :: ~]
#pt-heartbeat --database=sysbench --create-table --table=hb --daemonize --log=/root/monitor.log -h192.168.1. -P3306 -uzlm -pzlmzlm --monitor [root@zlm2 :: ~]
#ls -l|grep monitor
-rw-r--r-- root root Jul : monitor.log [root@zlm2 :: ~]
#cat monitor.log
*******************************************************************
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-heartbeat 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-heartbeat line . # A software update is available:
# * The current version for Percona::Toolkit is 3.0. .00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ] [root@zlm2 :: ~]
# //The output will be write into the logfile sepcified by option "--log".
//The logfile will be continuously written unless you've specified th option "--run-time".
//It seems no delay between master and slave at all.We can use sysbench generate some transactions to observe the difference.
Execute sysbench and watch again.
 [root@zlm2 :: ~/sysbench-1.0/src/lua]
#sysbench oltp_read_write.lua --mysql-host=192.168.1.101 --mysql-port= --mysql-user=zlm --mysql-password=zlmzlm --mysql-db=sysbench --tables= --table-size= --mysql-storage-engine=innodb prepare
sysbench 1.0. (using bundled LuaJIT 2.1.-beta2) Creating table 'sbtest1'...
Inserting records into 'sbtest1'
FATAL: mysql_drv_query() returned error (The table 'sbtest1' is full) for query 'INSERT INTO sbtest1(k, c, pad) VALUES(5007652, '---------', '----'),(4980838, '---------', '----'),(4369690, '---------', '----'),(4980019, '---------', '----'),(4997783, '---------', '----'),(4996542, '---------', '----'),(5016589, '---------', '----'),(4994933, '---------', '----'),(5024203, '---------', '----'),(4162450, '---------', '----'),(4995444, '---------', '----'),(5014023, '---------', '----'),(5024261, '---------', '----'),(5024236, '---------', '----'),(5049980, '---------', '----'),(5471904, '---------', '----'),(5019788, '---------', '----'),(5021303, '---------', '----'),(5015123, '---------', '----'),(3676715, '---------', '----'),(5236763, '----FATAL: `sysbench.cmdline.call_command' function failed: ./oltp_common.lua:230: db_bulk_insert_next() failed //Master
[root@zlm2 :: ~/sysbench-1.0/src/lua]
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root .4G .4G 68M % / //The disk space of master is full.
devtmpfs 488M 488M % /dev
tmpfs 497M 497M % /dev/shm
tmpfs 497M 6.6M 491M % /run
tmpfs 497M 497M % /sys/fs/cgroup
/dev/sda1 497M 118M 379M % /boot
none 87G 80G .6G % /vagrant //Slave
[root@zlm3 :: ~]
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root .4G .4G 20K % / //The disk space of slave is full,too.
devtmpfs 488M 488M % /dev
tmpfs 497M 497M % /dev/shm
tmpfs 497M 6.5M 491M % /run
tmpfs 497M 497M % /sys/fs/cgroup
/dev/sda1 497M 118M 379M % /boot
none 87G 80G .6G % /vagrant //Check the output logfile.
[root@zlm2 :: ~]
#tail monitor.log
.00s [ .40s, .65s, .55s ]
.00s [ .26s, .65s, .55s ]
.99s [ .13s, .65s, .55s ]
.99s [ .00s, .66s, .55s ]
.99s [ .86s, .67s, .56s ]
.99s [ .73s, .68s, .56s ]
.99s [ .59s, .70s, .57s ]
.00s [ .46s, .72s, .57s ]
.01s [ .29s, .73s, .58s ]
.99s [ .16s, .75s, .58s ] //Long time later. [root@zlm2 :: ~]
#tail monitor.log
.00s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ] [root@zlm2 :: ~]
#tail monitor.log
.00s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ]
.99s [ .50s, .50s, .50s ]
.00s [ .50s, .50s, .50s ] (zlm@192.168.1.102 )[sysbench]>show slave status\G
ERROR (HY000): Lost connection to MySQL server during query
(zlm@192.168.1.102 )[sysbench]>show slave status\G
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: sysbench //The slave has hung because of the lack of disk space.
//The delay continuously increasing what can be seen in the logfile on master. # :: server id end_log_pos Table_map: `sysbench`.`hb` mapped to number
# at
# :: server id end_log_pos Update_rows: table id flags: STMT_END_F
### UPDATE `sysbench`.`hb`
### WHERE
### @='2018-07-19T04:31:55.004000' /* VARSTRING(78) meta=78 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='mysql-bin.000019' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @= /* LONGINT meta=0 nullable=1 is_null=0 */
### @=NULL /* VARSTRING(765) meta=765 nullable=1 is_null=1 */
### @=NULL /* LONGINT meta=0 nullable=1 is_null=1 */
### SET
### @='2018-07-19T04:31:55.004400' /* VARSTRING(78) meta=78 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='mysql-bin.000019' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @= /* LONGINT meta=0 nullable=1 is_null=0 */
### @=NULL /* VARSTRING(765) meta=765 nullable=1 is_null=1 */
### @=NULL /* LONGINT meta=0 nullable=1 is_null=1 */
# at
# :: server id end_log_pos Xid =
COMMIT/*!*/; //The output above is the detail of updating heartbeat table "hb" on master.
Summary
  • pt-heartbeat is rather useful in checking replica lag of MySQL and it also support PostgreSQL.
  • pt-heartbeat will read connection options in my.cnf by default if you do not specify them.
  • If you've executed the pt-heartbeat with option "--daemonize",don't forget to kill the redundant processes.
  • We can retrieve these lag information in heartbeat table using our monitor tool or script.

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

  1. Linux后台开发工具箱

    https://files-cdn.cnblogs.com/files/aquester/Linux后台开发工具箱.pdf 目录 目录 1 1. 前言 3 2. 脚本类工具 3 2.1. sed命令- ...

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

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

  3. Linux后台开发工具箱-葵花宝典

    Linux后台开发工具箱-葵花宝典 一见 2016/11/4 目录 目录 1 1. 前言 4 2. 脚本类工具 4 2.1. 双引号和单引号 4 2.2. 取脚本完整文件路径 5 2.3. 环境变量和 ...

  4. 推荐几款MySQL相关工具

    前言: 随着互联网技术的不断发展, MySQL 相关生态也越来越完善,越来越多的工具涌现出来.一些公司或个人纷纷开源出一些不错的工具,本篇文章主要介绍几款 MySQL 相关实用工具.提醒下,这里并不介 ...

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

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

  6. SQL慢查询安装过程

    SQL慢查询 基本操作 打开防火墙 firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload 安 ...

  7. 快速安装Percona pt工具

    yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-Time-HiRes perl-IO-Socket-SSLwget http://pk ...

  8. Percona 工具包 pt-online-schema-change 简介

    mysql的在线表结构修改,因为低效和阻塞读写.一直被诟病.至于ALTER TABLE 的原理,参看我上一篇文章.MySQL在线修改大表结构.看完后,发现的问题是还是会锁的,且对于在线更新的这块也是不 ...

  9. 安装percona工具包

    1.安装percona源 sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona- ...

  10. percona pt toolkit 总结

    ##=====================================================##pt-osc之工作流程:1.检查更改表是否有主键或唯一索引,是否有触发器2.检查修改表 ...

随机推荐

  1. 优先队列(priority_queue)的cmp,POJ(2051)

    sort()函数的cmp为函数,priority_queue的cmp为类,具体写法是: struct Node { int i,j; } node[]; struct cmp { bool opera ...

  2. PHPmailer群发Gmail的常见问题

    博主小白一枚,phpmailer只会一些基本的用法,就这样一个邮件的群发功能也难住了我一周,下面把我遇到的问题给大家总结一下 1.Could not authenticate 首先,如果你没有使用循环 ...

  3. python将图像转化为矩阵

    Image.fromarray(matrix).show()

  4. CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]

    启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...

  5. react中密码自动填充及解决火狐浏览器,360浏览器记住密码后,密码框自动填充终极解决方案

    先直接上核心代码如下: 在火狐浏览器,360浏览器,初次加载,bug长这样: 如果你想通过生命周期componentDidMounted等生命周期进行置空操作都是不行的,这可能是浏览器自带的特性记住密 ...

  6. 网页头部ico 不显示

    这个都是很基础的东西,.今天整理以前的笔记,看到了记录一下 <link rel="shortcut icon" href="/favicon.ico"/& ...

  7. spring-开发需要jar包

    需要的开发包 Spring核心开发包: Commons-logging.jar Spring-beans.jar Spring-context.jar Spring-core.jar Spring-e ...

  8. MySQL超大表如何提高count速度

    经常用到count统计记录数,表又超级大,这时候sql执行很慢,就是走索引,也是很慢的,怎么办呢? 1.这个时候我们就要想为什么这么慢:根本原因是访问的数据量太大,就算只计算记录数也是很慢的. 2.如 ...

  9. Python学习之三级菜单

    Python经典练习题 - 三级菜单 需求: 可依次选择进入各子菜单 可从任意一层往回退到上一层 可从任意一层退出程序 示例代码: # -*- coding: utf-8 -*- menu = { ' ...

  10. iPhone 横屏时默认会放大文字的问题

    有人说用 html { text-size-adjust: 100%; }我发现这个并不能解决问题.下面代码可以完美解决. 添加标签:<meta name="viewport" ...