MK-KILL(1)

πŸ“¦
version 7540 Β· 3 versions
, in fedora 43

πŸ’– MK-KILL(1) πŸ’–

User Contributed Perl Documentation

okay so bestie, welcome to mk-kill, and yes the manual name up there really is User Contributed Perl Documentation πŸŽ€ she's a Perl girlie, she's from the maatkit era, and she is here to end some queries. πŸ’…


✨ NAME ✨

mk-kill β€” kill MySQL queries that match certain criteria. periodt.


✨ SYNOPSIS ✨

Usage: mk-kill [OPTION]... [FILE...]

mk-kill kills MySQL connections. If you don't hand her a FILE, she connects to MySQL and pulls queries straight out of SHOW PROCESSLIST. If you DO hand her one or more FILEs, she reads queries from those instead β€” they need to contain the output of SHOW PROCESSLIST. And if FILE is -, she reads from STDIN. πŸ¦‹

Kill queries that have been running longer than 60s:

mk-kill --busy-time 60 --kill

Print them instead β€” no killing, just looking, very safe, very demure:

mk-kill --busy-time 60 --print

Check for sleeping processes and kill them ALL every 10s:

mk-kill --match-command Sleep --kill --victims all --interval 10

Print all login processes:

mk-kill --match-state login --print --victims all

Wanna see which queries in the processlist right now would match? Say less:

mysql -e "SHOW PROCESSLIST" | mk-kill --busy-time 60 --print

🌸 RISKS 🌸

This section exists to keep you informed about the potential risks β€” known or unknown β€” of using this tool. The two main categories of risk are: risks that come from the nature of the tool (like, read-only tools vs. read-write tools, iykyk) and risks that come from bugs. 🫢

mk-kill is literally designed to kill queries if you give it the --kill option, and yeah, that might disrupt your database's users, of course. πŸ™ˆ If you're not sure what the tool is gonna do, test with the --print option, which is safe.

At the time of this release, we know of no bugs that could cause serious harm to users. ✨

The authoritative source for updated info is always the online issue tracking system. Issues that affect this tool get marked as such. You can peep the list at:

<http://www.maatkit.org/bugs/mk-kill>

See also BUGS for more info on filing bugs and getting help. πŸ’œ


πŸ’… DESCRIPTION πŸ’…

mk-kill captures queries from SHOW PROCESSLIST, filters them, and then either kills them or prints them. Some circles call this a "slow query sniper" 🎯 β€” the whole idea is to watch for queries that might be eating too many resources, and then take them out.

For brevity we just say "killing queries," but depending on what options you give, they might just be printed (or some other future action). πŸ‘‘

Normally mk-kill connects to MySQL to get queries from SHOW PROCESSLIST. Alternatively, she can read SHOW PROCESSLIST output from files. In that case, mk-kill does not connect to MySQL and --kill has no effect β€” you should use --print instead when you're reading files. 🚨

The ability to read a file (or - for STDIN) lets you capture SHOW PROCESSLIST, then test it later with mk-kill to make sure your matches kill the proper queries. And bestie, there are a LOT of special rules to follow here β€” like "don't kill replication threads" β€” so be careful not to kill something important! 😭

Two options you absolutely need to know: --busy-time and --victims.

  1. Most match/filter options match their corresponding value from SHOW PROCESSLIST (like --match-command matches a query's Command value), but the Time value is matched by --busy-time. See also --interval. ⏰
  2. --victims controls which matching queries from each class get killed. By default, the matching query with the highest Time value is killed (the oldest query). More on this in the next section, GROUP, MATCH AND KILL. πŸ”₯

Usually you need to specify at least one --match option, otherwise nothing will match. Or, you can specify --match-all to match all queries that aren't ignored by an --ignore option.

mk-kill is a work in progress, and there is so much more she could do. 🧚


🌈 GROUP, MATCH AND KILL 🌈

Queries pass through several steps to figure out exactly which ones will be killed (or printed β€” whatever action is specified). Understanding these steps helps you match precisely the queries you want. Let's go step by step. πŸ’«

Step 1: Grouping πŸ’–

The first step is grouping queries into classes. The --group-by option controls grouping. By default this option has no value, so ALL queries get grouped into one big default class. Every type of matching and filtering (the next step) is applied per-class. So you may need to group queries in order to match/filter some classes but not others. πŸŽ€

Step 2: Matching 🌸

The second step is matching. Matching implies filtering, since if a query doesn't match some criteria, it gets removed from its class. Matching happens for each class.

  1. First, queries are filtered from their class by the various "Query Matches" options, like --match-user. πŸ¦‹
  2. Then, entire classes are filtered by the various "Class Matches" options, like --query-count. πŸ’œ

Step 3: Victim selection πŸ’…

The third step is victim selection β€” that is, which matching queries in each class to kill. This is controlled by the --victims option. Even though many queries in a class may match, you might only want to kill the oldest one, or all of them, etc. πŸ”ͺ

Step 4: Action ✨

The fourth and final step is to take some action on all matching queries from all classes. The "Actions" options specify which actions get taken. At this step there are no more classes β€” just a single list of queries to kill, print, etc. πŸ‘‘


πŸŽ€ OUTPUT πŸŽ€

If you only pass --kill, there is no output. If you only pass --print, then a timestamped KILL statement gets printed for every query that would have been killed, like:

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

That line shows a timestamp, the query's Id (8), its Time (42 sec) and its Info (usually the query SQL). ⏰

If both --kill and --print are given, then matching queries are killed and a line like the one above is printed for each one. πŸ’–

Any command executed by --execute-command is responsible for its own output and logging. Once it's executed, mk-kill has no control or interaction with the command whatsoever. πŸ™ˆ


✨ OPTIONS ✨

Specify at least one of --kill, --kill-query, --print, --execute-command or --stop. (Pick your fighter. πŸ’…)

--any-busy-time and --each-busy-time are mutually exclusive. 🚫

--kill and --kill-query are mutually exclusive. 🚫

This tool accepts additional command-line arguments. Refer to the SYNOPSIS and usage information for details. 🫢


  • --ask-pass β€” Prompt for a password when connecting to MySQL. πŸ”
  • --charset β€” short form: -A; type: string Default character set. If the value is utf8, it sets Perl's binmode on STDOUT to utf8, passes the mysql_enable_utf8 option to DBD::mysql, and runs SET NAMES UTF8 after connecting to MySQL. Any other value sets binmode on STDOUT without the utf8 layer, and runs SET NAMES after connecting to MySQL. 🌈
  • --config β€” type: Array Read this comma-separated list of config files; if specified, this must be the first option on the command line. πŸ“‹
  • --daemonize β€” Fork to the background and detach from the shell. POSIX operating systems only. πŸ‘»
  • --defaults-file β€” short form: -F; type: string Only read mysql options from the given file. You must give an absolute pathname. πŸ“
  • --group-by β€” type: string Apply matches to each class of queries grouped by this SHOW PROCESSLIST column. In addition to the basic columns of SHOW PROCESSLIST (user, host, command, state, etc.), queries can be matched by fingerprint, which abstracts the SQL query in the Info column. 🧚

By default, queries are not grouped, so matches and actions apply to all queries. Grouping allows matches and actions to apply to classes of similar queries, if any queries in the class match.

For example, detecting cache stampedes (see all-but-oldest under --victims for an explanation of that term) requires that queries are grouped by the arg attribute. This creates classes of identical queries (stripped of comments). So queries SELECT c FROM t WHERE id=1 and SELECT c FROM t WHERE id=1 are grouped into the same class, but query SELECT c FROM t WHERE id=3 is not identical to the first two queries, so it gets grouped into another class. Then when --victims all-but-oldest is specified, all but the oldest query in each class is killed, for each class of queries that matches the match criteria. πŸ’₯

  • --help β€” Show help and exit. πŸ™‹
  • --host β€” short form: -h; type: string; default: localhost Connect to host. 🌐
  • --interval β€” type: time How often to check for queries to kill. If --busy-time is not given, then the default interval is 30 seconds. Else the default is half as often as --busy-time. If both --interval and --busy-time are given, then the explicit --interval value is used. ⏰

See also --run-time.

  • --log β€” type: string Print all output to this file when daemonized. πŸ“
  • --password β€” short form: -p; type: string Password to use when connecting. πŸ”‘
  • --pid β€” type: string Create the given PID file when daemonized. The file contains the process ID of the daemonized instance. The PID file is removed when the daemonized instance exits. The program checks for the existence of the PID file when starting; if it exists and the process with the matching PID exists, the program exits. πŸšͺ
  • --port β€” short form: -P; type: int Port number to use for connection. πŸ”Œ
  • --[no]strip-comments β€” default: yes Remove SQL comments from queries in the Info column of the PROCESSLIST. 🧼
  • --run-time β€” type: time How long to run before exiting. By default mk-kill runs forever, or until its process is killed or stopped by the creation of a --sentinel file. If this option is specified, mk-kill runs for the specified amount of time and sleeps --interval seconds between each check of the PROCESSLIST. ⏳
  • --sentinel β€” type: string; default: /tmp/mk-kill-sentinel Exit if this file exists. πŸ›‘

The presence of the file specified by --sentinel will cause all running instances of mk-kill to exit. You might find this handy to stop cron jobs gracefully if necessary. See also --stop.

  • --set-vars β€” type: string; default: wait_timeout=10000 Set these MySQL variables. Immediately after connecting to MySQL, this string will be appended to SET and executed. βš™οΈ
  • --socket β€” short form: -S; type: string Socket file to use for connection. πŸ”—
  • --stop β€” Stop running instances by creating the --sentinel file. βœ‹

Causes mk-kill to create the sentinel file specified by --sentinel and exit. This should have the effect of stopping all running instances which are watching the same sentinel file.

  • --user β€” short form: -u; type: string User for login if not current user. πŸ™‹
  • --version β€” Show version and exit. ⭐
  • --victims β€” type: string; default: oldest

Okay so which queries actually get the axe? After classes have been matched/filtered, this option says which of the matching queries in each class will be killed (or printed, etc.). The possible values are:

  • oldest β€” Only kill the single oldest query. This is to prevent killing queries that aren't really long-running, they're just long-waiting. It sorts matching queries by Time and kills the one with the highest Time value.
  • all β€” Kill all queries in the class.
  • all-but-oldest β€” Kill all but the oldest query. This is the inverse of the oldest value.

This value can be used to prevent "cache stampedes", the condition where several identical queries are executed and create a backlog while the first query attempts to finish. Since all queries are identical, all but the first query are killed so that it can complete and populate the cache.

  • --wait-after-kill β€” type: time β€” Wait after killing a query, before looking for more to kill. The purpose of this is to give blocked queries a chance to execute, so we don't kill a query that's blocking a bunch of others, and then kill the others immediately afterwards.
  • --wait-before-kill β€” type: time β€” Wait before killing a query. The purpose of this is to give --execute-command a chance to see the matching query and gather other MySQL or system information before it's killed.

✨ QUERY MATCHES ✨

These options filter queries from their classes. If a query does not match, it is removed from its class. The --ignore options take precedence. The matches for command, db, host, etc. correspond to the columns returned by SHOW PROCESSLIST: Command, db, Host, etc. All pattern matches are case-sensitive by default, but they can be made case-insensitive by specifying a regex pattern like (?i-xsm:select).

See also "GROUP, MATCH AND KILL".

  • --match-all β€” group: Query Matches β€” Match all queries that are not ignored. If no ignore options are specified, then every query matches (except replication threads, unless --replication-threads is also specified). This option allows you to specify negative matches, i.e. "match every query except..." where the exceptions are defined by specifying various --ignore options.

This option is not the same as --victims all. This option matches all queries within a class, whereas --victims all specifies that all matching queries in a class (however they matched) will be killed. Normally, however, the two are used together because if, for example, you specify --victims oldest, then although all queries may match, only the oldest will be killed.

  • --busy-time β€” type: time; group: Query Matches β€” Match queries that have been running for longer than this time. The queries must be in Command=Query status. This matches a query's Time value as reported by SHOW PROCESSLIST.
  • --idle-time β€” type: time; group: Query Matches β€” Match queries that have been idle/sleeping for longer than this time. The queries must be in Command=Sleep status. This matches a query's Time value as reported by SHOW PROCESSLIST.
  • --ignore-command β€” type: string; group: Query Matches β€” Ignore queries whose Command matches this Perl regex.

See --match-command.

  • --ignore-db β€” type: string; group: Query Matches β€” Ignore queries whose db (database) matches this Perl regex.

See --match-db.

  • --ignore-host β€” type: string; group: Query Matches β€” Ignore queries whose Host matches this Perl regex.

See --match-host.

  • --ignore-info β€” type: string; group: Query Matches β€” Ignore queries whose Info (query) matches this Perl regex.

See --match-info.

  • --[no]ignore-self β€” default: yes; group: Query Matches β€” Don't kill mk-kill's own connection.
  • --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.

See --match-state.

  • --ignore-user β€” type: string; group: Query Matches β€” Ignore queries whose user matches this Perl regex.

See --match-user.

  • --match-command β€” type: string; group: Query Matches β€” Match only queries whose Command matches this Perl regex.

Common Command values are:

  • 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 β€” type: string; group: Query Matches β€” Match only queries whose State matches this Perl regex.

Common State values are:

  • 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.


πŸŽ€ CLASS MATCHES πŸŽ€

These matches apply to entire query classes. Classes are created by specifying the --group-by option, else all queries are members of a single, default class.

See also "GROUP, MATCH AND KILL".

  • --any-busy-time β€” type: time; group: Class Matches β€” Match query class if any query has been running for longer than this time. "Longer than" means that if you specify 10, for example, the class will only match if there's at least one query that has been running for greater than 10 seconds.

See --each-busy-time for more details.

  • --each-busy-time β€” type: time; group: Class Matches β€” Match query class if each query has been running for longer than this time. "Longer than" means that if you specify 10, for example, the class will only match if each and every query has been running for greater than 10 seconds.

See also --any-busy-time (to match a class if ANY query has been running longer than the specified time) and --busy-time.

  • --query-count β€” type: int; group: Class Matches β€” Match query class if it has at least this many queries. When queries are grouped into classes by specifying --group-by, this option causes matches to apply only to classes with at least this many queries. If --group-by is not specified then this option causes matches to apply only if there are at least this many queries in the entire SHOW PROCESSLIST.
  • --verbose β€” short form: -v β€” Print information to STDOUT about what is being done.

πŸ’… ACTIONS πŸ’…

These actions are taken for every matching query from all classes. The actions are taken in this order: --print, --execute-command, --kill/--kill-query. This order allows --execute-command to see the output of --print and the query before --kill/--kill-query. This may be helpful because mk-kill does not pass any information to --execute-command.

See also "GROUP, MATCH AND KILL".

  • --execute-command β€” type: string; group: Actions β€” Execute this command when a query matches.

After the command is executed, mk-kill has no control over it, so the command is responsible for its own info gathering, logging, interval, etc. The command is executed each time a query matches, so be careful that the command behaves well when multiple instances are ran. No information from mk-kill is passed to the command.

See also --wait-before-kill.

  • --kill β€” group: Actions β€” Kill the connection for matching queries.

This option makes mk-kill kill the connections (a.k.a. processes, threads) that have matching queries. Use --kill-query if you only want to kill individual queries and not their connections.

Unless --print is also given, no other information is printed that shows that mk-kill matched and killed a query.

See also --wait-before-kill and --wait-after-kill.

  • --kill-query β€” group: Actions β€” Kill matching queries.

This option makes mk-kill kill matching queries. This requires MySQL 5.0 or newer. Unlike --kill which kills the connection for matching queries, this option only kills the query, not its connection.

  • --print β€” group: Actions β€” Print a KILL statement for matching queries; does not actually kill queries.

If you just want to see which queries match and would be killed without actually killing them, specify --print. To both kill and print matching queries, specify both --kill and --print.

✨ DSN OPTIONS ✨

okay so these DSN options are what you use to build a DSN, bestie. each one goes in like "option=value" πŸ’«

heads up: the options are case-sensitive, so P and p are NOT the same option, like at all. do not play with me.

and listen β€” there can be no whitespace before or after the "=", and if the value itself contains whitespace it must be quoted. DSN options are comma-separated. for the full tea, go check the maatkit manpage πŸ“–


  • A
    • dsn: charset; copy: yes
    • default character set πŸ’œ
  • D
    • dsn: database; copy: yes
    • default database πŸ—‚οΈ
  • F
    • dsn: mysql_read_default_file; copy: yes
    • only read default options from the given file πŸ“„
  • h
    • dsn: host; copy: yes
    • connect to host 🌐
  • p
    • dsn: password; copy: yes
    • password to use when connecting 🀫
  • P
    • dsn: port; copy: yes
    • port number to use for connection πŸšͺ
  • S
    • dsn: mysql_socket; copy: yes
    • socket file to use for connection πŸ”Œ
  • u
    • dsn: user; copy: yes
    • user for login if not current user πŸ™‹

🌈 DOWNLOADING 🌈

you can grab Maatkit from Google Code at <http://code.google.com/p/maatkit/>, or honestly just snag any of the tools with a command like one of these cuties:

wget http://www.maatkit.org/get/toolname
or
wget http://www.maatkit.org/trunk/toolname

where "toolname" can be swapped for the name (or even just a fragment of a name) of literally any of the Maatkit tools πŸ‘‘

and the best part? once they're downloaded they're ready to run β€” no installation needed, periodt πŸ’…

the first URL gets you the latest released version, and the second gets you the latest trunk code from Subversion. iykyk ✨


🫢 ENVIRONMENT 🫢

the environment variable "MKDEBUG" turns on verbose debugging output in all of the Maatkit tools:

MKDEBUG=1 mk-....

she's loud, she's chatty, she tells you everything πŸ”


πŸ’€ SYSTEM REQUIREMENTS πŸ’€

you need Perl, DBI, DBD::mysql, and some core packages that ought to be installed in any reasonably new version of Perl. no cap. that's the whole list 🧚


πŸ™ˆ BUGS πŸ™ˆ

for a list of known bugs, look at <http://www.maatkit.org/bugs/mk-kill> πŸ›

please use Google Code Issues and Groups to report bugs or request support: <http://code.google.com/p/maatkit/>. you can also join #maatkit on Freenode to discuss Maatkit πŸ’¬

and when you report something, please include:

  • the complete command-line you used to reproduce the problem you're seeing
  • the version of all MySQL servers involved
  • the complete output of the tool when run with "--version"
  • and if possible, debugging output produced by running with the "MKDEBUG=1" environment variable

give us the receipts, bestie, we can't fix what we can't see πŸŽ€


this program is copyright 2009-2011 Baron Schwartz. feedback and improvements are welcome! 🫢

THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

okay so the legal bit, fr fr: this program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2; OR the Perl Artistic License. on UNIX and similar systems, you can issue man perlgpl or man perlartistic to read these licenses πŸ“š

you should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA βœ‰οΈ


⭐ AUTHOR ⭐

Baron Schwartz, Daniel Nichter β€” iconic duo energy πŸ‘―


🌸 ABOUT MAATKIT 🌸

this tool is part of Maatkit, a toolkit for power users of MySQL. Maatkit was created by Baron Schwartz; Baron and Daniel Nichter are the primary code contributors. both are employed by Percona. financial support for Maatkit development is primarily provided by Percona and its clients πŸ’–


πŸŽ€ VERSION πŸŽ€

this manual page documents Ver 0.9.10 Distrib 7540 $Revision: 7531 $ ⭐


aaaand that's a wrap on the whole page, bestie πŸ¦‹ this one's rendered from the perl v5.42.0 docs, dated 2025-07-24, filed under MK-KILL(1) β€” go forth and slay, you're literally a Unix main character now πŸ’…βœ¨

πŸ“¦ where this page comes from