將舊數據轉移到另一張表/文件,或直接刪除,對線上 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,否則導出文件中文亂碼。
# 查找 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
# 打印運行超過 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
# 本地 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,直觀定位差異項。
pt-mysql-summary -- --user=root --password=zhang@123 --host=192.168.3.92
收集 SHOW VARIABLES、SHOW GLOBAL STATUS 等,生成可讀性極強的報告,適合巡檢/交接。
# 從實例讀取 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 等),給出建議值。