Kill All Users in MySQL


MySQL – Script to Kill A User Session

I googled around and found some code that could be used with a few tweaks to kill all users EXCEPT the User(s) you specify.

#vi kill_all_users.sh
#!/bin/bash
connections=$(mysql -h$1 -u$2 -p$3 -e "SHOW PROCESSLIST" |grep -v wmayall| grep -v Id | grep -v root|awk '{print $1}' )
for id in $connections
do
    mysql -h$1 -u$2 -p$3 -e "KILL $id"
done

#chmod 700 kill_all_users.sh

The syntax is:

./kill_all_users.sh  –host –user –password:

Below is an example:

./kill_all_users.sh localhost root my_password

The grep –v excludes the User you do NOT want to kill their session.  

If the User is connected through ODBC, a new session could be created automatically, in which case you would want to block connections to that port and re-issue the kill_all_users.sh script.




Comments

Popular posts from this blog

PostgreSQL Database Version 13.4 To MySQL Database Version 8.0.20 Migration by using SQLines Tool

RDS MySQL / MariaDB SSL Connection by using Workbench and command line

Install Mydumper and Myloader Software for Backup of MySQL / MariaDB Databases and Usage of commands