MySQL - KILL Statement



Using the MySQL KILL statement you can kill the specified thread.

Syntax

Following is the syntax od the MySQL KILL statement −

KILL [CONNECTION | QUERY] processlist_id

Where, processlist_id is the id of the process we need to kill −

Example

Assume we have created a user named sample using the CREATE statement as shown below −

CREATE USER sample;

If you verify the current processes list using the SHOW statement, you can find three processes running. The MySQL command prompt root user (if you are running), MySQL sample user (connected from command prompt) and the event_scheduler.

SHOW PROCESSLIST;

Output

The above query produces the following output −

Id User Host db Command Time State
5 event_ scheduler localhost NULL Daemon 2065 Waiting for next activation
9 root localhost: 49996 test Query 0 init
11 sample localhost: 58238 NULL Sleep 55

Following query kills the process with id 11

KILL 11;

After killing the process sample if you verify the process list you can observe that there are only two processes left −

SHOW PROCESSLIST;

Output

Once the query is executed, it will display the following output −

Id User Host db Command Time State
5 event_ scheduler localhost NULL Daemon 2217 Waiting for next activation
9 root localhost: 49996 test Query 0 init

You can kill a connection using the CONNECTION clause

SHOW PROCESSLIST;

Output

The above query produces the following output −

Id User Host db Command Time State
5 event_ scheduler localhost NULL Daemon 2421 Waiting for next activation
9 root localhost: 49996 test Query 0 init
15 sample localhost: 58275 NULL Sleep 89
KILL CONNECTION 14;

You can kill a query using the QUERY clause with this statement −

SHOW PROCESSLIST;

Output

Following is the output of the above query −

Id User Host db Command Time State
5 event_ scheduler localhost NULL Daemon 2720 Waiting for next activation
9 root localhost: 49996 test Query 0 init
15 sample localhost: 58330 NULL Sleep 9
Advertisements