14
pt-kill - Kill MySQL queries that match certain criteria.
22
Usage: pt-kill [OPTION]... [FILE...]
24
pt-kill kills MySQL connections. pt-kill connects to MySQL and gets queries
25
from SHOW PROCESSLIST if no FILE is given. Else, it reads queries from one
26
or more FILE which contains the output of SHOW PROCESSLIST. If FILE is -,
27
pt-kill reads from STDIN.
29
Kill queries running longer than 60s:
34
pt-kill --busy-time 60 --kill
37
Print, do not kill, queries running longer than 60s:
42
pt-kill --busy-time 60 --print
45
Check for sleeping processes and kill them all every 10s:
50
pt-kill --match-command Sleep --kill --victims all --interval 10
53
Print all login processes:
58
pt-kill --match-state login --print --victims all
61
See which queries in the processlist right now would match:
66
mysql -e "SHOW PROCESSLIST" | pt-kill --busy-time 60 --print
75
The following section is included to inform users about the potential risks,
76
whether known or unknown, of using this tool. The two main categories of risks
77
are those created by the nature of the tool (e.g. read-only tools vs. read-write
78
tools) and those created by bugs.
80
pt-kill is designed to kill queries if you use the "--kill" option is given,
81
and that might disrupt your database's users, of course. You should test with
82
the <"--print"> option, which is safe, if you're unsure what the tool will do.
84
At the time of this release, we know of no bugs that could cause serious harm to
87
The authoritative source for updated information is always the online issue
88
tracking system. Issues that affect this tool will be marked as such. You can
89
see a list of such issues at the following URL:
90
`http://www.percona.com/bugs/pt-kill <http://www.percona.com/bugs/pt-kill>`_.
92
See also "BUGS" for more information on filing bugs and getting help.
100
pt-kill captures queries from SHOW PROCESSLIST, filters them, and then either
101
kills or prints them. This is also known as a "slow query sniper" in some
102
circles. The idea is to watch for queries that might be consuming too many
103
resources, and kill them.
105
For brevity, we talk about killing queries, but they may just be printed
106
(or some other future action) depending on what options are given.
108
Normally pt-kill connects to MySQL to get queries from SHOW PROCESSLIST.
109
Alternatively, it can read SHOW PROCESSLIST output from files. In this case,
110
pt-kill does not connect to MySQL and "--kill" has no effect. You should
111
use "--print" instead when reading files. The ability to read a file (or
112
- for STDIN) allows you to capture SHOW PROCESSLIST and test it later with
113
pt-kill to make sure that your matches kill the proper queries. There are a
114
lot of special rules to follow, such as "don't kill replication threads,"
115
so be careful to not kill something important!
117
Two important options to know are "--busy-time" and "--victims".
118
First, whereas most match/filter options match their corresponding value from
119
SHOW PROCESSLIST (e.g. "--match-command" matches a query's Command value),
120
the Time value is matched by "--busy-time". See also "--interval".
122
Second, "--victims" controls which matching queries from each class are
123
killed. By default, the matching query with the highest Time value is killed
124
(the oldest query). See the next section, "GROUP, MATCH AND KILL",
127
Usually you need to specify at least one \ ``--match``\ option, else no
128
queries will match. Or, you can specify "--match-all" to match all queries
129
that aren't ignored by an \ ``--ignore``\ option.
131
pt-kill is a work in progress, and there is much more it could do.
134
*********************
135
GROUP, MATCH AND KILL
136
*********************
139
Queries pass through several steps to determine which exactly will be killed
140
(or printed--whatever action is specified). Understanding these steps will
141
help you match precisely the queries you want.
143
The first step is grouping queries into classes. The "--group-by" option
144
controls grouping. By default, this option has no value so all queries are
145
grouped into one, big default class. All types of matching and filtering
146
(the next step) are applied per-class. Therefore, you may need to group
147
queries in order to match/filter some classes but not others.
149
The second step is matching. Matching implies filtering since if a query
150
doesn't match some criteria, it is removed from its class.
151
Matching happens for each class. First, queries are filtered from their
152
class by the various \ ``Query Matches``\ options like "--match-user".
153
Then, entire classes are filtered by the various \ ``Class Matches``\ options
154
like "--query-count".
156
The third step is victim selection, that is, which matching queries in each
157
class to kill. This is controlled by the "--victims" option. Although
158
many queries in a class may match, you may only want to kill the oldest
159
query, or all queries, etc.
161
The forth and final step is to take some action on all matching queries
162
from all classes. The \ ``Actions``\ options specify which actions will be
163
taken. At this step, there are no more classes, just a single list of
164
queries to kill, print, etc.
172
If only "--kill" then there is no output. If only "--print" then a
173
timestamped KILL statement if printed for every query that would have
179
# 2009-07-15T15:04:01 KILL 8 (Query 42 sec) SELECT * FROM huge_table
182
The line shows a timestamp, the query's Id (8), its Time (42 sec) and its
183
Info (usually the query SQL).
185
If both "--kill" and "--print" are given, then matching queries are
186
killed and a line for each like the one above is printed.
188
Any command executed by "--execute-command" is responsible for its own
189
output and logging. After being executed, pt-kill has no control or interaction
198
Specify at least one of "--kill", "--kill-query", "--print", "--execute-command" or "--stop".
200
"--any-busy-time" and "--each-busy-time" are mutually exclusive.
202
"--kill" and "--kill-query" are mutually exclusive.
204
This tool accepts additional command-line arguments. Refer to the
205
"SYNOPSIS" and usage information for details.
210
Prompt for a password when connecting to MySQL.
216
short form: -A; type: string
218
Default character set. If the value is utf8, sets Perl's binmode on
219
STDOUT to utf8, passes the mysql_enable_utf8 option to DBD::mysql, and runs SET
220
NAMES UTF8 after connecting to MySQL. Any other value sets binmode on STDOUT
221
without the utf8 layer, and runs SET NAMES after connecting to MySQL.
229
Read this comma-separated list of config files; if specified, this must be the
230
first option on the command line.
236
Fork to the background and detach from the shell. POSIX operating systems
243
short form: -F; type: string
245
Only read mysql options from the given file. You must give an absolute
254
Apply matches to each class of queries grouped by this SHOW PROCESSLIST column.
255
In addition to the basic columns of SHOW PROCESSLIST (user, host, command,
256
state, etc.), queries can be matched by \ ``fingerprint``\ which abstracts the
257
SQL query in the \ ``Info``\ column.
259
By default, queries are not grouped, so matches and actions apply to all
260
queries. Grouping allows matches and actions to apply to classes of
261
similar queries, if any queries in the class match.
263
For example, detecting cache stampedes (see \ ``all-but-oldest``\ under
264
"--victims" for an explanation of that term) requires that queries are
265
grouped by the \ ``arg``\ attribute. This creates classes of identical queries
266
(stripped of comments). So queries \ ``"SELECT c FROM t WHERE id=1"``\ and
267
\ ``"SELECT c FROM t WHERE id=1"``\ are grouped into the same class, but
268
query c<"SELECT c FROM t WHERE id=3"> is not identical to the first two
269
queries so it is grouped into another class. Then when "--victims"
270
\ ``all-but-oldest``\ is specified, all but the oldest query in each class is
271
killed for each class of queries that matches the match criteria.
283
short form: -h; type: string; default: localhost
293
How often to check for queries to kill. If "--busy-time" is not given,
294
then the default interval is 30 seconds. Else the default is half as often
295
as "--busy-time". If both "--interval" and "--busy-time" are given,
296
then the explicit "--interval" value is used.
298
See also "--run-time".
306
Print all output to this file when daemonized.
312
short form: -p; type: string
314
Password to use when connecting.
322
Create the given PID file when daemonized. The file contains the process ID of
323
the daemonized instance. The PID file is removed when the daemonized instance
324
exits. The program checks for the existence of the PID file when starting; if
325
it exists and the process with the matching PID exists, the program exits.
331
short form: -P; type: int
333
Port number to use for connection.
341
Remove SQL comments from queries in the Info column of the PROCESSLIST.
349
How long to run before exiting. By default pt-kill runs forever, or until
350
its process is killed or stopped by the creation of a "--sentinel" file.
351
If this option is specified, pt-kill runs for the specified amount of time
352
and sleeps "--interval" seconds between each check of the PROCESSLIST.
358
type: string; default: /tmp/pt-kill-sentinel
360
Exit if this file exists.
362
The presence of the file specified by "--sentinel" will cause all
363
running instances of pt-kill to exit. You might find this handy to stop cron
364
jobs gracefully if necessary. See also "--stop".
370
type: string; default: wait_timeout=10000
372
Set these MySQL variables. Immediately after connecting to MySQL, this string
373
will be appended to SET and executed.
379
short form: -S; type: string
381
Socket file to use for connection.
387
Stop running instances by creating the "--sentinel" file.
389
Causes pt-kill to create the sentinel file specified by "--sentinel" and
390
exit. This should have the effect of stopping all running instances which are
391
watching the same sentinel file.
397
short form: -u; type: string
399
User for login if not current user.
405
Show version and exit.
411
type: string; default: oldest
413
Which of the matching queries in each class will be killed. After classes
414
have been matched/filtered, this option specifies which of the matching
415
queries in each class will be killed (or printed, etc.). The following
421
Only kill the single oldest query. This is to prevent killing queries that
422
aren't really long-running, they're just long-waiting. This sorts matching
423
queries by Time and kills the one with the highest Time value.
429
Kill all queries in the class.
435
Kill all but the oldest query. This is the inverse of the \ ``oldest``\ value.
437
This value can be used to prevent "cache stampedes", the condition where
438
several identical queries are executed and create a backlog while the first
439
query attempts to finish. Since all queries are identical, all but the first
440
query are killed so that it can complete and populate the cache.
450
Wait after killing a query, before looking for more to kill. The purpose of
451
this is to give blocked queries a chance to execute, so we don't kill a query
452
that's blocking a bunch of others, and then kill the others immediately
461
Wait before killing a query. The purpose of this is to give
462
"--execute-command" a chance to see the matching query and gather other
463
MySQL or system information before it's killed.
471
These options filter queries from their classes. If a query does not
472
match, it is removed from its class. The \ ``--ignore``\ options take precedence.
473
The matches for command, db, host, etc. correspond to the columns returned
474
by SHOW PROCESSLIST: Command, db, Host, etc. All pattern matches are
475
case-sensitive by default, but they can be made case-insensitive by specifying
476
a regex pattern like \ ``(?i-xsm:select)``\ .
478
See also "GROUP, MATCH AND KILL".
485
Match all queries that are not ignored. If no ignore options are specified,
486
then every query matches (except replication threads, unless
487
"--replication-threads" is also specified). This option allows you to
488
specify negative matches, i.e. "match every query \ *except*\ ..." where the
489
exceptions are defined by specifying various \ ``--ignore``\ options.
491
This option is \ *not*\ the same as "--victims" \ ``all``\ . This option matches
492
all queries within a class, whereas "--victims" \ ``all``\ specifies that all
493
matching queries in a class (however they matched) will be killed. Normally,
494
however, the two are used together because if, for example, you specify
495
"--victims" \ ``oldest``\ , then although all queries may match, only the oldest
502
type: time; group: Query Matches
504
Match queries that have been running for longer than this time. The queries
505
must be in Command=Query status. This matches a query's Time value as
506
reported by SHOW PROCESSLIST.
512
type: time; group: Query Matches
514
Match queries that have been idle/sleeping for longer than this time.
515
The queries must be in Command=Sleep status. This matches a query's Time
516
value as reported by SHOW PROCESSLIST.
522
type: string; group: Query Matches
524
Ignore queries whose Command matches this Perl regex.
526
See "--match-command".
532
type: string; group: Query Matches
534
Ignore queries whose db (database) matches this Perl regex.
542
type: string; group: Query Matches
544
Ignore queries whose Host matches this Perl regex.
552
type: string; group: Query Matches
554
Ignore queries whose Info (query) matches this Perl regex.
562
default: yes; group: Query Matches
564
Don't kill pt-kill's own connection.
570
type: string; group: Query Matches; default: Locked
572
Ignore queries whose State matches this Perl regex. The default is to keep
573
threads from being killed if they are locked waiting for another thread.
581
type: string; group: Query Matches
583
Ignore queries whose user matches this Perl regex.
591
type: string; group: Query Matches
593
Match only queries whose Command matches this Perl regex.
595
Common Command values are:
616
See `http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html <http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html>`_ for a full
617
list and description of Command values.
623
type: string; group: Query Matches
625
Match only queries whose db (database) matches this Perl regex.
631
type: string; group: Query Matches
633
Match only queries whose Host matches this Perl regex.
635
The Host value often time includes the port like "host:port".
641
type: string; group: Query Matches
643
Match only queries whose Info (query) matches this Perl regex.
645
The Info column of the processlist shows the query that is being executed
646
or NULL if no query is being executed.
652
type: string; group: Query Matches
654
Match only queries whose State matches this Perl regex.
656
Common State values are:
665
Copying to tmp table on disk
676
See `http://dev.mysql.com/doc/refman/5.1/en/general-thread-states.html <http://dev.mysql.com/doc/refman/5.1/en/general-thread-states.html>`_ for
677
a full list and description of State values.
683
type: string; group: Query Matches
685
Match only queries whose User matches this Perl regex.
689
--replication-threads
693
Allow matching and killing replication threads.
695
By default, matches do not apply to replication threads; i.e. replication
696
threads are completely ignored. Specifying this option allows matches to
697
match (and potentially kill) replication threads on masters and slaves.
706
These matches apply to entire query classes. Classes are created by specifying
707
the "--group-by" option, else all queries are members of a single, default
710
See also "GROUP, MATCH AND KILL".
715
type: time; group: Class Matches
717
Match query class if any query has been running for longer than this time.
718
"Longer than" means that if you specify \ ``10``\ , for example, the class will
719
only match if there's at least one query that has been running for greater
722
See "--each-busy-time" for more details.
728
type: time; group: Class Matches
730
Match query class if each query has been running for longer than this time.
731
"Longer than" means that if you specify \ ``10``\ , for example, the class will
732
only match if each and every query has been running for greater than 10
735
See also "--any-busy-time" (to match a class if ANY query has been running
736
longer than the specified time) and "--busy-time".
742
type: int; group: Class Matches
744
Match query class if it has at least this many queries. When queries are
745
grouped into classes by specifying "--group-by", this option causes matches
746
to apply only to classes with at least this many queries. If "--group-by"
747
is not specified then this option causes matches to apply only if there
748
are at least this many queries in the entire SHOW PROCESSLIST.
756
Print information to STDOUT about what is being done.
765
These actions are taken for every matching query from all classes.
766
The actions are taken in this order: "--print", "--execute-command",
767
"--kill"/"--kill-query". This order allows "--execute-command"
768
to see the output of "--print" and the query before
769
"--kill"/"--kill-query". This may be helpful because pt-kill does
770
not pass any information to "--execute-command".
772
See also "GROUP, MATCH AND KILL".
777
type: string; group: Actions
779
Execute this command when a query matches.
781
After the command is executed, pt-kill has no control over it, so the command
782
is responsible for its own info gathering, logging, interval, etc. The
783
command is executed each time a query matches, so be careful that the command
784
behaves well when multiple instances are ran. No information from pt-kill is
785
passed to the command.
787
See also "--wait-before-kill".
795
Kill the connection for matching queries.
797
This option makes pt-kill kill the connections (a.k.a. processes, threads) that
798
have matching queries. Use "--kill-query" if you only want to kill
799
individual queries and not their connections.
801
Unless "--print" is also given, no other information is printed that shows
802
that pt-kill matched and killed a query.
804
See also "--wait-before-kill" and "--wait-after-kill".
812
Kill matching queries.
814
This option makes pt-kill kill matching queries. This requires MySQL 5.0 or
815
newer. Unlike "--kill" which kills the connection for matching queries,
816
this option only kills the query, not its connection.
824
Print a KILL statement for matching queries; does not actually kill queries.
826
If you just want to see which queries match and would be killed without
827
actually killing them, specify "--print". To both kill and print
828
matching queries, specify both "--kill" and "--print".
839
These DSN options are used to create a DSN. Each option is given like
840
\ ``option=value``\ . The options are case-sensitive, so P and p are not the
841
same option. There cannot be whitespace before or after the \ ``=``\ and
842
if the value contains whitespace it must be quoted. DSN options are
843
comma-separated. See the percona-toolkit manpage for full details.
848
dsn: charset; copy: yes
850
Default character set.
856
dsn: database; copy: yes
864
dsn: mysql_read_default_file; copy: yes
866
Only read default options from the given file
880
dsn: password; copy: yes
882
Password to use when connecting.
890
Port number to use for connection.
896
dsn: mysql_socket; copy: yes
898
Socket file to use for connection.
906
User for login if not current user.
916
Visit `http://www.percona.com/software/ <http://www.percona.com/software/>`_ to download the latest release of
917
Percona Toolkit. Or, to get the latest release from the command line:
922
wget percona.com/latest/percona-toolkit/PKG
925
Replace \ ``PKG``\ with \ ``tar``\ , \ ``rpm``\ , or \ ``deb``\ to download the package in that
926
format. You can also get individual tools from the latest release:
931
wget percona.com/latest/percona-toolkit/TOOL
934
Replace \ ``TOOL``\ with the name of any tool.
942
The environment variable \ ``PTDEBUG``\ enables verbose debugging output to STDERR.
943
To enable debugging and capture all output to a file, run the tool like:
948
PTDEBUG=1 pt-kill ... > FILE 2>&1
951
Be careful: debugging output is voluminous and can generate several megabytes
960
You need Perl, DBI, DBD::mysql, and some core packages that ought to be
961
installed in any reasonably new version of Perl.
969
For a list of known bugs, see `http://www.percona.com/bugs/pt-kill <http://www.percona.com/bugs/pt-kill>`_.
971
Please report bugs at `https://bugs.launchpad.net/percona-toolkit <https://bugs.launchpad.net/percona-toolkit>`_.
972
Include the following information in your bug report:
975
\* Complete command-line used to run the tool
983
\* MySQL version of all servers involved
987
\* Output from the tool including STDERR
991
\* Input files (log/dump/config files, etc.)
995
If possible, include debugging output by running the tool with \ ``PTDEBUG``\ ;
1004
Baron Schwartz and Daniel Nichter
1007
*********************
1008
ABOUT PERCONA TOOLKIT
1009
*********************
1012
This tool is part of Percona Toolkit, a collection of advanced command-line
1013
tools developed by Percona for MySQL support and consulting. Percona Toolkit
1014
was forked from two projects in June, 2011: Maatkit and Aspersa. Those
1015
projects were created by Baron Schwartz and developed primarily by him and
1016
Daniel Nichter, both of whom are employed by Percona. Visit
1017
`http://www.percona.com/software/ <http://www.percona.com/software/>`_ for more software developed by Percona.
1020
********************************
1021
COPYRIGHT, LICENSE, AND WARRANTY
1022
********************************
1025
This program is copyright 2009-2011 Baron Schwartz, 2011 Percona Inc.
1026
Feedback and improvements are welcome.
1028
THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
1029
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1030
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1032
This program is free software; you can redistribute it and/or modify it under
1033
the terms of the GNU General Public License as published by the Free Software
1034
Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
1035
systems, you can issue \`man perlgpl' or \`man perlartistic' to read these
1038
You should have received a copy of the GNU General Public License along with
1039
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
1040
Place, Suite 330, Boston, MA 02111-1307 USA.
1048
Percona Toolkit v1.0.0 released 2011-08-01