• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于pt-heartbeat(percona toolkit)的实例代码

mysql 搞代码 4年前 (2022-01-09) 28次浏览 已收录 0个评论
pt-heartbeat是用来监控主从延迟的一款percona工具,现在我们大部分的MySQL架构还是基于主从复制,例如MHA,MMM,keepalived等解决方案。而主从环境的话,我们很关心的就是主从延迟的问题,一般情况下我们在从库执行以下语句:
mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.16.16.35Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000016Read_Master_Log_Pos: 299786938Relay_Log_File: mxqmongodb2-relay-bin.000032Relay_Log_Pos: 299787151Relay_Master_Log_File: mysql-bin.000016Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 299786938Relay_Log_Space: 299787451Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 353306Master_UUID: 806ede0c-357e-11e7-9719-00505693235dMaster_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: 806ede0c-357e-11e7-9719-005<p style="color:transparent">本文来源gao!daima.com搞$代!码网</p>05693235d:666-330347Executed_Gtid_Set: 6a4ab82c-4029-11e7-86b0-00505693235d:1-3,806ede0c-357e-11e7-9719-00505693235d:1-330347Auto_Position: 1Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:1 row in set (0.00 sec)
就可以很明显看得到主从的状态,一般我们都会监控以下两个进程的状态确定主从延迟是否出现错误:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
对于主从延迟来说,我们可能监控最多的就是通过SMB(Seconds_Behind_Master)来监控,但是这个也并不是很靠谱的。或者我们通过监控Read_Master_Log_Pos-Exec_Master_Log_Pos的差值来看从库是否有延迟,是否和主库会有一定的延迟。但是这个也并不是很完美。首先看SMB,这个参数是怎么比较出来的呢,SMB是通过将服务器当前的时间戳与二进制日志中的事件时间戳相对比得到的,而且是会产生误报的情况。比如主库执行一个大事物,时间执行很久,从库接收以后对比时间戳发现已经延迟了,就会瞬间增大SMB的值。Read_Master_Log_Pos-Exec_Master_Log_Pos的值也并不是完全可靠。现在pt-heartbeat就能帮我们解决这个问题。
下面我们来看一下pt-heartbeat的原理:
pt-heartbeat会在master上创建一张表,按照一定的时间频率更新该表的字段,向该表写入当前的时间戳,然后对比从库的时间戳和pt-heartbeat所在机器的时间戳来判断主从延迟。
其实这里就有一个问题了,如果pt-heartbeat部署在从库的话,要保证主从机器的时间是同步的,这个一般都是系统会每天自动对时间,是可以实现的。如果pt-heartbeat部署在主库就不会有这个问题。其实个人感觉比较好的方法就是对比主从两个库的这张表的时间戳来得出延迟更为靠谱一点,当然这也要考虑到主从查询的问题。
下面看一下基本的用法:
先创建表:
[root@mxqmongodb2 bin]# ./pt-heartbeat --host=172.16.16.35 --port=3306 --user=root --password=123456 --database=test --update --create-table --daemonize

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于pt-heartbeat(percona toolkit)的实例代码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址