KILL [CONNECTION | QUERY] thread_id
Each connection to mysqld runs in a separate
thread. You can see which threads are running with the
SHOW PROCESSLIST statement and kill a thread
with the KILL
statement.
thread_id
KILL allows the optional
CONNECTION or QUERY
modifier:
KILL CONNECTION is the same as
KILL with no modifier: It terminates the
connection associated with the given
thread_id.
KILL QUERY terminates the statement that
the connection is currently executing, but leaves the
connection itself intact.
If you have the PROCESS privilege, you can
see all threads. If you have the SUPER
privilege, you can kill all threads and statements. Otherwise,
you can see and kill only your own threads and statements.
You can also use the mysqladmin processlist and mysqladmin kill commands to examine and kill threads.
You cannot use KILL with the Embedded MySQL
Server library, because the embedded server merely runs inside
the threads of the host application. It does not create any
connection threads of its own.
When you use KILL, a thread-specific kill
flag is set for the thread. In most cases, it might take some
time for the thread to die, because the kill flag is checked
only at specific intervals:
In SELECT, ORDER BY
and GROUP BY loops, the flag is checked
after reading a block of rows. If the kill flag is set, the
statement is aborted.
During ALTER TABLE, the kill flag is
checked before each block of rows are read from the original
table. If the kill flag was set, the statement is aborted
and the temporary table is deleted.
During UPDATE or
DELETE operations, the kill flag is
checked after each block read and after each updated or
deleted row. If the kill flag is set, the statement is
aborted. Note that if you are not using transactions, the
changes are not rolled back.
GET_LOCK() aborts and
returns NULL.
An INSERT DELAYED thread quickly flushes
(inserts) all rows it has in memory and then terminates.
If the thread is in the table lock handler (state:
Locked), the table lock is quickly
aborted.
If the thread is waiting for free disk space in a write call, the write is aborted with a “disk full” error message.
Killing a REPAIR TABLE or
OPTIMIZE TABLE operation on a
MyISAM table results in a table that is
corrupted and unusable. Any reads or writes to such a
table fail until you optimize or repair it again (without
interruption).

User Comments
Here the sample script to kill automatic the mysql user. I used this and my mysql server keep alive and not hang again.
$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["Time"] > 200 ) {
$sql="KILL $process_id";
mysql_query($sql);
}
}
Note that if you have to kill an ALTER TABLE command, while the documentation says that it sets a KILL flag, I think this seems to only works when copying over to a new table (and not building the index).
I just did this on a 8G table... it was in REPAIR BY SORTING at the time and it took another 2 or 3 minutes to kill.
The original table was then restored.
The status of the task said KILLED though. Its possible that it checks the status less often during the repair.
FYI.
This should make it easy for windows based mysql installations but can be used on either system (its a php file).
It will remove all user's threads (who aren't root). Note: its just an adaption from a previous script, just complete for copying.
This is a VERY handy class to re-use in future scripts.
<?php
class dbMysql
{
var $host, $user, $pass, $conn;
var $errNum, $errMsg;
var $rowCount, $qryType;
function dbMysql($host, $user, $pass)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->rowCount = 0;
$this->errNum = 0;
$this->errMsg = '';
$this->qryType = 0;
}
function open()
{
if (!$this->conn = @mysql_connect($this->host,$this->user,$this->pass))
{
$this->errNum = 2;
$this->errMsg = 'Cannot Connect to DataBase Server';
return false;
}
$this->errNum = 0;
$this->errMsg = 'OK';
return true;
}
function select($dbName)
{
if (!(@mysql_select_db($dbName,$this->conn)))
{
$this->errNum = 2;
$this->errMsg = 'Cannot Select Requested DataBase';
return false;
}
$this->errNum = 0;
$this->errMsg = 'OK';
return true;
}
function getAutoId()
{
return @mysql_insert_id($this->conn);
}
function getResultCount($result)
{
return ($this->qryType) ? @mysql_num_rows($result) : @mysql_affected_rows($this->conn);
}
function query($qryStr)
{
eregi('^SELECT',$qryStr) ? $this->qryType = 0 : $this->qryType = 1;
return @mysql_query($qryStr,$this->conn);
}
function fetch($result)
{
return @mysql_fetch_object($result);
}
function close()
{
@mysql_close($this->conn);
}
}
/* START HERE - ADD HOST USER AND PASSSWORD */
$db = new dbMysql("servername", "username", "password");
if (!$db->open())
{ print("ERROR: " . $db->errNum . " -- " . $db->errMsg); }
$result = $db->query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["User"] != "root" ) {
print("KILLED: " . $process_id);
$sql="KILL $process_id";
mysql_query($sql);
}
}
if (!$result)
{
$db->close();
print("ERROR: " . $db->errNum . " -- " . $db->errMsg);
}
$db->close();
?>
I think you should resolve the problem of your low performance instead of killing everything all time.
You can starting saving all killed querys in a table, so you can see how optimize them later. MySQL has a list of optimizations you can do (like removing unnecesary '()'s, removing 'ORDER BY' clause when not needed, etc).
Add your own comment.