How to Kill All MySQL Sleeping Processes

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.


© 2020 DevDojo LLC. All rights reserved.