pt-heartbeat监控mysql主从复制

对于MySQL数据库主从复制延迟的监控,可以借助percona的有力武器pt-heartbeat来实现。
pt-heartbeat的工作原理通过使用时间戳方式在主库上更新特定表,然后在从库上读取被更新的时间戳然后与本地系统时间对比来得出其延迟。具体流程:
1)在主上创建一张heartbeat表,按照一定的时间频率更新该表的字段(把时间更新进去)。监控操作运行后,heartbeat表能促使主从同步!
2)连接到从库上检查复制的时间记录,和从库的当前系统时间进行比较,得出时间的差异。
鸣谢 MySQL DBA散尽浮华 也是参考了他们的文章以及pt-toolkit下线文档

首先通过percona官网的下载地址下载通用二进制包,可以避免很多依赖问题。
安装过程也很简单 解压包 移动到/usr/local/toolkit后简历软连接

tar -zxvf percona-toolkit-3.0.12_x86_64.tar.gz

mv percona-toolkit-3.0.12_x86_64.tar.gz /usr/local/toolkit

ln -sf /usr/local/toolkit/bin/* /usr/bin/ 

执行命令验证:

[root@localhost bin]# pt-heartbeat --help
pt-heartbeat measures replication lag on a MySQL or PostgreSQL server.  You can
use it to update a master or monitor a replica.  If possible, MySQL connection
options are read from your .my.cnf file.  For more details, please use the
--help option, or try 'perldoc /usr/bin/pt-heartbeat' for complete
documentation.

Usage: pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop

Options:

  --ask-pass                  Prompt for a password when connecting to MySQL
  --charset=s             -A  Default character set
  --check                     Check slave delay once and exit
  --check-read-only           Check if the server has read_only enabled; If it
                              does, the tool skips doing any inserts
  --config=A                  Read this comma-separated list of config files;
                              if specified, this must be the first option on
                              the command line
  --create-table              Create the heartbeat --table if it does not exist
  --create-table-engine=s     Sets the engine to be used for the heartbeat table
  --daemonize                 Fork to the background and detach from the shell
  --database=s            -D  The database to use for the connection
  --dbi-driver=s              Specify a driver for the connection; mysql and Pg
                              are supported (default mysql)
  --defaults-file=s       -F  Only read mysql options from the given file
  --fail-successive-errors=i  If specified, pt-heartbeat will fail after given
                              number of successive DBI errors (failure to
                              connect to server or issue a query)
  --file=s                    Print latest --monitor output to this file
  --frames=s                  Timeframes for averages (default 1m,5m,15m)
  --help                      Show help and exit
  --host=s                -h  Connect to host
  --[no]insert-heartbeat-row  Insert a heartbeat row in the --table if one
                              doesn't exist (default yes)
  --interval=f                How often to update or check the heartbeat --
                              table (default 1.0)
  --log=s                     Print all output to this file when daemonized
  --master-server-id=s        Calculate delay from this master server ID for --
                              monitor or --check
  --monitor                   Monitor slave delay continuously
  --password=s            -p  Password to use when connecting
  --pid=s                     Create the given PID file
  --port=i                -P  Port number to use for connection
  --print-master-server-id    Print the auto-detected or given --master-server-
                              id
  --read-only-interval=i      When --check-read-only is specified, the interval
                              to sleep while the server is found to be read-only
  --recurse=i                 Check slaves recursively to this depth in --check
                              mode
  --recursion-method=a        Preferred recursion method used to find slaves (
                              default processlist,hosts)
  --replace                   Use REPLACE instead of UPDATE for --update
  --run-time=m                Time to run before exiting.  Optional suffix s=
                              seconds, m=minutes, h=hours, d=days; if no
                              suffix, s is used.
  --sentinel=s                Exit if this file exists (default /tmp/pt-
                              heartbeat-sentinel)
  --set-vars=A                Set the MySQL variables in this comma-separated
                              list of variable=value pairs
  --skew=f                    How long to delay checks (default 0.5)
  --slave-password=s          Sets the password to be used to connect to the
                              slaves
  --slave-user=s              Sets the user to be used to connect to the slaves
  --socket=s              -S  Socket file to use for connection
  --stop                      Stop running instances by creating the sentinel
                              file
  --table=s                   The table to use for the heartbeat (default
                              heartbeat)
  --update                    Update a master's heartbeat
  --user=s                -u  User for login if not current user
  --utc                       Ignore system time zones and use only UTC
  --version                   Show version and exit
  --[no]version-check         Check for the latest version of Percona Toolkit,
                              MySQL, and other programs (default yes)

Option types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time

Rules:

  Specify at least one of --stop, --update, --monitor, or --check.
  --update, --monitor, and --check are mutually exclusive.
  --daemonize and --check are mutually exclusive.
  This tool accepts additional command-line arguments. Refer to the SYNOPSIS and usage information for details.

DSN syntax is key=value[,key=value...]  Allowable DSN keys:

  KEY  COPY  MEANING
  ===  ====  =============================================
  A    yes   Default character set
  D    yes   Default database
  F    yes   Only read default options from the given file
  P    yes   Port number to use for connection
  S    yes   Socket file to use for connection
  h    yes   Connect to host
  p    yes   Password to use when connecting
  u    yes   User for login if not current user

  If the DSN is a bareword, the word is treated as the 'h' key.

Options and values after processing arguments:

  --ask-pass                  FALSE
  --charset                   (No value)
  --check                     FALSE
  --check-read-only           FALSE
  --config                    /etc/percona-toolkit/percona-toolkit.conf,/etc/percona-toolkit/pt-heartbeat.conf,/root/.percona-toolkit.conf,/root/.pt-heartbeat.conf
  --create-table              FALSE
  --create-table-engine       (No value)
  --daemonize                 FALSE
  --database                  (No value)
  --dbi-driver                mysql
  --defaults-file             (No value)
  --fail-successive-errors    (No value)
  --file                      (No value)
  --frames                    1m,5m,15m
  --help                      TRUE
  --host                      (No value)
  --insert-heartbeat-row      TRUE
  --interval                  1.0
  --log                       (No value)
  --master-server-id          (No value)
  --monitor                   FALSE
  --password                  (No value)
  --pid                       (No value)
  --port                      (No value)
  --print-master-server-id    FALSE
  --read-only-interval        (No value)
  --recurse                   (No value)
  --recursion-method          processlist,hosts
  --replace                   FALSE
  --run-time                  (No value)
  --sentinel                  /tmp/pt-heartbeat-sentinel
  --set-vars                  
  --skew                      0.5
  --slave-password            (No value)
  --slave-user                (No value)
  --socket                    (No value)
  --stop                      FALSE
  --table                     heartbeat
  --update                    FALSE
  --user                      (No value)
  --utc                       FALSE
  --version                   FALSE
  --version-check             TRUE

这里参考下MySQL DBA的文章里的中文说明:

注意:需要指定的参数至少有 --stop,--update,--monitor,--check。其中--update,--monitor和--check是互斥的,--daemonize和--check也是互斥。

--ask-pass
隐式输入MySQL密码

--charset
字符集设置

--check
检查从的延迟,检查一次就退出,除非指定了--recurse会递归的检查所有的从服务器。

--check-read-only
如果从服务器开启了只读模式,该工具会跳过任何插入。

--create-table
在主上创建心跳监控的表,如果该表不存在。可以自己建立,建议存储引擎改成memory。通过更新该表知道主从延迟的差距。
CREATE TABLE heartbeat (
  ts                    varchar(26) NOT NULL,
  server_id             int unsigned NOT NULL PRIMARY KEY,
  file                  varchar(255) DEFAULT NULL,    -- SHOW MASTER STATUS
  position              bigint unsigned DEFAULT NULL, -- SHOW MASTER STATUS
  relay_master_log_file varchar(255) DEFAULT NULL,    -- SHOW SLAVE STATUS
  exec_master_log_pos   bigint unsigned DEFAULT NULL  -- SHOW SLAVE STATUS
);
heratbeat表一直在更改ts和position,而ts是我们检查复制延迟的关键。

--daemonize
执行时,放入到后台执行

--user
-u,连接数据库的帐号

--database
-D,连接数据库的名称

--host
-h,连接的数据库地址

--password
-p,连接数据库的密码

--port
-P,连接数据库的端口

--socket
-S,连接数据库的套接字文件

--file 【--file=output.txt】
打印--monitor最新的记录到指定的文件,很好的防止满屏幕都是数据的烦恼。

--frames 【--frames=1m,2m,3m】
在--monitor里输出的[]里的记录段,默认是1m,5m,15m。可以指定1个,如:--frames=1s,多个用逗号隔开。可用单位有秒(s)、分钟(m)、小时(h)、天(d)。

--interval
检查、更新的间隔时间。默认是见是1s。最小的单位是0.01s,最大精度为小数点后两位,因此0.015将调整至0.02。

--log
开启daemonized模式的所有日志将会被打印到制定的文件中。

--monitor
持续监控从的延迟情况。通过--interval指定的间隔时间,打印出从的延迟信息,通过--file则可以把这些信息打印到指定的文件。

--master-server-id
指定主的server_id,若没有指定则该工具会连到主上查找其server_id。

--print-master-server-id
在--monitor和--check 模式下,指定该参数则打印出主的server_id。

--recurse
多级复制的检查深度。模式M-S-S...不是最后的一个从都需要开启log_slave_updates,这样才能检查到。

--recursion-method
指定复制检查的方式,默认为processlist,hosts。

--update
更新主上的心跳表。

--replace
使用--replace代替--update模式更新心跳表里的时间字段,这样的好处是不用管表里是否有行。

--stop
停止运行该工具(--daemonize),在/tmp/目录下创建一个“pt-heartbeat-sentinel” 文件。后面想重新开启则需要把该临时文件删除,才能开启(--daemonize)。

--table
指定心跳表名,默认heartbeat。

这里大家可以慢慢研究。

安装就完成了 接下来就是怎么监控:
主:192.168.113.153
从:192.168.113.155
这里说明下,操作均可以在主上执行, 以下操作都在主上执行:
选择存放监控表的数据库,在库中创建heartbeat 比如我的dcumcm:

use dcumcm;
CREATE TABLE `heartbeat` (
  `ts` varchar(26) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `file` varchar(255) DEFAULT NULL,
  `position` bigint(20) unsigned DEFAULT NULL,
  `relay_master_log_file` varchar(255) DEFAULT NULL,
  `exec_master_log_pos` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在主上运行:–daemonize表示守护进程,–interval=1每一秒更新一次表时间后台运行。

[root@localhost bin]#  pt-heartbeat --user=root --ask-pass --host=192.168.113.153  -D dcumcm --interval=1 --update --replace --daemonize

在主上运行监测复制延迟

[root@localhost bin]#  pt-heartbeat -D dcumcm --table=heartbeat --monitor --host=192.168.113.155 --user=back --password=123456
0.00s [  0.00s,  0.00s,  0.00s ]
...
8.00s [  1.08s,  0.22s,  0.07s ]
9.00s [  1.23s,  0.25s,  0.08s ]

0表示从没有延迟。 [ 0.00s, 0.00s, 0.00s ] 表示1m,5m,15m的平均值。可以通过–frames去设置。
刚部署完我们还可以使用–check监测一次就退出:

[root@localhost bin]# pt-heartbeat -D cumcm --table=heartbeat --check -h 192.168.103.55

如何关闭主上面执行的后台进程。可以用参数–stop 去关闭:
这里直接使用散尽浮华的文章内容:

[root@master-server ~]# ps -ef|grep heartbeat
root 15152 1 0 19:49 ? 00:00:02 perl /usr/bin/pt-heartbeat --user=root --ask-pass --host=192.168.1.101 --create-table -D huanqiu --interval=1 --update --replace --daemonize
root 15310 1 0 19:59 ? 00:00:01 perl /usr/bin/pt-heartbeat -D huanqiu --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123456 --log=/opt/master-slave.txt --daemonize
root 15555 31932 0 20:13 pts/2 00:00:00 grep heartbeat
[root@master-server ~]# pt-heartbeat --stop
Successfully created file /tmp/pt-heartbeat-sentinel
[root@master-server ~]# ps -ef|grep heartbeat
root 15558 31932 0 20:14 pts/2 00:00:00 grep heartbeat
[root@master-server ~]#

这样就把在主上开启的进程杀掉了,后续要继续开启后台进行的话,需要把/tmp/pt-heartbeat-sentinel 文件删除,否则启动不了。

也可以通过kill

[root@master-server ~]# ps -ef|grep heartbeat
root 15152 1 0 19:49 ? 00:00:02 perl /usr/bin/pt-heartbeat --user=root --ask-pass --host=192.168.1.101 --create-table -D huanqiu --interval=1 --update --replace --daemonize
root 15310 1 0 19:59 ? 00:00:01 perl /usr/bin/pt-heartbeat -D huanqiu --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123456 --log=/opt/master-slave.txt --daemonize
root 15555 31932 0 20:13 pts/2 00:00:00 grep heartbeat
[root@master-server ~]# kill -9 15152
[root@master-server ~]# ps -ef|grep heartbeat
root 15558 31932 0 20:14 pts/2 00:00:00 grep heartbeat

这里目前的监控是临时监控方式 直接console输出 其实可以直接输出到文件中 在通过读取文件中的数值来达到自动化监控,后续更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值