MySQL "Too Many Connections" means all available connection slots are used. This causes immediate downtime for all applications using the database.
Step 1 — Emergency Fix (Immediate)
# Connect as root (one connection is reserved for root)
mysql -u root -p
# Check current connections
SHOW STATUS LIKE 'Threads_connected';
SHOW PROCESSLIST;
# Kill idle connections immediately
SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist
WHERE command='Sleep' AND time > 30;
-- Run the generated KILL statements
Step 2 — Increase Max Connections
# Temporary fix (until restart)
SET GLOBAL max_connections = 500;
# Permanent fix
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
max_connections = 500
wait_timeout = 60
interactive_timeout = 60
sudo systemctl restart mysql
Step 3 — Find Connection Leaks
# Which users/hosts are using most connections
SELECT user, host, COUNT(*) as connections
FROM information_schema.processlist
GROUP BY user, host
ORDER BY connections DESC;
# Check for sleeping connections
SELECT COUNT(*) FROM information_schema.processlist WHERE command = 'Sleep';
Step 4 — Fix WordPress Connection Leaks
# wp-config.php — add connection timeout
define('DB_CHARSET', 'utf8mb4');
# Check for plugins holding connections open
wp plugin list --status=active
Step 5 — Set Up Connection Pooling
# Install ProxySQL for connection pooling
sudo apt install proxysql -y
# Or use PgBouncer alternative: mysqlrouter
sudo apt install mysql-router -y
Step 6 — Monitor Connections
# Watch connections in real time
watch -n 1 "mysql -u root -p'password' -e 'SHOW STATUS LIKE "Threads_connected";'"
Conclusion
Connection exhaustion needs both immediate fix and root cause analysis. Our team resolves database emergencies and implements permanent solutions.
Comments