首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Percona Toolkit 核心工具實戰:pt-archiver、pt-find、pt-kill、pt-config-diff 等高頻命令詳解

     編輯:MySQL 資料庫相關知識

1. pt-archiver:歸檔/刪除歷史數據神器

將舊數據轉移到另一張表/文件,或直接刪除,對線上 OLTP 影響極小(分批提交)。

常用參數

  • --source:源 DSN(h=host,D=db,t=table)
  • --dest:目標 DSN(可為文件 --file)
  • --where:篩選條件
  • --purge:僅刪除,不歸檔
  • --commit-each:每行提交(安全)
  • --limit:每批處理行數

實戰範例

# 歸檔 id<=100000 到另一庫同名表,並導出文件
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 \
  --dest h=192.168.3.92,D=sanmao,t=oss_log --file '/var/log/oss_log_archive_20120605.log' \
  --where "id<=100000" --commit-each

# 僅歸檔到本地文件
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 \
  --file '/var/log/oss_log_archive_20120607.log' --where "id<=160000" --commit-each

# 直接刪除
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 \
  --purge --where 'id<=167050'

避坑:字符集為 utf8 時,需在 my.cnf [client] 設 default-character-set=utf8,否則導出文件中文亂碼。

2. pt-find:類 find 命令查表並執行動作

實戰範例

# 查找 1 天前創建的 InnoDB 表
pt-find --ctime +1 --host=192.168.3.135 --engine InnoDB --user=root --password=zhang@123

# 查找 1 天前修改、庫名匹配 hostsops、引擎 MyISAM 的表,轉為 InnoDB
pt-find --mtime +1 --dblike hostsops --engine MyISAM --host=192.168.3.135 --user=root --password=zhang@123 \
  --exec "ALTER TABLE %D.%N ENGINE=InnoDB"

# 查找 aaa、zhang 庫中的空表並刪除
pt-find --empty aaa zhang --host=192.168.3.135 --user=root --password=zhang@123 --exec-plus "DROP TABLE %s"

# 查找超過 100MB 的表
pt-find --tablesize +100M --host=192.168.3.135 --user=root --password=zhang@123

3. pt-kill:殺掉長時間運行/阻塞的查詢

# 打印運行超過 60s 的語句
pt-kill --busy-time 60 --print --host=192.168.3.135 --user=root --password=zhang@123

# 直接 Kill 掉
pt-kill --busy-time 60 --kill --host=192.168.3.135 --user=root --password=zhang@123

# 從文件分析(離線模式)
mysql -uroot -pzhang@123 -h192.168.3.135 -e "show processlist" > processlist.txt
pt-kill --test-matching processlist.txt --busy-time 60 --print

4. pt-config-diff:配置文件/實例參數差異對比

# 本地 vs 遠程實例
pt-config-diff h=localhost h=192.168.3.92 --user=root --password=zhang@123

# 本地文件 vs 遠程實例
pt-config-diff /etc/my.cnf h=192.168.3.92 --user=root --password=zhang@123

# 兩個本地配置文件
pt-config-diff /usr/local/mysql/share/mysql/my-large.cnf /usr/local/mysql/share/mysql/my-medium.cnf

輸出格式:Variable Source1 Source2,直觀定位差異項。

5. pt-mysql-summary:實例配置與狀態一鍵匯總

pt-mysql-summary -- --user=root --password=zhang@123 --host=192.168.3.92

收集 SHOW VARIABLES、SHOW GLOBAL STATUS 等,生成可讀性極強的報告,適合巡檢/交接。

6. pt-variable-advisor:參數診斷顧問

# 從實例讀取
pt-variable-advisor --user=root --password=zhang@123 localhost

# 從配置文件讀取
pt-variable-advisor --user=root --password=zhang@123 --source-of-variables my.cnf

基於預置規則檢查配置錯誤(如 innodb_buffer_pool_size 過小、sync_binlog!=1 等),給出建議值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved