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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
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.
4595 0
|
存储 关系型数据库 MySQL
|
5月前
|
编译器 Go 开发工具
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
54 0
|
10月前
|
Oracle Java 关系型数据库
华为云aarch64架构下载jdk;Linux上jdk无法执行二进制文件及​gzip: stdin: invalid compressed data–format violated​报错
华为云aarch64架构下载jdk;Linux上jdk无法执行二进制文件及​gzip: stdin: invalid compressed data–format violated​报错
300 0
|
分布式计算 开发工具 计算机视觉
ps,pr ,ae,dw等软件简短解析(含安装包)
Adobe photoshop 专长在于图像处理,而不是图形创作 平面设计是Photoshop应用最为广泛的领域,
|
计算机视觉
关于 编译QT项目时报错: error: cannot find -lGL 的解决方法
关于 编译QT项目时报错: error: cannot find -lGL 的解决方法
|
SQL MySQL 关系型数据库
pt-tools系统:pt-kill 实战
列出几种常用场景,并进行分析实战测试 特殊、 打印出执行时间超过3秒的connection,仅仅打印,不kill 每2秒循环一次,超过10秒就退出pt-kill程序pt-kill --host xx.
1388 0
|
监控 关系型数据库 MySQL
pt-tools系列:xtrabackup最佳实践
安装xtrabackup 下载2.4版本即可 运行报错 /data/Keithlan/pt_backup/bin/innobackupex: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.
2574 0
Confluence 6 用户宏示例 - Color and Size
这个示例定义了如何向你宏中传递参数。我们将会创建一个字体样式宏,在这个宏中有 2 个参数,允许用户在这 2 个参数中指定宏中包含的字体的颜色大小。
1047 0

热门文章

最新文章