pt-tools系列:pt-kill 官方使用文档

简介:

https://www.percona.com/doc/percona-toolkit/LATEST/pt-kill.html

一、NAME

pt-kill字面意思就是: kill掉MySQL满足某些特征的query语句

二、大纲:使用方法

pt-kill [OPTIONS] [DSN]

pt-kill kill MySQL的链接。如果pt-kill没有指定特定文件的话,它连接到MySQL Server,然后通过show processlist 来得到查询语句
如果参数指定了文件,则可以从包含show processlist的文件中读取query语句并分析处理
默认从STDIN获取

  • kill掉执行时间超过60s的query
pt-kill --busy-time 60 --kill
  • 打印出执行时间超过60s的query,仅仅是打印,不会kill
pt-kill --busy-time 60 --print
  • 每10s 去检查sleep 状态的query , 并kill掉
pt-kill --match-command Sleep --kill --victims all --interval 10
  • 打印所有login 状态的 query
pt-kill --match-state login --print --victims all
  • 通过文件分析哪些query满足match条件
mysql -e "SHOW PROCESSLIST" > proclist.txt
pt-kill --test-matching proclist.txt --busy-time 60 --print

三、风险

任何软件都有风险,在使用这个工具前,如下建议请关注:

  • 仔细阅读此工具的说明书
  • review此工具的已知BUGS
  • 在非生产环境进行测试
  • 做好备份并检查你的备份是否可用

四、说明

pt-kill 获取从show processlist中获取query,并进行过滤,然后要么kill,要么print

在某种场景,这也是公认的另一种slow query终结者

主要目的就是观察那些有可能使用非常多资源的query,然后进行kill来保护数据库

通常pt-kill是通过连接MySQL,然后show processlist来获取query,但也还有另一种方法,就是通过指定file

在指定文件这种场景下,pt-kill中的参数--kill就不起作用了,你应该使用--print

当你指定--test-matching的时候,才表示你从文件获取query,然后test是否满足相关匹配条件

接下来,你还有很多规则需要遵守,比如:‘不要将replication thread’ 给kill了,千万别kill掉一些比较重要的thread

两个重要的options : --busy-time 和 --victims

--busy-time 指的是query的执行时间(需要测试 --match-command 和 --busy-time 都指定的话,是或的关系,还是且的关系 )
--victims 指的是满足条件的query是否都需要kill,是删除oldest query,还是所有的都删除

通常,你至少需要制定一个--match option,否则没有query将会被匹配
你也可以制定--match-all 去匹配所有query(不包括--ignore忽略的)

五、GROUP, MATCH AND KILL

query语句是如何经过层层筛选,最终得到精确的语句的呢?接下来我们具体来看看详细的流程

  • 第一步:group query into classes
1. --group-by 选项就是控制grouping的。
2. 默认--group-by没有值,表示所有queries都被分在默认的class中。
3. 第二步中的matching规则将会应用在每个class中,如果你不想全部应用的话,就需要单独分组group
  • 第二步:matching
Matching implies filtering since if a query doesn’t match some criteria, it is removed from its class.
Matching happens for each class. First, queries are filtered from their class by the various Query Matches options like --match-user.
Then, entire classes are filtered by the various Class Matches options like --query-count.
  • 第三步:KILL
最后一步其实就是victim selection

你是想kill oldest query  还是 all queries,  由 --victims 决定
  • 最后
The forth and final step is to take some action on all matching queries from all classes.

action: 有这些 , 按照 --print, --execute-command, --kill"/"--kill-query 的顺序执行

六、OUTPUT

如果仅仅指定了 --kill , 那么不会有output
如果仅仅指定了 --print, 那么你会看到这样的output

# 2009-07-15T15:04:01 KILL 8 (Query 42 sec) SELECT * FROM huge_table

这一行显示了时间戳,query的id是8,时间是42秒,info 是query SQL本身

如果同时指定了 --kill --print ,那么匹配后的query会被kill,且会打印出来

七、OPTIONS

至少要指定这些参数里面的一个: --kill, --kill-query, --print, --execute-command or --stop

--any-busy-time and --each-busy-time 这两个参数是互斥的,二者只能取其一
--kill and --kill-query 这两个参数是互斥的,二者只能取其一
--daemonize and --test-matching 这两个参数是互斥的,二者只能取其一

它还可以接受命令行参数: --ask-pass : 连接MySQL的时候输入的密码

--charset: 默认字符集
--config:Read this comma-separated list of config files; if specified, this must be the first option on the command line.
--create-log-table:创建一个--log-dsn指定的表
--daemonize:后台运行
--database:数据库名
--defaults-file:给定决定路径,仅从这个文件去获取MySQL的options
--filter:这个不常用,不做多解释,用的时候再来看
--group-by:可以根据不同show processlist字段分组,比如:info,可以根据SQL语句分组,这样的用法也不常见,有需求的时候再细看
--help:查看帮助
--host:ip
--interval : 检查频率,如果--busy-time没有指定,那么默认的interval就是30秒。 否则,interval就是--busy-time的一半。 如果同时指定,频率就以显示指定的--interval为准
--log: 当后台运行时,output打印到指定日志
--log-dsn: 存储每一个被kill的query到DSN(数据库表)
--password: 数据库密码
--pid: 指定pid,如果pid存在,则此工具不会运行
--port: 端口
--run-time: pt-kill工具可以运行多长时间,默认是永久。
--sentinel: 当某个文件存在时,pt-kill自动停止运行
--slave-user & --slave-password : slave相关的选项,以更小的权限访问slave而已
--set-vars : 在MySQL中设置某些变量,比如: wait_timeout=10000
--socket:socket file to use for connection
--stop: Stop running instances by creating the --sentinel file
--[no]strip-comments : 删除掉query后面的comment
--version: 显示pt-kill的版本
--user: 用户名
--[no]version-check : 版本检查
--victims : 默认是oldest ,其他选项为(all,all-but-oldest)

八、QUERY MATCHES

默认是区分大小写的,可以通过regex不区分大小写,比如:(?i-xsm:select)

  • --busy-time:
type: time; group: Query Matches

状态:Command=Query , 执行时间超过--busy-time=N 秒
  • --idle-time
type: time; group: Query Matches
状态:Command=Sleep ,空闲时间超过--idle-time=N 秒
  • --ignore-command
type: string; group: Query Matches

忽略的command,支持正则
  • --ignore-db
type: string; group: Query Matches

忽略的DB,支持正则匹配
  • --ignore-host
type: string; group: Query Matches

Ignore queries whose Host matches this Perl regex.
  • --ignore-info
type: string; group: Query Matches

Ignore queries whose Info (query) matches this Perl regex.
  • --[no]ignore-self
default: yes; group: Query Matches

Don’t kill pt-kill‘s own connection.  默认不会删除pt-kill自己的连接
  • --ignore-state
type: string; group: Query Matches; default: Locked

Ignore queries whose State matches this Perl regex. The default is to keep threads from being killed if they are locked waiting for another thread.

默认如果被锁住,那么是不会被kill掉的
  • --ignore-user
type: string; group: Query Matches

Ignore queries whose user matches this Perl regex.
  • --match-all
如果没有指定--ignore,那么匹配所有query(不包括replication thread,除非指定 --replication-threads)
  • --match-command
type: string; group: Query Matches

Match only queries whose Command matches this Perl regex.

常用的Command如下:

Query
Sleep
Binlog Dump
Connect
Delayed insert
Execute
Fetch
Init DB
Kill
Prepare
Processlist
Quit
Reset stmt
Table Dump


See http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html for a full list and description of Command values.
  • --match-db
type: string; group: Query Matches

Match only queries whose db (database) matches this Perl regex.
  • --match-host
type: string; group: Query Matches

Match only queries whose Host matches this Perl regex.

The Host value often time includes the port like “host:port”.
  • --match-info
type: string; group: Query Matches

Match only queries whose Info (query) matches this Perl regex.

The Info column of the processlist shows the query that is being executed or NULL if no query is being executed.
  • --match-state
ype: string; group: Query Matches

Match only queries whose State matches this Perl regex.

常用state如下:

Locked
login
copy to tmp table
Copying to tmp table
Copying to tmp table on disk
Creating tmp table
executing
Reading from net
Sending data
Sorting for order
Sorting result
Table lock
Updating

See http://dev.mysql.com/doc/refman/5.1/en/general-thread-states.html for a full list and description of State values.
  • --match-user
type: string; group: Query Matches

Match only queries whose User matches this Perl regex.
  • --replication-threads
group: Query Matches

Allow matching and killing replication threads.

By default, matches do not apply to replication threads; i.e. replication threads are completely ignored. Specifying this option allows matches to match (and potentially kill) replication threads on masters and slaves.


默认,是不允许kill 复制线程的,除非显示指定了这个选项
  • --test-matching
type: array; group: Query Matches

Files with processlist snapshots to test matching options against. Since the matching options can be complex, you can save snapshots of processlist in files, then test matching options against queries in those files.

This option disables --run-time, --interval, and --[no]ignore-self.

指定一个文件,根据文件中的show processlist来匹配,而不是连接数据库

九、CLASS MATCHES

忽略

十、ACTIONS

默认的执行顺序是: --print, --execute-command, --kill"/"--kill-query

  • --execute-command
当query匹配后,执行这个command
  • --kill
当query匹配后,执行kill 删除connection

  • --kill-busy-commands
默认是kill command为query的连接,但是如果你想kill其他command,怎么办呢?

--kill-busy-commands=Query,Execute  参考下

  • --kill-query
只kill query,不kill connection
  • --print

打印被kill的语句

十一、DSN OPTIONS

忽略

十二、ENVIRONMENT

忽略

十三、SYSTEM REQUIREMENTS

You need Perl, DBI, DBD::mysql, and some core packages that ought to be installed in any reasonably new version of Perl.

十四、BUGS

http://www.percona.com/bugs/pt-kill.

十五、DOWNLOADING

http://www.percona.com/software/percona-toolkit/

十六、VERSION

pt-kill 3.0.12

十七、作者

Baron Schwartz and Daniel Nichter

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
存储 安全 索引
vacuum freeze无法回收事务号问题分析
vacuum freeze报错问题分析
5058 0
|
SQL 关系型数据库 MySQL
pt-tools系列:pt-online-schema-change 最佳实践
pt的详细步骤 Step 1: Create the new table. Step 2: Alter the new, empty table. This should be very quick, or die if the user specified a bad alter statement.
6058 0
|
7月前
|
人工智能 安全 网络协议
2026版OpenClaw Skills深度实战指南:浏览器与邮件技能全解析+阿里云部署速成
如果说OpenClaw(原Clawdbot,曾用名Moltbot)与大模型的组合是打造智能AI助理的“大脑”,那么Skills就是赋予它行动能力的“双手”。作为阿里云生态下开源AI自动化代理工具的核心扩展模块,Skills通过标准化功能封装,让AI助手突破“只会说不会做”的局限,自主完成网页浏览、信息检索、邮件管理等实操任务。2026年最新版OpenClaw已默认集成浏览器操作插件agent-browser v0.2.0,同时支持从Clawhub技能库扩展超3000款实用功能,搭配阿里云云端部署的高稳定性,能快速打造7×24小时运行的自动化AI助理。
4468 2
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
40676 184
|
NoSQL Redis 监控
redis-shake数据同步&迁移&备份导入导出工具使用介绍
redis-shake是阿里云Redis&MongoDB团队开源的用于redis数据同步的工具。
74541 4
redis-shake数据同步&迁移&备份导入导出工具使用介绍
|
监控 关系型数据库 MySQL
初体验:数据库监控、管理和可观测性工具(PMM)
Percona Monitoring and Management (PMM) 是一个开源工具,用于监控MySQL、PostgreSQL和MongoDB的性能。它提供实时监控、数据可视化、故障排除和管理功能,支持本地和云端数据库。要安装PMM,首先需安装Docker,然后通过提供的脚本部署PMM服务器和客户端。在MySQL服务器上创建PMM用户后,使用`pmm-admin`命令添加数据库。访问PMM的HTTPS网址(默认用户名和密码为admin)进行配置。本文还包含了安装Docker和PMM的命令行步骤。
初体验:数据库监控、管理和可观测性工具(PMM)
|
存储 SQL 缓存
一文带你了解MySQL之Adaptive Hash Index
在InnoDB体系架构图的内存结构中,还有一块区域名为:Adaptive Hash Index,翻译成中文:自适应哈希索引,缩写:AHI,它是一个纯内存结构,我们今天就来了解它。
3629 0
|
JSON 运维 网络协议
做短信接口时,http接口和cmpp接口怎么选?
本文介绍了短信接口中HTTP与CMPP协议的区别及适用场景,帮助开发者根据业务需求选择合适的接口类型。
1491 1
|
SQL 关系型数据库 MySQL
阿里面试:1000万级大表, 如何 加索引?
45岁老架构师尼恩在其读者交流群中分享了如何在生产环境中给大表加索引的方法。文章详细介绍了两种索引构建方式:在线模式(Online DDL)和离线模式(Offline DDL),并深入探讨了 MySQL 5.6.7 之前的“影子策略”和 pt-online-schema-change 方案,以及 MySQL 5.6.7 之后的内部 Online DDL 特性。通过这些方法,可以有效地减少 DDL 操作对业务的影响,确保数据的一致性和完整性。尼恩还提供了大量面试题和解决方案,帮助读者在面试中充分展示技术实力。
|
SQL 监控 关系型数据库
使用 pt-query-digest 工具分析 MySQL 慢日志
【8月更文挑战第5天】使用 pt-query-digest 工具分析 MySQL 慢日志
1636 3
使用 pt-query-digest 工具分析 MySQL 慢日志