Where the open source database community meets: Use code PERCONA75 and secure your spot for Percona Live.  Register

Terminate MySQL Connections

May 21, 2009
Author
Peter Zaitsev
Share this Post:

Every so often I run into a situation when I need to terminate connections on MySQL server – for example, hundreds of instances of some bad query is running, making the server unusable. Many people have special scripts which can take the user, source host, or query as a parameter and perform the action. There is also a way to do it just using MySQL with a few commands:

In general, this is a very powerful approach which I use in a lot of cases to create a set of SQL statements by SQL query and then execute it.

It would be nice and clean if MySQL would have some way to “eval” – execute the result set of the query as SQL commands. This would avoid the requirement to use temporary file etc.

0 0 votes
Article Rating
Subscribe
Notify of
guest

35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
http://krow.livejournal.com/

You are not the only one. I’ve thought that this would be handy several times.

Create a session variable and then make it so that it can be parsed by the parser?

Not terribly hard.

-Brian

Sergey
Sergey
16 years ago

Could you please update your post to point out that this feature is 5.1+?

Robert Wultsch
Robert Wultsch
16 years ago

I prefer the following as it will kill them in a multi threaded manner… (sometimes killing a single query can take a while)

for i in $(mysql -uroot -pPASS -e ‘show processlist’ | grep ‘search_term’ | awk ‘{print $1}’); do
mysql -uroot -pPASS -e “kill $i” &
done

Robert Wultsch
Robert Wultsch
16 years ago

Peter,
Couldn’t you use prepared statements as an eval?

Nils
16 years ago

could also be a modifier to the client instead of a special sql construct (much like G)

Shlomi Noach
16 years ago

Hi Peter,

With INFORMATION_SCHEMA.PROCESSLIST one can create a stored procedure using server cursor to achieve this. The following procedure accepts a user name and kills all queries for that user.
server cursors are a MySQL solution for ‘eval’.

Shlomi

DELIMITER $$

DROP PROCEDURE IF EXISTS test.kill_user_queries$$
CREATE PROCEDURE test.kill_user_queries (kill_user_name VARCHAR(16) CHARSET utf8)
SQL SECURITY INVOKER
BEGIN
DECLARE query_id INT;
DECLARE iteration_complete INT DEFAULT 0;
DECLARE select_cursor CURSOR FOR SELECT id FROM INFORMATION_SCHEMA.PROCESSLIST WHERE user=kill_user_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET iteration_complete=1;

OPEN select_cursor;
cursor_loop: LOOP
FETCH select_cursor INTO query_id;
IF iteration_complete THEN
LEAVE cursor_loop;
END IF;
KILL QUERY query_id;
END LOOP;
CLOSE select_cursor;

END$$

DELIMITER ;

Shlomi Noach
16 years ago

Peter,

I agree. I think there are some general-purpose stored procedures kits around. There are also many useful queries+views by Roland Bouman that I know of.
Would be nice to gather them all into a “utilities” schema.

PS a prepared statement will not allow “KILL” or “OPTIMIZE” or “ANALYZE” queries. Just the normal SELECT/INSERT/UPDATE/…

Chip Turner
Chip Turner
16 years ago

Check out mypgrep.py from the google mysql tools project on code.google.com. It basically is a supercharged process grep for mysql, including the ability to kill processes.

Peter Stuifzand
16 years ago

A few years ago I wrote an article about how to use Vim with mysql to generate and execute SQL statements. It’s here: http://peterstuifzand.nl/external-programs-in-vim.html

Bryan
Bryan
16 years ago

If information_schema.processlist doesn’t exist on your version of MySQL, this works in a linux script:

#!/bin/bash
for each in mysqladmin -u root -prootpwd processlist | awk '{print $2, $4, $8}' | grep $dbname | grep $dbuser | awk '{print $1}';
do mysqladmin -u root -prootpwd kill $each;
done

Bryan
Bryan
16 years ago

No Percona patches yet, unfortunately. We’re running the standard MySQL 5.0.77 on Centos.

Glynn Durham
Glynn Durham
16 years ago

How about a technique like this:

. File optimizeworld.sfs:

# Optimize tables in the World database.
SELECT ‘OPTIMIZE TABLE ‘, CONCAT(TABLE_SCHEMA, ‘.’, TABLE_NAME, ‘;’)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = ‘World’;

. To execute:

shell> mysql –skip-column-names < optimizeworld.sfs | mysql

Andrew Watson
Andrew Watson
16 years ago

I do this:

mysqladmin proc | grep Sleep | sort -r -n -k6 | awk {‘print $1; ‘} | xargs mysqladmin kill

or something to that effect…

Brent William Peterson
16 years ago

I am wondering if there isn’t a symptom that is missing? I am running a high volume ecommerce site and we switched to Magento from custom app written in vb.net (.net 1.1). Shouldn’t MySql handle all of this? What I am wondering is why my old windows box running doesn’t crash with the same load as my high powered linux box?

Ahmad Ali
Ahmad Ali
16 years ago

I am facing the problem
Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/road2l/public_html/connect.php on line 15
Error connecting to mysqlToo many connections
Please help me out.

haberler
14 years ago

I used the command and the query being run in mysql processlist still running for 28226 and l cant kill it with everything l did. what might else l should try ?
thanks.

Kirill
Kirill
13 years ago

Thank you Peter for Percona and for this post!

Hitesh Agja
13 years ago

Thanks Peter. Query is really very helpful to me.

Jitender
Jitender
13 years ago

Hi, i am using mysql 5.0 which does not have processlist table in information_schema, now is there any way i can list all query which is taking more then x time. Any script will be really appriciated.

sheyda
sheyda
13 years ago

This may not directly related to this chain, but I landed here to kill massive connection due to the hung drop trigger – I have a dilemma with dropping a trigger on a percona 5.5.27 on a very large table ~ 68 Gig. I get the metadata lock and it hangs.

I have tried everything. Is it safe to remove the trigger from DB.TRG file? Thanks a lot

Alon
Alon
13 years ago

ERROR 1146 (42S02): Table ‘infromation_schema.processlist’ doesn’t exist

Shlomi Noach
13 years ago

‘@Alon,
You have a typo. Should be information_schema.processlist

Aptitude
12 years ago

Thank you.. very informative as all your articles.. keep it up.

Aptitude
12 years ago

Very informative as all your articles.. keep it up.

scriptime
12 years ago

i am also wiring about mysql administration on my blog

Ben
Ben
7 years ago

Great tip, thanks!

Utpal
7 years ago

Thanks for this post. Nice tips to kill mysql connections.

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved