MySQL Process Management
Number of Connections
show status where variable_name = 'threads_connected';
Connectionsshow status where variable_name = 'threads_running';
Active ConnectionsSHOW VARIABLES LIKE "max_connections";
Connection LimitSHOW VARIABLES LIKE "max_used_connections";
High Water MarkRunning Processes
show processlist;
or...
SELECT id,
user,
host,
db,
command,
time,
state,
info
FROM information_schema.processlist;
d - change refresh intervale - explain a queryf - show full queryr - reverse sort orderi - toggle idle processesk - kill a query
Kill Processes
KILL 9999;
Where 9999 is the id from processlistKill All Processes Running for Longer than a Specified Time for a Specified User
SELECT CONCAT('KILL ',id,';')
FROM information_schema.processlist
WHERE user='myuser'
AND time > 1800
INTO OUTFILE '/mysql/mysql-files/myfile.txt';
1800 seconds equates to 30 minutesSOURCE /mysql/mysql-files/myfile.txt;
Kill All SELECT Queries for a Specified User
SELECT CONCAT('KILL ',id,';')
FROM information_schema.processlist
WHERE user='myuser'
AND info LIKE 'SELECT%'
INTO OUTFILE '/mysql/mysql-files/myfile.txt';
SOURCE /mysql/mysql-files/myfile.txt;
Kill All Processes for a Specified User
SELECT CONCAT('KILL ',id,';')
FROM information_schema.processlist
WHERE user='myuser'
INTO OUTFILE '/mysql/mysql-files/myfile.txt';
SOURCE /mysql/mysql-files/myfile.txt;
For mariaDB only...
KILL USER myuser;
Python
import pymysql
connection = pymysql.connect(host='localhost',
user='myuser',
db='mydb',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
cursor.execute('SHOW PROCESSLIST')
for item in cursor.fetchall():
if item.get('Time') > 1800:
_id = item.get('Id')
print('kill %s' % item)
cursor.execute('kill %s', _id)
connection.close()
PHP
$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["Time"] > 1800) {
$sql="KILL $process_id";
mysql_query($sql);
}
}
TCP/IP or Socket
To determine if a connection is using a TCP/IP connection or a UNIX Socket use...
netstat -ln | grep 'mysql'
Useful Tables/Views
information_schema.processlist
performance_schema.threads
Bibliography
https://unix.stackexchange.com/questions/32134/how-can-i-determine-the-connection-method-used-by-a-mysql-clienthttps://dba.stackexchange.com/questions/241152/whats-the-difference-between-a-thread-vs-a-processhttps://alvinalexander.com/blog/post/mysql/how-show-open-database-connections-mysql/https://stackoverflow.com/questions/7432241/mysql-show-status-active-or-total-connectionshttps://stackoverflow.com/questions/1903838/how-do-i-kill-all-the-processes-in-mysql-show-processlist
Processeshttps://dataedo.com/kb/query/mysql/list-database-sessions