How to Kill All MySQL Sleeping Processes

for i in `mysql -e "show processlist" | awk '/Sleep/ {print $1}'` ; do mysql -e "KILL $i;"; done

Having a lot of MySQL sleeping processes can cause a huge spike in your CPU load.

Here is a simple one-line command which would kill all of the current sleeping MySQL processes:

for i in `mysql -e "show processlist" | awk '/Sleep/ {print $1}'` ; do mysql -e "KILL $i;"; done

Note that this is a temporary solution, I would highly recommend investigating and fixing the root cause of the problem. For example, you could configure the wait_timeout variable to a specific time that MySQL should wait before closing idle connections.

Hope that it helps.

Snippet By Dev Dojo

Β·

Created June 12th, 2021