pt工具


percona-toolkit簡介
percona-toolkit是一組高級命令行工具的集合,用來執行各種通過手工執行非常復雜和麻煩的mysql任務和系統任務,這些任務包括:
 檢查master和slave數據的一致性
 有效地對記錄進行歸檔
 查找重復的索引
 對服務器信息進行匯總
 分析來自日志和tcpdump的查詢
 當系統出問題的時候收集重要的系統信息
這些工具主要包括開發、性能、配置、監控、復制、系統、實用六大類,作為一個優秀的DBA,里面有的工具非常有用,如果能掌握並加以靈活應用,將能極大的提高工作效率。
 
percona-toolkit工具包安裝
percona-toolkit-2.2.18-1.noarch.rpm
percona-toolkit-2.2.18.tar.gz
 
[root@mysql1 /]# yum install percona-toolkit-2.2.18-1.noarch.rpm

(一) 開發類工具
1.pt-duplicate-key-checker
功能介紹:
功能為從mysql表中找出重復的索引和外鍵,這個工具會將重復的索引和外鍵都列出來,並生成了刪除重復索引的語句,非常方便
包含比較多的選項,具體的可以通過命令pt-duplicate-key-checker --help來查看具體支持那些選項
pt-duplicate-key-checker --host=localhost --user=system --password=123456 --databases=test
 
2.pt-online-schema-change
功能介紹:
功能為在alter操作更改表結構的時候不用鎖定表,也就是說執行alter的時候不會阻塞寫和讀取操作,注意執行這個工具的時候必須做好備份,操作之前最好詳細讀一下官方文檔
工作原理是創建一個和你要執行alter操作的表一樣的空表結構,執行表結構修改,然后從原表中copy原始數據到表結構修改后的表,當數據copy完成以后就會將原表移走,用新表代替原表,
默認動作是將原表drop掉。在copy數據的過程中,任何在原表的更新操作都會更新到新表,因為這個工具在會在原表上創建觸發器,觸發器會將在原表上更新的內容更新到新表。如果表中已經定義了觸發器這個工具就不能工作了。
大表添加字段
pt-online-schema-change --user=system --password=123456 --host=localhost --alter="ADD COLUMN domain_id INT" D=test,t=t --execute
pt-online-schema-change --host='10.10.100.119' --port=3306 --user='root' --password='aSv_y8c_Bup_UqF' --charset=utf8 --alter "add COLUMN retention_flag int(11)" D=tms_production,t=waybill --execute --alter-foreign-keys-method=rebuild_constraints
 
3.pt-show-grants
功能介紹:
規范化和打印mysql權限,讓你在復制、比較mysql權限以及進行版本控制的時候更有效率!
查看指定mysql的所有用戶權限:
pt-show-grants --host='localhost' --user='system' --password='123456'
查看指定數據庫的權限:
pt-show-grants --host='localhost' --user='system' --password='123456' --database='test'
 
4.pt-upgrade
功能介紹:
在多台服務器上執行查詢,並比較有什么不同!這在升級服務器的時候非常有用,可以先安裝並導數據到新的服務器上,然后使用這個工具跑一下sql看看有什么不同,可以找出不同版本之間的差異。
pt-upgrade h='localhost' h=192.168.1.202 --user='system' --password='123456' --query="select * from mysql.user limit 5"

(二) 性能類工具
1.pt-index-usage
功能介紹:
從log文件中讀取查詢語句,並用explain分析他們是如何利用索引。完成分析之后會生成一份關於索引沒有被查詢使用過的報告。
pt-index-usage /data/mysqldata/3306/slow_query.log --host='localhost' --user='system' --password='123456'
 
2.pt-pmp
功能介紹:
為查詢程序執行聚合的GDB堆棧跟蹤,先進行堆棧跟蹤,然后將跟蹤信息匯總。
pt-pmp -p 21933
pt-pmp -b /usr/local/mysql/bin/mysqld_safe
 
3.pt-visual-explain
功能介紹:
格式化explain出來的執行計划按照tree方式輸出,方便閱讀。
pt-visual-explain a.txt
mysql -usystem -p123456 -e "explain select * from mysql.user" | pt-visual-explain

(三) 配置類工具
1.pt-config-diff
功能介紹:
比較mysql配置文件和服務器參數
pt-config-diff h=localhost h=192.168.1.202 --user='system' --password='123456'
 
2.pt-mysql-summary
功能介紹:
精細地對mysql的配置和status信息進行匯總
pt-mysql-summary --user='system' --password='123456' --host=localhost
 
3.pt-variable-advisor
功能介紹:
分析mysql的參數變量,並對可能存在的問題提出建議
有格式要求
pt-variable-advisor --user='system' --password='123456' --source-of-variables /data/mysqldata/3306/my.cnf

(四) 監控類工具
1.pt-deadlock-logger
功能介紹:
提取和記錄mysql死鎖的相關信息
pt-deadlock-logger --user='system' --password='123456' h=localhost –print
 
2.pt-fk-error-logger
功能介紹:
提取和記錄mysql外鍵錯誤信息
 
3.pt-mext
功能介紹:
並行查看SHOW GLOBAL STATUS的多個樣本的信息。
 
4.pt-query-digest
功能介紹:
分析查詢執行日志,並產生一個查詢報告
直接分析慢查詢文件:
pt-query-digest  slow.log > slow_report.log
分析最近12小時內的查詢:
pt-query-digest  --since=12h  slow.log > slow_report2.log
分析指定時間范圍內的查詢:
pt-query-digest slow.log --since '2017-01-07 09:30:00' --until '2017-01-07 10:00:00' > slow_report3.log
針對某個用戶的慢查詢
pt-query-digest --filter '($event->{user} || "") =~ m/^root/i' slow.log> slow_report5.log
查詢所有所有的全表掃描或full join的慢查詢
pt-query-digest --filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' slow.log> slow_report6.log
把查詢保存到query_review表
pt-query-digest --user=root –password=abc123 --review  h=localhost,D=test,t=query_review--create-review-table  slow.log
把查詢保存到query_history表
pt-query-digest  --user=root –password=abc123 --review  h=localhost,D=test,t=query_history--create-review-table  slow.log_0001
pt-query-digest  --user=root –password=abc123 --review  h=localhost,D=test,t=query_history--create-review-table  slow.log_0002
 
(五) 復制類工具
1.pt-heartbeat
功能介紹:
監控mysql復制延遲
 
2.pt-slave-delay
功能介紹:
設置從服務器落后於主服務器指定時間。
 
3.pt-slave-find
功能介紹:
查找和打印mysql所有從服務器復制層級關系
pt-slave-find -ubluewhale -p --host=10.9.97.224
bluewhale001
 
4.pt-slave-restart
功能介紹:
監視mysql復制錯誤,並嘗試重啟mysql復制當復制停止的時候
 
5.pt-table-checksum
功能介紹:
檢查mysql復制一致性
 
6.pt-table-sync
功能介紹:
高效同步mysql表的數據
 
7.pt-table-usage
功能介紹:
分析如何使用mysql中的表

(六) 系統類工具
1.pt-diskstats
功能介紹:
是一個對GUN/LINUX的交互式監控工具

(七) 實用類工具
1.pt-archiver
功能介紹:
將mysql數據庫中表的記錄歸檔到另外一個表或者文件,也可以直接進行記錄的刪除操作。
 
2.pt-find
功能介紹:
查找mysql表並執行指定的命令,和gnu的find命令類似
 
3.pt-kill
功能介紹:
kill掉符合指定條件mysql語句
pt-kill --log-dsn D=test,t=kill_log --create-log-table --host=192.168.1.122 --user=root --password=msds007 --port=3306 --busy-time=10 --print --kill-query --match-info "SELECT|select"  --victims all
pt-kill --log-dsn D=test,t=kill_log --create-log-table --host=192.168.1.122 --user=root --password=msds007 --port=3306 --busy-time=10 --print --kill-query --match-info "SELECT|select"
pt-kill --log-dsn D=test,t=kill_log --create-log-table --host=192.168.1.122 --user=root --password=msds007 --port=3306 --busy-time=10 --print --kill --match-info "SELECT|select"

pt-kill --no-version-check --host 10.10.208.34 --port 3306 --user 'root' --password 'XtVha6_SdWYNxGdY' --charset utf8 --match-command Query --match-user us_rwx --busy-time 3 --kill --victims all --interval 10 --print
pt-kill --no-version-check --host 10.10.208.182 --port 3306 --user 'root' --password 'n4ut-A-b9YRkrOBg' --charset utf8 --match-command Query --match-user us_rwx --busy-time 3 --kill --victims all --interval 10 --print

pt-align 對文件格式進行格式化輸出
pt-fifo-split 將文件分割成多個數據塊(chunks),從而控制每次傳輸到mysql服務器的數據量大小
pt-fingerprint 用於生成查詢指紋。主要將將sql查詢生成queryID,pt-query-digest中的ID即是通過此工具來完成的。類似於Oracle中的SQL_ID,涉及綁定變量,字面量等
pt-ioprofile 用於分析查看mysql的真實IO情況
pt-stalk 出現問題的時候收集mysql的用於診斷的數據
pt-summary 打印出來的信息包括:CPU、內存、硬盤、網卡等信息,還包括文件系統、磁盤調度和隊列大小、LVM、RAID、網絡鏈接信息、netstat 的統計,以及前10的負載占用信息和vmstat信息。
 
 
利用percona-toolkit工具檢查MySQL數據庫主從一致性及修復
一、pt-table-checksum檢查主從庫數據的一致性
pt-table-checksum在MASTER上校驗指定庫、表,將結果存在一個庫表里,復制進程將檢驗sql傳遞到slave上再執行一次。
通過比較M/S的檢驗值確定數據是否一致。利用主從復制做檢驗,不需要在檢驗期間對主從數據庫同時鎖表,可以控制校驗的數據和速度,不影響到正常服務。
主庫執行
(system@localhost) [(none)]> set global binlog_format=statement;
Query OK, 0 rows affected (0.00 sec)
 
[root@mysql1 ~]# pt-table-checksum --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock
主庫
表要有主鍵
(system@localhost) [test]> select * from t;
+------+
| i    |
+------+
|   16 |
|   17 |
|   18 |
|   19 |
|   20 |
+------+
5 rows in set (0.00 sec)
 
從庫1
(system@localhost) [test]> select * from t;
+------+
| i    |
+------+
|   16 |
|   19 |
|   20 |
+------+
3 rows in set (0.00 sec)
 
從庫2
(system@localhost) [test]> select * from t;
+------+
| i    |
+------+
|   16 |
|   17 |
|   18 |
+------+
3 rows in set (0.00 sec)
 
#!/bin/bash
NUM=$(/usr/bin/pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --databases=huanqiu  h=192.168.1.101,u=root,p=123456,P=3306|awk -F" " '{print $3}'|sed -n '2p')
if [ $NUM -eq 1 ];then
  /usr/bin/pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --print
  /usr/bin/pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --execute
else
  echo "data is ok"
fi
 
pt-table-checksum --recursion-method="processlist" --no-check-binlog-format --replicate=test.checksums --databases=huanqiu  h=192.168.1.101,u=root,p=123456,P=3306
 

二、pt-table-sync修復從庫不一致的數據
主庫執行
grant all on *.* to 'system'@'%' identified by '123456';
(system@localhost) [test]> create table user(id int primary key,name varchar(10));
(system@localhost) [test]> insert into user values (1,'abc');
(system@localhost) [test]> insert into user values (2,'def');
(system@localhost) [test]> insert into user values (3,'ghi');
 
pt-table-sync: 高效的同步MySQL表之間的數據,他可以做單向和雙向同步的表數據。他可以同步單個表,也可以同步整個庫。它不同步表結構、索引、或任何其他模式對象。所以在修復一致性之前需要保證他們表存在。
從庫執行,沒有不一致的數據
pt-table-sync --print --sync-to-master h=192.168.1.202,u=system,p=123456,P=3306,D=test,t=user
pt-table-sync --print --sync-to-master h=192.168.1.203,u=system,p=123456,P=3306,D=test,t=user
 
mysql2添加一條記錄
(system@localhost) [test]> insert into user values (4,'jkl');
mysql3刪除一條記錄
(system@localhost) [test]> delete from user where id=2;
 
從庫執行,將不一致的數據打印出來
pt-table-sync --print --sync-to-master h=192.168.1.202,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
pt-table-sync --print --sync-to-master h=192.168.1.203,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
 
從庫執行,修復不一致的數據
pt-table-sync --execute --sync-to-master h=192.168.1.202,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
pt-table-sync --execute --sync-to-master h=192.168.1.203,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
 
從庫執行,沒有不一致的數據
pt-table-sync --print --sync-to-master h=192.168.1.202,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
pt-table-sync --print --sync-to-master h=192.168.1.203,u=system,p=123456,P=3306,D=test,t=user --charset=utf8
pt-table-sync --print --sync-to-master h=192.168.1.202,u=system,p=123456,P=3306 --databases test --charset=utf8
pt-table-sync --print --sync-to-master h=192.168.1.203,u=system,p=123456,P=3306 --databases test --charset=utf8
 
主庫執行,添加表
pt-heartbeat --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock -D test --master-server-id=201 --create-table --update
在后台持續更新主庫上的heartbeat
pt-heartbeat --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock -D test --master-server-id=201 --update &
 
持續查看從庫上的延遲情況
pt-heartbeat --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock -D test --master-server-id=201 --monitor --print-master-server-id
單次查看從庫上的延遲情況
pt-heartbeat --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock -D test --master-server-id=201 --check
 
使用守護進程監控從庫並輸出日志
pt-heartbeat --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock -D test --master-server-id=201 --monitor --print-master-server-id --daemonize --log=/tmp/slave-lag.log
 
#下面是定期過濾--log文件中最大值的腳本,加入監控調用即可:
#!/bin/bash
cat /tmp/b.txt > /tmp/b_tmp.txt
echo > /tmp/b.txt
max_time=`cat /tmp/b_tmp.txt |grep -v '^$' |awk '{print $1}' |sort -k1nr |head -1`
echo "$max_time"

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

打印MySQL復制的層次結構
pt-slave-find -h 192.168.1.201 -u system -p 123456 -P 3306

分析慢查詢日志
pt-query-digest --user='system' --password='123456' /data/mysqldata/3306/slow_query.log

分析慢查詢中索引和表使用情況
pt-index-usage --h localhost --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock /data/mysqldata/3306/slow_query.log
pt-table-usage --h localhost --user='system' --password='123456' -S /data/mysqldata/3306/mysql.sock /data/mysqldata/3306/slow_query.log
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM