1. 程式人生 > >pt-heartbeat

pt-heartbeat

epo 輸入 rdl 記錄 tmp heartbeat memory 參數 ids

1、pt-heartbeat的作用

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.

pt-heartbeat is a two-part MySQL and PostgreSQL replication delay monitoring system that measures delay by looking at actual replicated data. This avoids reliance on the replication mechanism itself, which is unreliable. (For example, SHOW SLAVE STATUS on MySQL).

2、pt-heartbeat的原理

The first part is an --update instance of pt-heartbeat that connects to a master and updates a timestamp (“heartbeat record”) every --interval seconds. Since the heartbeat table may contain records from multiple masters (see “MULTI-SLAVE HIERARCHY”), the server’s ID (@@server_id) is used to identify records.

主庫上存在一個用於檢查延遲的表heartbeat,可手動或自動創建

pt-heartbeat使用--update參數連接到主庫上並持續(根據設定的--interval參數)使用一個時間戳更新到表heartbeat

The second part is a --monitor or --check instance of pt-heartbeat that connects to a slave, examines the replicated heartbeat record from its immediate master or the specified --master-server-id, and computes the difference from the current system time. If replication between the slave and the master is delayed or broken, the computed difference will be greater than zero and otentially increase if --monitor is specified.

pt-heartbeat使用--monitor 或--check連接到從庫,檢查從主庫同步過來的時間戳,並與當前系統時間戳進行比對產生一個差值,

該值則用於判斷延遲。(註,前提條件是主庫與從庫應保持時間同步)

You must either manually create the heartbeat table on the master or use --create-table. See --create-table for the proper heartbeat table structure. The MEMORY storage engine is suggested, but not re-quired of course, for MySQL.

The heartbeat table must contain a heartbeat row. By default, a heartbeat row is inserted if it doesn’t exist. This feature can be disabled with the --[no]insert-heartbeat-row option in case the database user does not have INSERT privileges.

pt-heartbeat depends only on the heartbeat record being replicated to the slave, so it works regardless of the replication mechanism (built-in replication, a system such as Continuent Tungsten, etc). It works at any depth in the replication hierarchy; for example, it will reliably report how far a slave lags its master’s master’s master. And if replication is stopped, it will continue to work and report (accurately!) that the slave is falling further and further behind the master.

pt-heartbeat has a maximum resolution of 0.01 second. The clocks on the master and slave servers must be closely synchronized via NTP. By default, --update checks happen on the edge of the second (e.g. 00:01) and --monitor checks happen halfway between seconds (e.g. 00:01.5). As long as the servers’ clocks are closely synchronized and replication events are propagating in less than half a second, pt-heartbeat will report zero seconds of delay.

pt-heartbeat will try to reconnect if the connection has an error, but will not retry if it can’t get a connection when it first starts.

The --dbi-driver option lets you use pt-heartbeat to monitor PostgreSQL as well. It is reported to work well with Slony-1 replication.

3、獲取pt-heartbeat幫助信息

a、獲取幫助信息

[[email protected] ~]# pt-heartbeat #直接輸入pt-heartbeat可獲得一個簡要描述,使用pt-heartbeat --help獲得一個完整幫助信息

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

Errors in command-line arguments:

* Specify at least one of --stop, --update, --monitor or --check

* --database must be specified

4、演示使用pt-heartbeat

[python] view plain copy print?在CODE上查看代碼片派生到我的代碼片

a、首先添加表

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --create-table --update

MASTER> select * from heartbeat;

+----------------------------+-----------+------------------+-----------+-----------------------+---------------------+

| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |

+----------------------------+-----------+------------------+-----------+-----------------------+---------------------+

| 2014-12-01T09:48:14.003020 | 11 | mysql-bin.000390 | 677136957 | mysql-bin.000179 | 120 |

+----------------------------+-----------+------------------+-----------+-----------------------+---------------------+

b、更新主庫上的heartbeat

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --update &

[1] 31249

c、從庫上監控延遲

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --monitor --print-master-server-id

1.00s [ 0.02s, 0.00s, 0.00s ] 11 #實時延遲,1分鐘延遲,5分鐘延遲,15分鐘延遲

1.00s [ 0.03s, 0.01s, 0.00s ] 11

1.00s [ 0.05s, 0.01s, 0.00s ] 11

1.00s [ 0.07s, 0.01s, 0.00s ] 11

1.00s [ 0.08s, 0.02s, 0.01s ] 11

1.00s [ 0.10s, 0.02s, 0.01s ] 11

1.00s [ 0.12s, 0.02s, 0.01s ] 11

1.00s [ 0.13s, 0.03s, 0.01s ] 11

d、其他操作示例

#將主庫上的update使用守護進程方式調度

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --update --daemonize

#修改主庫上的更新間隔為2s

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --update --daemonize --interval=2

#停止主庫上的pt-heartbeat守護進程

[[email protected] ~]# pt-heartbeat --stop

Successfully created file /tmp/pt-heartbeat-sentinel

[[email protected] ~]# rm -rf /tmp/pt-heartbeat-sentinel

#單次查看從庫上的延遲情況

[[email protected] ~]$ pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --check

1.00

#使用守護進程監控從庫並輸出日誌

[[email protected] ~]# pt-heartbeat --user=root --password=xxx -S /tmp/mysql.sock -D test --master-server-id=11 --monitor --print-master-server-id --daemonize --log=/tmp/slave-lag.log

5、常用參數: 註意:需要指定的參數至少有 --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。

pt-heartbeat