1. 程式人生 > >Percona Toolkit使用之pt-heartbeat

Percona Toolkit使用之pt-heartbeat

     pt-heartbeat的功能是監控MySQL複製延遲。

     用法如下:

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

     pt-heartbeat測算MySQL或者PostgreSQL伺服器上的複製延遲。你可以使用它來更新master或者監控replica。如若可能,pt-heartbeat從你的.my.cnf配置檔案讀取MySQL連線引數。

     ①啟動守護程序來更新master上的`test`.`heartbeat`庫表:

pt-heartbeat -D test --update -h master-server --daemonize

     ②監控slave上的複製延遲(--dbi-driver Pg表示PostgreSQL):

pt-heartbeat -D test --monitor -h slave-server
pt-heartbeat -D test --monitor -h slave-server --dbi-driver Pg

     ③檢查slave延遲一次然後退出(使用表示MySQL連線訪問的DSN選項來指定slave伺服器):

pt-heartbeat -D test --check h=slave-server

     pt-heartbeat是一個由兩部分組成的MySQL和PostgreSQL複製延遲監控系統,通過察看具體的被複制的資料來測算延遲,這避免了對不可信的複製機制本身的依賴,例如MySQL的“ SHOW SLAVE STATUS ”。

     pt-heartbeat的第一個部分是“ --update ”例項,每“ --interval ”秒連線到master然後更新一個時間戳(“ 心跳記錄 ”)。由於heartbeat表可能包含來自多個master的記錄,使用伺服器ID(@@server_id)來唯一標識記錄。

     pt-heartbeat的第二個部分是“ --monitor ”或者“ --check ”例項,連線到slave檢測從其直接的或者“ --master-server-id ”指定的master複製而來的heartbeat記錄,然後計算與當前系統時間的差異。如果介於該master和slave之間的複製存在延遲或者故障,計算而來的差值將會大於零,如果指定“ --monitor ”選項,差值還會潛在增大。

     使用pt-heartbeat之前必須在master上要麼手動要麼使用“ --create-table ”選項建立heartbeat表。對於MySQL而言,推薦使用MEMORY儲存引擎,當然這並不是必須的。heartbeat表必須包含一條心跳記錄。如若沒有預設情況下會插入一條。該特性可以通過“ --[no]insert-heartbeat-row ”選項禁用,以防資料庫使用者沒有INSERT操作許可權。heartbeat表的結構具體如下。

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
);

     pt-heartbeat僅僅依賴於將被複制到slave的心跳記錄,因此不管是什麼複製機制(內建複製、例如Continuent Tungsten的複製系統等等),它都能工作。它作用於複製層次的任何深度。例如,它可以可信地報告一個slave同它的master的master的master延遲有多久。而且即便複製被停止,它也會繼續工作和(準確地)彙報slave越來越落後於master。

     pt-heartbeat最大可以精確到0.01秒。master和slave上的時鐘必須通過NTP完全同步。預設情況下,“ --update ”檢查發生於每一秒的開始(e.g 00:01)而“ --monitor ”檢查發生於秒與秒的中間(e.g 00:01.5)。只要伺服器間的時鐘完全同步並且複製事件在半秒以內傳送,pt-heartbeat就會給出零秒的延遲結果。

     如果連線報錯,pt-heartbeat會嘗試重新連線。但如果首次啟動時無法獲取連線,則不會重試。

     “ --dbi-driver ”選項允許你也可以使用pt-heartbeat來監控PostgreSQL。據說對於Slony-1複製工作良好。

     以下為個人本地環境的測試資料。

     首先是master啟動“ --update ”例項來寫入和更新心跳。

[email protected]:~# pt-heartbeat -h192.168.112.129 -P3306 -uroot -p123456 -Dplayer --interval=11 --create-table --update --daemonize
[email protected]:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT * FROM `player`.`heartbeat`\G
*************************** 1. row ***************************
                   ts: 2018-04-02T23:33:05.002410
            server_id: 1024
                 file: master-bin.000010
             position: 8301
relay_master_log_file: NULL
  exec_master_log_pos: NULL
1 row in set (0.00 sec)

mysql> 

     slave啟動“ --monitor ”監控複製延遲,這裡定義每11秒檢查輸出一次計算結果。測試中是存在3秒的延遲,但這其實是“ 假 ”的。因為我在master的“ --update ”和slave的“ --monitor ”都指定了“ --interval=11 ”。每一行具體延遲時間後面的中括號中給出了三個時間段的延遲均值,預設是1分鐘、5分鐘和15分鐘,通過引數選項“ --frames ”控制。

[email protected]:~# pt-heartbeat -h192.168.112.128 -P3306 -uroot -p123456 -Dplayer --interval=11 --monitor

# A software update is available:
3.00s [  0.05s,  0.01s,  0.00s ]
3.00s [  0.10s,  0.02s,  0.01s ]
3.00s [  0.15s,  0.03s,  0.01s ]

     檢查slave延遲1次然後退出。這裡通過“ CHANGE MASTER TO MASTER_DELAY = N ”來設定了一下複製延遲(https://dev.mysql.com/doc/refman/5.7/en/replication-delayed.html),然後會發現延遲滯後了很多。但是就不會再額外輸出區間段的延遲均值了。

[email protected]:~# mysql -h192.168.112.128 -P3306 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.18 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE MASTER TO MASTER_DELAY = 36;
Query OK, 0 rows affected (0.01 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> \q
Bye
[email protected]:~# pt-heartbeat -h192.168.112.128 -P3306 -uroot -p123456 -Dplayer --check
44.00
[email protected]:~# 

     “ --stop ”master上的“ --update ”守護程序取消監控。

[email protected]:~# ps aux | grep heartbeat
root       5198  0.0  1.0  97060 21576 ?        Ss   00:06   0:00 perl /usr/bin/pt-heartbeat -h192.168.112.129 -P3306 -uroot -p123456 -Dplayer --interval=11 --create-table --update --daemonize
root       5208  0.0  0.0  15984   972 pts/8    S+   00:06   0:00 grep --color=auto heartbeat
[email protected]:~# ls /tmp/ | grep ^pt
[email protected]:~# pt-heartbeat -h192.168.112.129 -P3306 -uroot -p123456 -Dplayer --sentinel=/tmp/pt-heartbeat-sentinel --stop
Successfully created file /tmp/pt-heartbeat-sentinel
[email protected]:~# ps aux | grep heartbeat
root       5215  0.0  0.0  15984  1016 pts/8    S+   00:07   0:00 grep --color=auto heartbeat
[email protected]:~# ls /tmp/ | grep ^pt
pt-heartbeat-sentinel
[email protected]:~# 

參考: