一、下载percona-toolkit工具包

percona-toolkit是一组高级命令行工具的集合,可以查看当前服务的摘要信息,磁盘检测,分析慢查询日志,查找重复索引,实现表同步等等。

[root@push-- src]# cd /usr/local/src/
[root@push-- src]# wget https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/7/x86_64/percona-toolkit-3.0.12-1.el7.x86_64.rpm

二、安装rpm包

root@push-- src]# rpm -ivh percona-toolkit-3.0.-.el7.x86_64.rpm
警告:percona-toolkit-3.0.-.el7.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID cd2efd2a: NOKEY
错误:依赖检测失败:
perl(DBI) >= 1.13 被 percona-toolkit-3.0.-.el7.x86_64 需要
perl(DBD::mysql) >= 1.0 被 percona-toolkit-3.0.-.el7.x86_64 需要
perl(IO::Socket::SSL) 被 percona-toolkit-3.0.-.el7.x86_64 需要
perl(Digest::MD5) 被 percona-toolkit-3.0.-.el7.x86_64 需要
perl(Term::ReadK

显示需要一些依赖包,安装相应依赖包

[root@push-- src]#  yum install -y perl-DBI perl-DBD-MySQL perl-Digest-MD5 perl-IO-Socket-SSL perl-TermReadKey

再次安装

[root@push-- src]# rpm -ivh percona-toolkit-3.0.-.el7.x86_64.rpm
警告:percona-toolkit-3.0.-.el7.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID cd2efd2a: NOKEY
准备中... ################################# [%]
正在升级/安装...
:percona-toolkit-3.0.-.el7 ################################# [%]
[root@push-- src]#

安装成功,验证

[root@push-- src]# pt-online-schema-change --version
pt-online-schema-change 3.0.

三、工具命令的使用

1、pt-heartbeat
    #用于监控mysql复制架构的延迟。
    #主要是通过在主库上的--update线程持续更新指定表上的一个时间戳,从库上--monitor线程或者--check线程检查主库更新的时间戳并与当前系统时间对比,得到延迟值。

2、pt-query-digest
    #用于分析mysql服务器的慢查询日志,并格式化输出以便于查看和分析。

3、pt-diskstats
    #类似于iostat,打印磁盘io统计信息,但是这个工具是交互式并且比iostat更详细。可以分析从远程机器收集的数据。

4、pt-slave-find
    #连接mysql主服务器并查找其所有的从,然后打印出所有从服务器的层级关系。

[root@push-- src]# pt-slave-find --host=127.0.0.1 --user=root --password=xxxxxxx
127.0.0.1
Version 5.7.-log
Server ID
Uptime +:: (started --12T13::)
Replication Is not a slave, has slaves connected, is not read_only
Filters
Binary logging ROW
Slave status
Slave mode STRICT
Auto-increment increment , offset
InnoDB version 5.7.
+- 172.28.5.222
Version 5.7.-log
Server ID
Uptime +:: (started --12T14::)
Replication Is not a slave, has slaves connected, is not read_only
Filters
Binary logging ROW
Slave status
Slave mode STRICT
Auto-increment increment , offset
InnoDB version 5.7.
+- 172.28.5.223
Version 5.7.-log
Server ID
Uptime +:: (started --12T14::)
Replication Is not a slave, has slaves connected, is not read_only
Filters
Binary logging ROW
Slave status
Slave mode STRICT
Auto-increment increment , offset
InnoDB version 5.7.
[root@push-- src]#

5、pt-stalk
    #用于收集mysql数据库故障时的相关信息便于后续诊断处理。

6、pt-kill
    #Kill掉符合指定条件mysql语句

7、pt-upgrade

#该命令主要用于对比不同mysql版本下SQL执行的差异,通常用于升级前进行对比。
    #会生成SQL文件或单独的SQL语句在每个服务器上执行的结果、错误和警告信息等。
  
  8、pt-online-schema-change
    #功能为支持在线变更表构,且不锁定原表,不阻塞原表的DML操作。
    #该特性与Oracle的dbms_redefinition在线重定义表原理基本类似。
    
  9、pt-mysql-summary
    #对连接的mysql服务器生成一份详细的配置情况以及sataus信息
    #在尾部也提供当前实例的的配置文件的信息

四、pt-heartbeat 监控主从同步原理

1、主库上存在一个用于检查延迟的表heartbeat,可手动或自动创建, pt-heartbeat使用--update参数连接到主库上并持续(根据设定的--interval参数)使用一个时间戳更新到表heartbeat

2、pt-heartbeat使用--monitor 或--check连接到从库,检查从主库同步过来的时间戳,并与当前系统时间戳进行比对产生一个差值,该值则用于判断延迟。(注,前提条件是主库与从库应保持时间同步)

五、实际操作pt-heartbeat 监控主从同步

1、主库上手工创建一个heartbeat数据库

mysql> create database heartbeat default CHARACTER set utf8;
Query OK, row affected (0.16 sec)

2、利用pt-heartbeat的--create-table  参数在主库上添加heartbeat表

[root@push-- ~]# pt-heartbeat -S /var/lib/mysql//mysql.sock -uroot -pxxxxxxxx -D heartbeat --create-table --update
*******************************************************************
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:

ctrl+c 退出,进入mysql查看heartbeat表

[root@push-- ~]# mysql -S /var/lib/mysql//mysql.sock -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use heartbeat;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select * from heartbeat;
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| --04T11::07.006920 | | master-221.000022 | | NULL | NULL |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
row in set (0.00 sec) mysql>

3、启动pt-heartbeat守护进程,定时更新heartbeat表,定期update(默认1S)最新的timestamp信息到ts字段中

[root@push-- ~]# pt-heartbeat -S /var/lib/mysql//mysql.sock -uroot -pxxxxxxxxxxx -D heartbeat --update --daemonize
[root@push-- ~]#

4、查看heartbeat表,不停查询,此时发现ts时间戳每秒会更新一次

mysql> select * from heartbeat;
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| --04T11::06.001140 | | master-221.000022 | | NULL | NULL |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
row in set (0.00 sec) mysql> select * from heartbeat;
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| --04T11::08.064020 | | master-221.000022 | | NULL | NULL |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
row in set (0.00 sec) mysql> select * from heartbeat;
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
| --04T11::10.097910 | | master-221.000022 | | NULL | NULL |
+----------------------------+-----------+-------------------+-----------+-----------------------+---------------------+
row in set (0.00 sec)

5、运行pt-hearbeat守护进程连接从库执行监控命令

[root@push-- ~]# pt-heartbeat -uroot -pxxxxxxxxx -h172.28.5. --port= -D heartbeat --monitor
*******************************************************************
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:
.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 ]
.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 ]
.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 ]
^C

监控数据会每秒刷新一次,可以看到目前主从同步良好,没有延迟。

6、重启主库更新进程

[root@push-- ~]# pt-heartbeat --stop
Successfully created file /tmp/pt-heartbeat-sentinel
[root@push-- ~]# rm -rf /tmp/pt-heartbeat-sentinel
[root@push-- ~]# pt-heartbeat -S /var/lib/mysql//mysql.sock -uroot -pxxxxxxxxxxx -D heartbeat --update --interval= --daemonize
[root@push-- ~]#

7、使用守护进程监控从库,并输出到日志文件

[root@push-- ~]# pt-heartbeat -uroot -pxxxxxxxxxxx -h172.28.5. --port= -D heartbeat --monitor --daemonize --log=/tmp/mysql-slave-sync.log
[root@push-- ~]#

8、实时查看日志

[root@push-- ~]# tail -f /tmp/mysql-slave-sync.log
.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 ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]
.00s [ .00s, .00s, .00s ]

9、测试验证

1、登录从库,停掉slave

[root@push-- ~]# mysql -uroot -p -S /var/lib/mysql//mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> stop slave;
Query OK, rows affected (0.07 sec)

2、查看日志

[root@push-- ~]# tail -f /tmp/mysql-slave-sync.log
.00s [ .42s, .08s, .36s ]
.00s [ .85s, .17s, .39s ]
.00s [ .30s, .26s, .42s ]
.00s [ .77s, .35s, .45s ]
.00s [ .25s, .45s, .48s ]
.00s [ .75s, .55s, .52s ]
.00s [ .27s, .65s, .55s ]
.00s [ .80s, .76s, .59s ]
.00s [ .35s, .87s, .62s ]
.00s [ .92s, .98s, .66s ]
.00s [ .50s, .10s, .70s ]
.00s [ .10s, .22s, .74s ]
.00s [ .72s, .34s, .78s ]

此时已经出现了延迟

3、再次启动从库,同步数据

mysql> start slave;
Query OK, rows affected (0.01 sec) mysql>

此时监控同步延迟马上减小到0,表示同步成功,没有延迟。

.00s [ .50s, .50s, .40s ]
.00s [ .50s, .50s, .96s ]
.00s [ .50s, .50s, .52s ]
.00s [ .50s, .50s, .09s ]
.00s [ .50s, .50s, .65s ]
.00s [ .50s, .50s, .22s ]
.00s [ .50s, .50s, .78s ]
.00s [ .50s, .50s, .35s ]
.00s [ .50s, .50s, .92s ]
.00s [ .50s, .50s, .49s ]
.00s [ .50s, .50s, .06s ]
.00s [ .50s, .50s, .63s ]
.00s [ .50s, .50s, .21s ]
.00s [ .50s, .50s, .78s ]
.00s [ .50s, .50s, .36s ]
.00s [ .50s, .50s, .93s ]
.00s [ .50s, .50s, .51s ]
.00s [ .50s, .50s, .09s ]
.00s [ .50s, .50s, .67s ]
.00s [ .50s, .50s, .25s ]
.00s [ .50s, .50s, .83s ]
.00s [ .13s, .23s, .33s ]
.02s [ .82s, .56s, .69s ]
.00s [ .03s, .81s, .69s ]
.00s [ .23s, .05s, .69s ]
.00s [ .42s, .28s, .69s ]
.00s [ .58s, .52s, .69s ]
.00s [ .73s, .75s, .69s ]
.00s [ .87s, .97s, .69s ]
.00s [ .98s, .20s, .69s ]
.00s [ .08s, .42s, .69s ]
.00s [ .17s, .63s, .69s ]
.00s [ .23s, .85s, .69s ]
.00s [ .28s, .06s, .69s ]
.00s [ .32s, .26s, .69s ]
.00s [ .33s, .47s, .69s ]

六、利用Zabbix监控Mysql主从同步

1、使用pt-heartbeat 的--check选项参数,可以返回当前从库的同步延迟数据

[root@push-- zabbix]# pt-heartbeat -uroot -pxxxxxxxxx -h172.28.5. --port= -D heartbeat --check
0.00
[root@push-- zabbix]#

2、利用这个返回的延迟数,可以自定义一个zabbix监控项

[root@push-- ~]# vim /usr/local/zabbix/conf/zabbix_agentd.conf

在zabbix_agentd.conf配置文件中添加一个监控项

UserParameter=hlsms.check_mysql_slave_status, pt-heartbeat -uroot -pxxxxxxxxxx -h172.28.5. --port= -D heartbeat --check >/dev/null

3、启动zabbix_agent服务,使新加监控项生效

[root@push-- ~]# killall zabbix_agentd
[root@push-- ~]# zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
[root@push-- ~]#

4、在zabbix服务器端利用zabbix_get命令检验监控项

[root@zabbix_server mysh]# zabbix_get -s 172.28.5.221 -p  -k "hlsms.check_mysql_slave_status"
0.00

成功返回最新同步延迟数据

5、在zabbix管控台在主库的主机下新建一个监控项hlsms.check_mysql_slave_status,每10秒监控一次

6、在主库主机上打开最新数据,查看是否有hlsms.check_mysql_slave_status返回的数据

可以看到已经有数据获取到了,正常情况下同步成功没有延迟返回时0.00,有延迟会返回x.00,x表示延迟了多少秒。

7、创建触发器,当同步延迟达到60秒,发送告警,当同步延迟为0时,恢复。

8、测试zabbix监控项

停掉从库

mysql> stop slave;
Query OK, rows affected (0.06 sec) mysql>

此时刷新主库主机最新数据hlsms.check_mysql_slave_status监控项

可以看到已经获取到了最新延迟,等到了60后,产生告警

告警成功,再次连接从库,启动同步线程

mysql> start slave;
Query OK, rows affected (0.02 sec) mysql>

当完全同步后,返回延迟数据为0,

告警随之恢复

至此,zabbix监控Mysql主从状态配置成功。

zabbix利用percona-toolkit工具监控Mysql主从同步状态的更多相关文章

  1. 监控mysql主从同步状态脚本

    监控mysql主从同步状态脚本 示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroo ...

  2. 监控mysql主从同步状态是否异常

    监控mysql主从同步状态是否异常,如果异常,则发生短信或邮寄给管理员 标签:监控mysql主从同步状态是否异常 阶段1:开发一个守护进程脚本每30秒实现检测一次. 阶段2:如果同步出现如下错误号(1 ...

  3. 监控mysql主从同步状态

    在高并发网站架构中,MySQL数据库主从同步是不可或缺的,不过经常会发生由于网络原因或者操作错误,MySQL主从经常会出现不同步的情况,那么如何监控MySQL主从同步,也变成网站正常运行的重要环节. ...

  4. nagios 实现Mysql 主从同步状态的监控

    一.系统环境 主机名 IP nagios 192.168.15.111 mysql_s 192.168.15.21 二.操作步骤 2.1 mysql_s端的配置 2.1.1 编写check_mysql ...

  5. zabbix3.0.4监控mysql主从同步

    zabbix3.0.4监控mysql主从同步 1.监控mysql主从同步原理: 执行一个命令 mysql -u zabbix -pzabbix -e 'show slave status\G' 我们在 ...

  6. 监控mysql主从同步

    1,昨天看到shell一道面试题,需求如下: 监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员.提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:阶段1:开发一个守护进 ...

  7. nagios系列(七)nagios通过自定义脚本的方式监控mysql主从同步

    nagios监控mysql主从同步 起因:nagios可能监控到mysql服务的运行情况,但确不能监控mysql的主从复制是否正常:有时候,同步已经停止,但管理人员却不知道. 登陆mysql从服务器, ...

  8. shell脚本监控MySQL主从同步

    企业面试题1:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员. 阶段1:开发一个守护进程脚本每30秒实现检测一次. 阶段2:如果同步出现如下错误号(1158,1159,1008, ...

  9. 用shell脚本监控MySQL主从同步

    企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员.提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:阶段1:开发一个守护进程脚本每30秒 ...

随机推荐

  1. JVM系列1:内存区域

    1.JVM运行区域内存划分 2.各内存区域详细介绍 2.1 程序计数器 程序计数器是一块很小的内存区域,它作为前线程所执行的字节码的行号指示器,指向当前class文件的执行代码的行数.字节码解释器工作 ...

  2. leetcode-easy-math-204 Count Primes-NO

    mycode     time limited class Solution(object): def countPrimes(self, n): """ :type n ...

  3. Socket上自定义协议总结

    TCP只是一个可靠传输的通信管道,上层协议要你自己定的,通俗来说就是发送方和接收方的约定 自定义协议的核心有两个:1. 控制码2. 流程控制 用Socket进行通信,发送的数据包一定是有结构的,类似于 ...

  4. WPF prism 类、属性和方法的导入和导出

    学习Prism一定要掌握依赖注入的应用,只有了解了Prism的依赖注入才能更好的使用Prism提升应用开发的架构. 首先说明Prism依赖注入有两种方式及MEF和Unity ,在Prism中是两个没有 ...

  5. CompletableFuture引入

    一.Future介绍 Future以前我们如果有个方法action执行,比如去数据库中查询数据.上传一个文件等,这个方法执行10分钟,调用者就需要等10分钟.基于此,调用者可以先执行action,返回 ...

  6. SweetAlert(弹出层插件)

    怀着非常.非常恼火的心情写下这个博文.我深深怀疑有一些人只会瞎粘贴复制别人的博文,自己试都不试就发布到网上,左右看的人想法,就此深通恶绝!! 废话不多说. SweetAlert是一个简洁好用的自制Al ...

  7. python安装mongodb

    提前把mongodb-linux-x86_64-rhel70-3.2.4.tgz放到和脚本相同目录下,然后把下复制到脚本里面,开始执行 #!/usr/bin/python #-*- codinig: ...

  8. Unity Shader 基础

    推荐: https://www.cnblogs.com/nanwei/p/7277417.html 上面链接作者的整个系列都写的不错 https://www.cnblogs.com/nanwei/ca ...

  9. eureka和zookeeper的区别?

    eureka和zookeeper都可以提供服务注册与发现的功能,       zookeeper 是CP原则,强一致性(consistency)和分区容错性(Partition).       eur ...

  10. 【LeetCode】309、最佳买卖股票时机含冷冻期

    Best Time to Buy and Sell Stock with Cooldown 题目等级:Medium 题目描述: Say you have an array for which the ...