安装:
wget https://www.percona.com/downloads/percona-toolkit/2.2.20/tarball/percona-toolkit-2.2.20.tar.gz
tar xf percona-toolkit-2.2.20.tar.gz
cd percona-toolkit-2.2.20
yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes perl perl-DBI -y
perl Makefile.PL
make && make install
==================================================================================
检查数据的一致性
pt-table-checksum工具的使用
主从复制:
主:192.168.1.196
从:192.168.1.102
表一定要创建唯一主键或者是索引
pt-table-checksum在主服务器上添加权限 SELECT, INSERT, UPDATE, DELETE, CREATE, PROCESS, SUPER, REPLICATION SLAVE
1.在196上执行命令
[root@localhost ~]# pt-table-checksum --nocheck-replication-filters --create-replicate-table --no-check-binlog-format --replicate-check-only --replicate=yjzh.checksums --databases=yjzh --host=192.168.1.196 --user=root --password=123 --port=3306
参数:
--nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库
--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错
--replicate-check-only : 只显示不同步的信息
--replicate= : 把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中,一般在库名加checksum 上例:ykjk.checksums
--databases= : 指定需要被检查的数据库,多个则用逗号隔开
--tables= : 指定需要被检查的表,多个用逗号隔开
h= 或 --host= : 指定host地址,一般是master的地址
u= 或 --user= : 指定用户名,
p= 或 --password : 密码
P= 或 --port : 端口
在主从上都要进行授权,
[root@localhost ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate-check-only--replicate=ykjk.checksums --databases=ykjk --host=192.168.1.196 --user=root --password=123 --port=3306
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
09-23T11:34:22 0 0 1 1 0 0.027 ykjk.aa
结果参数的解释:
TS :完成检查的时间
ERRORS :检查时候发生错误和警告的数量
DIFFS :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息
ROWS :表的行数
CHUNKS :被划分到表中的块的数目
SKIPPED :由于错误或警告或过大,则跳过块的数目
TIME : 执行的时间
TABLE : 被检查的表名
=====================================================================================================================
pt-table-sync 负责当主从数据不一致时修复数据,让它们保存数据的一致性
当主从数据库数据不一致时,可以使用这个工具
pt-table-sync: 高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在
1.先创建表的索引
alter table aa add primary key(name);
2.利用工具修复主从不一致
pt-table-sync --replicate=ykjk.checksums h=192.168.1.196,u=root,p=123 h=192.168.1.102,u=root,p=123 --execute
参数:
第一个h : 是master的地址
第二个h :是slave的地址
--execute : 执行命令
--print : 打印,但不执行命令
pt-table-checksum 命令在检查数据一致性时会在库里产生一张checksums表,这张表所在的库是不能删除的,即使是删除了也会过段时间自动生成,如果想删除这张表和库,则需要把权限收回,才能删除这张表和库,主库删除,从库自动就删除了。
==========================================================================================================
解决数据的主从复制延迟
pt-heartbeat的使用
pt-heartbeat --help
pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop
至少指定上面四个参数的一个
其中:--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,
position bigint unsigned DEFAULT NULL,
relay_master_log_file varchar(255) DEFAULT NULL,
exec_master_log_pos bigint unsigned DEFAULT NULL
);
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。
更新主库上的heartbeat,--interval=1表示1秒钟更新一次(注意这个启动操作要在主库服务器上执行)
说是这条命令pt-heartbeat --user=root --ask-pass --host=192.168.1.196 --create-table -D yjzh --interval=1 --update --replace --daemonize也可以,但是用这条命令报错所以我是用下面这条命令
[root@localhost ~]# pt-heartbeat --user=root --password=123 --host=192.168.1.196 --create-table -D yjzh --interval=1 --update --replace --daemonize
查看是否正在运行
[root@localhost ~]# ps -ef|grep pt-heartbeat
root 2784 1 0 14:20 ? 00:00:00 perl /usr/local/bin/pt-heartbeat --user=root --password=123 --host=192.168.1.196 --create-table -D yjzh --interval=1 --update --replace --daemonize
root 2786 1157 0 14:20 pts/0 00:00:00 grep pt-heartbeat
在主库上运行监测同步延迟
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
解释:0表示从没有延迟。 [ 0.00s, 0.00s, 0.00s ] 表示1m,5m,15m的平均值。可以通过--frames去设置
或者加上--master-server-id参数(主库my.cnf里配置的server-id值)
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123 --master-server-id=2
0.02s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
也可以将主库的server-id打印出来(--print-master-server-id)
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123 --print-master-server-id
0.00s [ 0.00s, 0.00s, 0.00s ] 2
0.00s [ 0.00s, 0.00s, 0.00s ] 2
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --check --host=192.168.1.102 --user=root --password=123 --print-master-server-id
0.00 2
上面的监测命令会一直在运行状态中,可以使用--check监测一次就退出
但是使用--check就不能使用--monitor,因为这两个参数是互斥的
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --check --host=192.168.1.102 --user=root --password=123
0.00
注意:
如果想把这个输出结果加入自动化监控,那么可以使用如下命令使监控输出写到文件,然后使用脚本定期过滤文件中的最大值作为预警即可:
注意--log选项必须在有--daemonize参数的时候才会打印到文件中,且这个文件的路径最好在/tmp下,否则可能因为权限问题无法创建
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --monitor --host=192.168.1.102 --user=root --password=123 --log=/opt/master-slave.txt --daemonize
[root@localhost ~]# tail -f /opt/
apache-tomcat-7.0.65/ gitlab-6.6.5-0/ master-slave.txt
[root@localhost ~]# tail -f /opt/master-slave.txt
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
.......
在从库上运行监测同步延迟(也可以在命令后加上--master-server-id=101或--print-master-server-id,同上操作)
pt-heartbeat -D yjzh --table=heartbeat --monitor --user=root --password=123 --print-master-server-id
372310.00s [ 6205.17s, 1241.03s, 413.68s ] 2
372310.00s [ 12410.33s, 2482.07s, 827.36s ] 2
^C
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --user=root --password=123 --check --print-master-server-id
372310.00 2
[root@localhost ~]# pt-heartbeat -D yjzh --table=heartbeat --monitor --user=root --password=123 --log=/opt/master-slave.txt --daemonize
[root@localhost ~]# tail -f /opt/master-slave.txt
372310.00s [ 43436.17s, 8687.23s, 2895.74s ]
372310.00s [ 49641.33s, 9928.27s, 3309.42s ]
372310.00s [ 55846.50s, 11169.30s, 3723.10s ]
如何关闭上面在主库上执行的heartbeat更新进程呢?
方法一:添加参数 --stop
[root@localhost ~]# ps -ef|grep heartbeat
root 3060 1 0 22:05 ? 00:00:00 perl /usr/local/bin/pt-heartbeat -D yjzh --table=heartbeat --monitor --user=root --password=123 --log=/opt/master-slave.txt --daemonize
root 3063 1164 0 22:09 pts/0 00:00:00 grep heartbeat
[root@localhost ~]# pt-heartbeat --stop
Successfully created file /tmp/pt-heartbeat-sentinel
[root@localhost ~]# ps -ef|grep heartbeat
root 3066 1164 0 22:10 pts/0 00:00:00 grep heartbeat
这样就把在主上开启的进程杀掉了。
但是后续要继续开启后台进行的话,记住一定要先把/tmp/pt-heartbeat-sentinel 文件删除,否则启动不了
方法二:直接kill掉进程pid(推荐这种方法)
[root@localhost ~]# ps -ef|grep heartbeat
root 3084 1 0 22:16 ? 00:00:00 perl /usr/local/bin/pt-heartbeat -D yjzh --table=heartbeat --monitor --user=root --password=123 --log=/opt/master-slave.txt --daemonize
root 3089 1164 0 22:16 pts/0 00:00:00 grep heartbeat
[root@localhost ~]# kill -9 3084
[root@localhost ~]# ps -ef|grep heartbeat
root 3091 1164 0 22:16 pts/0 00:00:00 grep heartbeat
最后总结:
通过pt-heartbeart工具可以很好的弥补默认主从延迟的问题,但需要搞清楚该工具的原理。
默认的Seconds_Behind_Master值是通过将服务器当前的时间戳与二进制日志中的事件时间戳相对比得到的,所以只有在执行事件时才能报告延时。备库复制线程没有运行,也会报延迟null。
还有一种情况:大事务,一个事务更新数据长达一个小时,最后提交。这条更新将比它实际发生时间要晚一个小时才记录到二进制日志中。当备库执行这条语句时,会临时地报告备库延迟为一个小时,执行完后又很快变成0。