Percona Toolkit 工具包完整介绍与使用示例
一、概述
Percona Toolkit (简称 PT 工具)是由 Percona 公司开发的一套开源 MySQL/MariaDB 命令行工具集 ,包含 30+ 个实用工具,用 Perl 编写,专为 DBA 设计,用于执行各种复杂的数据库运维、监控、分析和优化任务。
核心能力:
- ✅ 分析慢查询日志,定位性能瓶颈
- ✅ 在线 DDL(大表结构变更不锁表)
- ✅ 主从数据一致性校验与修复
- ✅ 复制延迟精确监控
- ✅ 重复索引检测
- ✅ 数据归档与清理
- ✅ 系统/数据库信息汇总
- ✅ 异常自动诊断采集
二、安装
2.1 安装依赖
bash
# RHEL/CentOS
yum install -y perl-DBI perl-DBD-MySQL perl-Time-HiRes \
perl-IO-Socket-SSL perl-TermReadKey perl-Digest-MD5
# Debian/Ubuntu
apt-get install -y libdbi-perl libdbd-mysql-perl libtime-hires-perl \
libio-socket-ssl-perl libterm-readkey-perl
2.2 安装 Percona Toolkit
bash
# 方式一:RPM 安装(RHEL/CentOS)
wget https://downloads.percona.com/downloads/percona-toolkit/3.6.0/binary/redhat/8/x86_64/percona-toolkit-3.6.0-1.el8.x86_64.rpm
rpm -ivh percona-toolkit-3.6.0-1.el8.x86_64.rpm
# 方式二:DEB 安装(Debian/Ubuntu)
wget https://downloads.percona.com/downloads/percona-toolkit/3.6.0/binary/debian/jammy/x86_64/percona-toolkit_3.6.0-1.jammy_amd64.deb
dpkg -i percona-toolkit_3.6.0-1.jammy_amd64.deb
# 方式三:YUM/APT 仓库
# Percona 官方仓库配置后:
yum install -y percona-toolkit
# 或
apt-get install -y percona-toolkit
# 方式四:源码安装
wget https://downloads.percona.com/downloads/percona-toolkit/3.6.0/source/tarball/percona-toolkit-3.6.0.tar.gz
tar xzf percona-toolkit-3.6.0.tar.gz
cd percona-toolkit-3.6.0
perl Makefile.PL
make
make test
make install
2.3 验证安装
bash
pt-summary --version
# 输出: pt-summary 3.6.0
三、工具分类总览
| 分类 | 工具 | 功能 |
|---|---|---|
| 性能分析 | pt-query-digest |
慢查询日志分析 |
pt-index-usage |
索引使用分析 | |
pt-visual-explain |
可视化执行计划 | |
| 开发运维 | pt-online-schema-change |
在线 DDL |
pt-duplicate-key-checker |
重复索引检测 | |
pt-archiver |
数据归档/清理 | |
| 复制相关 | pt-table-checksum |
主从数据校验 |
pt-table-sync |
主从数据同步修复 | |
pt-heartbeat |
复制延迟监控 | |
| 信息汇总 | pt-summary |
系统信息汇总 |
pt-mysql-summary |
MySQL 状态汇总 | |
pt-config-diff |
配置差异对比 | |
| 实用工具 | pt-kill |
自动杀连接 |
pt-stalk |
异常自动采集 | |
pt-show-grants |
权限导出 | |
pt-pmp |
进程堆栈分析 | |
pt-diskstats |
磁盘 I/O 分析 |
四、常用工具完整使用示例
4.1 pt-query-digest --- 慢查询分析(⭐最常用)
功能: 分析慢查询日志/普通日志/二进制日志/tcpdump,生成查询性能统计报告。
bash
# ========== 基础用法 ==========
# 分析慢查询日志
pt-query-digest /var/log/mysql/slow-query.log
# 分析最近 1 小时的慢查询
pt-query-digest --since '1h' /var/log/mysql/slow-query.log
# 分析指定时间范围
pt-query-digest --since '2026-07-29 00:00:00' \
--until '2026-07-29 12:00:00' \
/var/log/mysql/slow-query.log
# ========== 过滤与排序 ==========
# 只看 SELECT 语句,按总耗时排序,取 Top 10
pt-query-digest --filter '$event->{cmd} eq "Query" && $event->{arg} =~ /^SELECT/i' \
--order-by Query_time:sum \
--limit 10 \
/var/log/mysql/slow-query.log
# 只看某个数据库的慢查询
pt-query-digest --filter '$event->{db} eq "mydb"' \
/var/log/mysql/slow-query.log
# 只看执行时间超过 5 秒的查询
pt-query-digest --filter '$event->{Query_time} > 5' \
/var/log/mysql/slow-query.log
# ========== 输出到文件/数据库 ==========
# 输出报告到文件
pt-query-digest /var/log/mysql/slow-query.log > /tmp/slow_report.txt
# 将分析结果存入 MySQL 表(便于后续查询)
pt-query-digest --review h=127.0.01,u=root,p=pass,D=percona,t=query_review \
--history h=127.0.0.1,u=root,p=pass,D=percona,t=query_history \
/var/log/mysql/slow-query.log
# ========== 分析二进制日志 ==========
# 先用 mysqlbinlog 转换,再分析
mysqlbinlog /var/lib/mysql/binlog.000001 | \
pt-query-digest --type binlog -
# ========== 分析 tcpdump 抓包 ==========
pt-query-digest --type tcpdump /tmp/mysql_capture.tcpdump
# ========== 分析 PROCESSLIST ==========
# 先导出 processlist
mysql -uroot -p -e "SELECT * FROM information_schema.processlist" > /tmp/processlist.txt
pt-query-digest --type processlist /tmp/processlist.txt
输出报告解读:
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 12 1200
# Exec time 35 350s 100ms 12s 292ms 1s 800ms 200ms
# Lock time 8 2s 0 50ms 2ms 3ms 5ms 1ms
# Rows sent 20 50000 0 5000 42 100 200 10
# Rows examine 15 5000000 0 500000 4167 10000 20000 1000
4.2 pt-online-schema-change --- 在线 DDL(⭐核心工具)
功能: 在不锁表的情况下对大表执行 ALTER TABLE 操作(加字段、加索引、改字段类型等)。
原理: 创建新表 → 触发器同步增量 → 分批拷贝数据 → RENAME 交换表名。
bash
# ========== 添加字段 ==========
pt-online-schema-change \
--alter "ADD COLUMN email VARCHAR(255) DEFAULT NULL AFTER name" \
--host=127.0.0.1 \
--port=3306 \
--user=root \
--password=yourpass \
D=mydb,t=users \
--execute
# ========== 添加索引 ==========
pt-online-schema-change \
--alter "ADD INDEX idx_created_at (created_at)" \
h=127.0.0.1,P=3306,u=root,p=yourpass,D=mydb,t=orders \
--execute
# ========== 修改字段类型 ==========
pt-online-schema-change \
--alter "MODIFY COLUMN status TINYINT NOT NULL DEFAULT 0" \
h=127.0.0.1,u=root,p=yourpass,D=mydb,t=orders \
--execute
# ========== 删除字段 ==========
pt-online-schema-change \
--alter "DROP COLUMN old_field" \
h=127.0.0.1,u=root,p=yourpass,D=mydb,t=users \
--execute
# ========== 安全选项(生产环境推荐) ==========
pt-online-schema-change \
--alter "ADD INDEX idx_user_id (user_id)" \
h=127.0.0.1,u=root,p=yourpass,D=mydb,t=orders \
--chunk-size=1000 \ # 每批处理 1000 行
--max-lag=1s \ # 从库延迟超过 1s 则暂停
--check-slave-lag=slave1 \ # 监控的从库地址
--critical-load="Threads_running=100" \ # 临界负载,超过则中止
--max-load="Threads_running=50" \ # 最大负载,超过则等待
--no-drop-old-table \ # 不自动删除旧表(保留备份)
--dry-run # 先试运行,不真正执行
# 确认无误后去掉 --dry-run,加上 --execute 真正执行
pt-online-schema-change \
--alter "ADD INDEX idx_user_id (user_id)" \
h=127.0.0.1,u=root,p=yourpass,D=mydb,t=orders \
--chunk-size=1000 \
--max-lag=1s \
--no-drop-old-table \
--execute
# ========== 指定从库检查 ==========
pt-online-schema-change \
--alter "ADD COLUMN remark TEXT" \
h=master_host,u=root,p=pass,D=mydb,t=big_table \
--check-slave-lag h=slave_host,u=repl,p=replpass \
--execute
⚠️ 注意事项:
4.3 pt-table-checksum --- 主从数据一致性校验
功能: 在主库上对表进行 CRC32 校验,将结果写入 percona.checksums 表,从库同步后对比校验值。
bash
# ========== 基础校验(校验所有库所有表) ==========
pt-table-checksum \
--host=127.0.0.1 \
--user=root \
--password=yourpass \
--port=3306 \
--no-check-binlog-format \
--recursion-method=processlist
# ========== 校验指定库表 ==========
pt-table-checksum \
h=127.0.0.1,u=root,p=yourpass \
--databases=mydb \
--tables=users,orders \
--no-check-binlog-format
# ========== 指定从库 ==========
pt-table-checksum \
h=127.0.0.1,u=root,p=yourpass \
--no-check-binlog-format \
--recursion-method dsn=h=slave_host,u=repl,p=replpass
# ========== 查看校验结果 ==========
# 在主库执行:
mysql -uroot -p -e "
SELECT db, tbl, chunk, chunk_index,
lower_boundary, upper_boundary,
this_crc, master_crc,
this_cnt, master_cnt
FROM percona.checksums
WHERE master_cnt <> this_cnt
OR master_crc <> this_crc;
"
# ========== 常用选项 ==========
pt-table-checksum \
h=127.0.0.1,u=root,p=yourpass \
--no-check-binlog-format \
--chunk-size=5000 \ # 每块 5000 行
--max-lag=2s \ # 从库延迟超过 2s 暂停
--run-time=3600 \ # 最长运行 1 小时
--set-vars innodb_lock_wait_timeout=10 \
--databases=mydb
4.4 pt-table-sync --- 主从数据同步修复
功能: 当 pt-table-checksum 发现数据不一致后,用此工具修复。
bash
# ========== 修复指定表(先打印 SQL,不执行) ==========
pt-table-sync \
--sync-to-master \
h=slave_host,u=root,p=yourpass \
--databases=mydb \
--tables=users \
--print
# ========== 确认 SQL 无误后执行修复 ==========
pt-table-sync \
--sync-to-master \
h=slave_host,u=root,p=yourpass \
--databases=mydb \
--tables=users \
--execute
# ========== 配合 checksum 结果修复 ==========
pt-table-sync \
--sync-to-master \
h=slave_host,u=root,p=yourpass \
--replicate=percona.checksums \
--execute
# ========== 双向对比修复(非主从场景) ==========
pt-table-sync \
--execute \
h=server1,u=root,p=pass,D=mydb,t=users \
h=server2,u=root,p=pass,D=mydb,t=users
⚠️ 注意: pt-table-sync 会直接修改数据,务必先用 **--print** 预览!
4.5 pt-heartbeat --- 复制延迟精确监控
功能: 通过在主库定期写入心跳表,在从库读取计算延迟,精度可达 0.01 秒 (比 SHOW SLAVE STATUS 的秒级精度更高)。
bash
# ========== 在主库启动心跳守护进程 ==========
pt-heartbeat \
--host=master_host \
--user=root \
--password=yourpass \
--database=percona \
--table=heartbeat \
--create-table \
--interval=0.5 \
--daemonize \
--pid=/tmp/pt-heartbeat.pid \
--log=/tmp/pt-heartbeat.log
# ========== 在从库监控延迟 ==========
# 持续监控(每秒输出)
pt-heartbeat \
--host=slave_host \
--user=root \
--password=yourpass \
--database=percona \
--table=heartbeat \
--monitor
# 只检查一次延迟
pt-heartbeat \
--host=slave_host \
--user=root \
--password=yourpass \
--database=percona \
--table=heartbeat \
--check
# 输出到文件(配合监控系统)
pt-heartbeat \
--host=slave_host \
--user=root \
--password=yourpass \
--database=percona \
--table=heartbeat \
--monitor \
--log=/tmp/replication_lag.log &
# ========== 停止心跳 ==========
pt-heartbeat --stop --pid=/tmp/pt-heartbeat.pid
输出示例:
0.00s [ 0.00s, 0.00s, 0.00s ]
0.50s [ 0.25s, 0.13s, 0.06s ]
0.00s [ 0.17s, 0.11s, 0.06s ]
含义:当前延迟 1分钟平均, 5分钟平均, 15分钟平均
4.6 pt-duplicate-key-checker --- 重复索引检测
功能: 检测表中的重复索引和冗余外键,并生成删除语句。
bash
# ========== 检查所有库 ==========
pt-duplicate-key-checker \
--host=127.0.0.1 \
--user=root \
--password=yourpass
# ========== 检查指定库 ==========
pt-duplicate-key-checker \
h=127.0.0.1,u=root,p=yourpass \
--databases=mydb
# ========== 检查指定表 ==========
pt-duplicate-key-checker \
h=127.0.0.1,u=root,p=yourpass \
--databases=mydb \
--tables=users
# ========== 输出到文件 ==========
pt-duplicate-key-checker \
h=127.0.0.1,u=root,p=yourpass \
> /tmp/duplicate_keys.txt
输出示例:
# ########################################################################
# mydb.users
# ########################################################################
# idx_name_status is a duplicate of idx_name
# Key definitions:
# `idx_name_status` (`name`, `status`),
# `idx_name` (`name`)
# Column types:
# `name` varchar(100) not null
# `status` tinyint(1) not null default '0'
# To remove this duplicate index, execute:
ALTER TABLE `mydb`.`users` DROP INDEX `idx_name`;
4.7 pt-archiver --- 数据归档/清理
功能: 高效地归档或删除大表中的历史数据,支持分批操作,避免长事务锁表。
bash
# ========== 归档数据到另一张表 ==========
pt-archiver \
--source h=127.0.0.1,u=root,p=pass,D=mydb,t=orders \
--dest h=127.0.0.1,u=root,p=pass,D=mydb_archive,t=orders \
--where "created_at < '2025-01-01'" \
--limit=1000 \
--commit-each \
--progress=5000 \
--statistics
# ========== 归档到文件 ==========
pt-archiver \
--source h=127.0.0.1,u=root,p=pass,D=mydb,t=logs \
--file /tmp/archive_logs_%Y%m%d.csv \
--where "log_time < '2025-06-01'" \
--limit=5000 \
--commit-each \
--output-format=csv
# ========== 直接删除过期数据 ==========
pt-archiver \
--source h=127.0.0.1,u=root,p=pass,D=mydb,t=sessions \
--where "expire_time < NOW()" \
--limit=1000 \
--commit-each \
--purge \
--progress=10000 \
--statistics
# ========== 试运行(不真正操作) ==========
pt-archiver \
--source h=127.0.0.1,u=root,p=pass,D=mydb,t=orders \
--where "created_at < '2025-01-01'" \
--limit=1000 \
--dry-run
# ========== 控制从库延迟 ==========
pt-archiver \
--source h=127.0.0.1,u=root,p=pass,D=mydb,t=orders \
--dest h=127.0.0.1,u=root,p=pass,D=mydb_archive,t=orders \
--where "created_at < '2025-01-01'" \
--limit=500 \
--commit-each \
--check-slave-lag h=slave_host,u=repl,p=replpass \
--max-lag=2s \
--sleep=0.5
4.8 pt-kill --- 自动杀连接
功能: 自动检测并杀死符合条件的连接(如空闲连接、慢查询、特定用户等)。
bash
# ========== 杀死空闲超过 60 秒的连接 ==========
pt-kill \
--host=127.0.0.1 \
--user=root \
--password=yourpass \
--port=3306 \
--match-command=Sleep \
--idle-time=60 \
--victim=all \
--interval=5 \
--kill \
--print \
--daemonize \
--pid=/tmp/pt-kill.pid \
--log=/tmp/pt-kill.log
# ========== 杀死执行超过 30 秒的查询 ==========
pt-kill \
h=127.0.0.1,u=root,p=yourpass \
--busy-time=30 \
--match-command=Query \
--victim=all \
--interval=5 \
--kill \
--print \
--log=/tmp/pt-kill-slow.log
# ========== 只杀特定用户的连接 ==========
pt-kill \
h=127.0.0.1,u=root,p=yourpass \
--match-user=app_user \
--idle-time=120 \
--match-command=Sleep \
--kill \
--interval=10
# ========== 只打印不杀(观察模式) ==========
pt-kill \
h=127.0.0.1,u=root,p=yourpass \
--match-command=Sleep \
--idle-time=60 \
--interval=5 \
--print \
--no-kill
# ========== 停止守护进程 ==========
pt-kill --stop --pid=/tmp/pt-kill.pid
4.9 pt-stalk --- 异常自动诊断采集
功能: 当系统指标超过阈值时,自动采集 SHOW PROCESSLIST、SHOW ENGINE INNODB STATUS、iostat、vmstat、tcpdump 等诊断信息。
bash
# ========== 基础用法(监控 Threads_running) ==========
pt-stalk \
--function=status \
--variable=Threads_running \
--threshold=50 \
--cycles=3 \
--iterations=5 \
--run-time=30 \
--sleep=10 \
--dest=/tmp/pt-stalk \
--retention-time=72 \
--daemonize \
--pid=/tmp/pt-stalk.pid \
--log=/tmp/pt-stalk.log \
--user=root \
--password=yourpass
# ========== 监控自定义 SQL 指标 ==========
pt-stalk \
--function=processlist \
--variable=State \
--match="Sending data" \
--threshold=20 \
--cycles=2 \
--dest=/tmp/pt-stalk \
--user=root \
--password=yourpass \
--daemonize
# ========== 使用自定义脚本 ==========
# 创建自定义检测脚本 /tmp/check_disk.sh
cat > /tmp/check_disk.sh << 'EOF'
#!/bin/bash
# 返回磁盘使用率百分比
df /data | tail -1 | awk '{gsub(/%/,""); print $5}'
EOF
chmod +x /tmp/check_disk.sh
pt-stalk \
--function=/tmp/check_disk.sh \
--threshold=90 \
--cycles=2 \
--dest=/tmp/pt-stalk \
--user=root \
--password=yourpass \
--daemonize
# ========== 查看采集结果 ==========
ls -la /tmp/pt-stalk/
# 2026_07_29_16_30_01-processlist.txt
# 2026_07_29_16_30_01-innodbstatus.txt
# 2026_07_29_16_30_01-iostat.txt
# 2026_07_29_16_30_01-vmstat.txt
# 2026_07_29_16_30_01-tcpdump.txt
4.10 pt-summary / pt-mysql-summary --- 信息汇总
bash
# ========== 系统信息汇总 ==========
pt-summary
# 输出内容包括:
# - CPU 型号、核心数、负载
# - 内存使用
# - 磁盘挂载与 I/O 调度器
# - 网络接口
# - 操作系统版本
# ========== MySQL 状态汇总 ==========
pt-mysql-summary \
--host=127.0.0.1 \
--user=root \
--password=yourpass \
--port=3306
# 输出内容包括:
# - MySQL 版本、运行时间
# - 关键配置参数
# - 所有数据库及大小
# - 存储引擎使用情况
# - 连接数、线程数
# - InnoDB Buffer Pool 状态
# - 复制状态
# ========== 输出到文件 ==========
pt-mysql-summary \
h=127.0.0.1,u=root,p=yourpass \
> /tmp/mysql_summary_$(date +%Y%m%d).txt
4.11 pt-config-diff --- 配置差异对比
功能: 对比两个 MySQL 实例的配置差异,或对比配置文件与实际运行参数。
bash
# ========== 对比两个实例的配置 ==========
pt-config-diff \
h=server1,u=root,p=pass \
h=server2,u=root,p=pass
# ========== 对比配置文件与运行参数 ==========
pt-config-diff \
/etc/my.cnf \
h=127.0.0.1,u=root,p=pass
# ========== 对比两个配置文件 ==========
pt-config-diff \
/etc/my-master.cnf \
/etc/my-slave.cnf
输出示例:
2 config differences
Variable server1 server2
================ ============== ==============
innodb_buffer_pool_size 4G 2G
max_connections 500 200
4.12 pt-show-grants --- 权限导出
bash
# ========== 导出所有用户权限 ==========
pt-show-grants \
--host=127.0.0.1 \
--user=root \
--password=yourpass
# ========== 只导出指定用户 ==========
pt-show-grants \
h=127.0.0.1,u=root,p=yourpass \
--only app_user
# ========== 导出并生成可执行 SQL ==========
pt-show-grants \
h=127.0.0.1,u=root,p=yourpass \
--flush \
--revoke \
> /tmp/grants_backup.sql
# ========== 对比两个实例的权限差异 ==========
diff <(pt-show-grants h=server1,u=root,p=pass) \
<(pt-show-grants h=server2,u=root,p=pass)
4.13 pt-index-usage --- 索引使用分析
功能: 读取慢查询日志中的 SQL,用 EXPLAIN 分析索引使用情况,找出未使用的索引。
bash
# ========== 分析慢查询中的索引使用 ==========
pt-index-usage \
h=127.0.0.1,u=root,p=yourpass \
--databases=mydb \
/var/log/mysql/slow-query.log
# ========== 将结果保存到数据库 ==========
pt-index-usage \
h=127.0.0.1,u=root,p=yourpass \
--databases=mydb \
--create-indexes \
/var/log/mysql/slow-query.log
# ========== 输出报告 ==========
pt-index-usage \
h=127.0.0.1,u=root,p=yourpass \
/var/log/mysql/slow-query.log \
> /tmp/index_usage_report.txt
4.14 pt-visual-explain --- 可视化执行计划
bash
# ========== 格式化 EXPLAIN 输出为树状结构 ==========
# 方式一:管道输入
mysql -uroot -p -e "EXPLAIN SELECT * FROM mydb.users WHERE name='test'" | \
pt-visual-explain
# 方式二:从文件读取
mysql -uroot -p -e "EXPLAIN SELECT * FROM mydb.users u JOIN mydb.orders o ON u.id=o.user_id" \
> /tmp/explain.txt
pt-visual-explain /tmp/explain.txt
# 方式三:直接连接数据库执行
pt-visual-explain \
h=127.0.0.1,u=root,p=yourpass \
--query "SELECT * FROM mydb.users WHERE status=1 ORDER BY created_at DESC"
输出示例:
JOIN
+- Bookmark lookup
| +- Table
| | table users
| +- Index lookup
| key users->idx_status
| rows 1500
+- Table scan
rows 50000
table orders
4.15 pt-pmp --- 进程堆栈分析
功能: 聚合 gdb 堆栈信息,快速定位 MySQL 性能瓶颈(类似 perf 但更专注 MySQL)。
bash
# ========== 采集当前 mysqld 堆栈 ==========
pt-pmp --pid=$(pidof mysqld)
# ========== 采集指定次数 ==========
pt-pmp --pid=$(pidof mysqld) --iterations=5 --interval=2
# ========== 输出到文件 ==========
pt-pmp --pid=$(pidof mysqld) > /tmp/pmp_$(date +%Y%m%d_%H%M%S).txt
4.16 pt-diskstats --- 磁盘 I/O 分析
bash
# ========== 实时查看磁盘 I/O ==========
pt-diskstats
# ========== 分析 iostat 日志 ==========
# 先采集 iostat
iostat -xmt 1 60 > /tmp/iostat.log
# 再分析
pt-diskstats /tmp/iostat.log
# ========== 只看特定设备 ==========
pt-diskstats --devices-regex 'sd[a-b]' /tmp/iostat.log
五、生产环境最佳实践
5.1 通用 DSN 格式
h=主机,P=端口,u=用户,p=密码,D=数据库,t=表
# 示例
h=192.168.1.100,P=3306,u=dba,p=Secret123,D=mydb,t=users
5.2 安全建议清单
| 场景 | 建议 |
|---|---|
| 密码安全 | 使用 --defaults-file 或环境变量,避免命令行明文密码 |
| 在线 DDL | 先 --dry-run,再 --execute;设置 --max-lag、--max-load |
| 数据修复 | pt-table-sync 先 --print 预览,再 --execute |
| 数据归档 | pt-archiver 先 --dry-run,设置 --check-slave-lag |
| 杀连接 | pt-kill 先用 --print --no-kill 观察,确认后再开启 --kill |
| 权限 | 使用最小权限专用账号,避免直接用 root |
5.3 使用 defaults-file 避免密码暴露
bash
# 创建 ~/.my.cnf
cat > ~/.my.cnf << 'EOF'
[client]
user=pt_user
password=SecurePass123
host=127.0.0.1
port=3306
EOF
chmod 600 ~/.my.cnf
# 使用时无需传密码
pt-query-digest --defaults-file=~/.my.cnf /var/log/mysql/slow.log
pt-online-schema-change --defaults-file=~/.my.cnf --alter "ADD INDEX idx_a(a)" D=mydb,t=t1 --execute
5.4 常用运维组合流程
┌─────────────────────────────────────────────────────────┐
│ 日常运维流程 │
├─────────────────────────────────────────────────────────┤
│ │
│ 1. pt-summary + pt-mysql-summary → 了解系统全貌 │
│ 2. pt-query-digest → 分析慢查询 │
│ 3. pt-duplicate-key-checker → 清理冗余索引 │
│ 4. pt-index-usage → 发现无用索引 │
│ 5. pt-online-schema-change → 安全加索引/改表 │
│ 6. pt-heartbeat → 监控复制延迟 │
│ 7. pt-table-checksum + pt-table-sync → 校验修复主从 │
│ 8. pt-archiver → 归档历史数据 │
│ 9. pt-kill → 清理异常连接 │
│ 10. pt-stalk → 异常自动采集 │
│ │
└─────────────────────────────────────────────────────────┘
六、参考资源
💡 提示: 每个工具都支持 --help 查看完整参数说明,例如 pt-online-schema-change --help。生产环境操作前务必在测试环境验证!