1. Check the current total number of connections
SHOW STATUS LIKE 'Threads_connected';
- Returns the total number of currently established connections
2. Check the maximum number of connections configuration
SHOW VARIABLES LIKE 'max_connections';
- Displays the maximum number of concurrent connections allowed by the server
3. View detailed connection information
SHOW PROCESSLIST;
- Shows details of all connections (user, source IP, executed SQL, etc.)
4. Statistics the number of connections by user grouping
SELECT user, COUNT(*) as connections
FROM information_schema.processlist
GROUP BY user;
5. Check the number of connections usage
SELECT (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Threads_connected') AS current_connections, (SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME = 'max_connections') AS max_connections, ROUND((SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Threads_connected') / (SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME = 'max_connections') * 100, 2) AS connection_usage_rate;
6. Check the number of connections in different states
SELECT command, COUNT(*)
FROM information_schema.processlist
GROUP BY command;
7. Check the number of idle connections (Sleep status)
SELECT COUNT(*) FROM information_schema.processlist WHERE command = 'Sleep';
Notes:
- Requires at least PROCESS permission to view all connections
- When the number of connections in the production environment is close to max_connections, it needs to be expanded or optimized.
- For long-term Sleep connections, you can consider appropriately lowering them.
wait_timeout
Parameters
If you need to terminate the connection, you can use:
KILL [connection_id]; -- fromSHOW PROCESSLISTObtained in the resultsID