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

栏目: Perl · 发布时间: 5年前

内容简介:pt-kill字面意思就是: kill掉MySQL满足某些特征的query语句pt-kill kill MySQL的链接。如果pt-kill没有指定特定文件的话,它连接到MySQL Server,然后通过show processlist 来得到查询语句

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 42sec)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 matchesthis Perl regex.
  • —ignore-info
type: string; group: Query Matches

Ignorequeries whoseInfo(query) matches thisPerlregex.
  • —[no]ignore-self
default: yes; group: Query Matches

Don’tkill pt-kill‘s ownconnection. 默认不会删除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 whoseusermatches 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) matchesthis 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) matchesthis 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

Matchonly queries whoseStatematches thisPerlregex.

常用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 matchesthis Perl regex.
  • —replication-threads
group: Query Matches

Allow matchingand 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

Fileswith 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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数学与泛型编程

数学与泛型编程

[美]亚历山大 A. 斯捷潘诺夫(Alexander A. Stepanov)、[美]丹尼尔 E. 罗斯(Daniel E. Rose) / 爱飞翔 / 机械工业出版社 / 2017-8 / 79

这是一本内容丰富而又通俗易懂的书籍,由优秀的软件设计师 Alexander A. Stepanov 与其同事 Daniel E. Rose 所撰写。作者在书中解释泛型编程的原则及其所依据的抽象数学概念,以帮助你写出简洁而强大的代码。 只要你对编程相当熟悉,并且擅长逻辑思考,那么就可以顺利阅读本书。Stepanov 与 Rose 会清晰地讲解相关的抽象代数及数论知识。他们首先解释数学家想要解决......一起来看看 《数学与泛型编程》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器