Chinaunix首页 | 论坛 | 博客
  • 博客访问: 952577
  • 博文数量: 83
  • 博客积分: 32
  • 博客等级: 民兵
  • 技术积分: 2080
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-12 09:45
个人简介

努力学习,努力分享,努力.....在努力..努力离开..

文章分类

全部博文(83)

文章存档

2017年(3)

2015年(1)

2014年(47)

2013年(32)

分类: Mysql/postgreSQL

2014-01-26 14:40:57

在常用的MySQL的架构中,Master-Slave架构用的最多,但是对于主从的延迟一般都是按照Seconds_Behind_Master来查看的,但是这个参数有时候是不准确的,比如网络闪断Seconds_Behind_Master看不出任何问题,在看pos才发现已经不走了.所以用该状态值去判断主从延迟不太靠谱.在这里用到Percona-Toolkit系列工具中的pt-heartbeat来实现监控主从复制延迟.

安装Percona-Toolkit

[mysql@localhost ~]$ sudo yum install percona-toolkit

使用方法:

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

例子:
[mysql@localhost ~]$ pt-heartbeat --user=root --password=xxxx -h 192.168.40.130 --D test --master-server-id=2 --check
128.00

[mysql@localhost ~]$ pt-heartbeat --user=root --password=xxxx -h 192.168.40.130 --D test --master-server-id=2 --monitor
0.00s [  0.00s,  0.00s,  0.00s ]
0.00s [  0.00s,  0.00s,  0.00s ]

[mysql@localhost ~]$ pt-heartbeat --user=root --password=xxxx -h 192.168.40.130 --D test --master-server-id=2 --update --daemonize
[mysql@localhost ~]$ ps -ef | grep mysql
mysql     3844     1  0 11:47 ?        00:00:00 perl /usr/bin/pt-heartbeat --user=root --password=root -h 192.168.40.176 --D test --master-server-id=2 --update --daemonize
mysql     3853  1579  0 11:48 pts/0    00:00:00 ps -ef

[mysql@localhost ~]$ pt-heartbeat --user=repl --password=xxxx --host=192.168.40.130 --port=3306 --create-table -D test --monitor --daemonize --master-server-id=2 --log=/mysql/pt-slave.log
[mysql@localhost ~]$ 

[mysql@localhost ~]$ tail -f /mysql/pt-slave.log 
795.00s [ 14.20s,  2.64s,  0.88s ]
796.00s [ 28.42s,  5.28s,  1.76s ]
797.00s [ 37.65s,  7.93s,  2.64s ]
798.00s [ 54.90s, 10.58s,  3.53s ]

0表示无没有延迟.[0.00s,0.00s,0.00s]表示1m,5m,15m的平均值,可以通过--frames去设置.

使pt-heartbeat守护进程停止

[mysql@localhost ~]$ pt-heartbeat --stop
Successfully created file /tmp/pt-heartbeat-sentinel
[mysql@localhost ~]$ rm -f /tmp/pt-heartbeat-sentinel

常用参数明细:
注意:--update,--monitor和--check是互斥的,--daemonize和--check是互斥.

--check
检查从的延迟,检查一次就退出.

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

--create-table
在主上创建心跳监控的表,如果该表不存在.可以自己建立.
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,而且是必须指定.

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

--recurse
多级复制的检查深度.比如M-S-S架构.

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

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

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

--stop
停止运行该工具

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

测试:

主:192.168.40.176

从:192.168.40.130

[mysql@localhost ~]$ pt-heartbeat --user=test --password=xxxxx --host=192.168.40.130 --port=3306 --create-table -D test --interval=1 --frames=1s,2s,3s --master-server-id=2 --print-master-server-id --monitor
0.00s [  0.00s,  0.00s,  0.00s ] 2
0.00s [  0.00s,  0.00s,  0.00s ] 2
0.00s [  0.00s,  0.00s,  0.00s ] 2

在M-S-S架构中用如下命令

[mysql@localhost ~]$ pt-heartbeat --user=test --password=xxxxx --host=192.168.40.130 --port=3306 --create-table -D test --interval=1 --frames=1s --master-server-id=2 --print-master-server-id --recurse=3 --recursion-method=processlist --check
192.168.40.130:3306  0.00 2
192.168.40.177:3306  0.00 2
阅读(6079) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~