Fix MySQL ERROR 1040: Too many connections
MySQL ERROR 1040 Too many connections means max_connections is used up. Use the reserved admin slot, kill leaked idle connections, then size the limit.
ERROR 1040 (08004): Too many connectionsThe server has reached max_connections (151 by default in 8.4) and refuses new connections. The usual cause is an application that opens connections and never closes them, so log in as an administrator first, find who holds the connections, free them, and only then touch the limit. Raising the limit alone just delays the next outage.
The same error also appears as ERROR 1040 (HY000): Too many connections. In testing, a normal account got 08004 and an admin blocked after the reserved slot was taken got HY000. The error reference lists SQLSTATE 08004.
[1] Try an admin account. mysqld accepts max_connections + 1 connections, and the extra one is reserved for accounts with CONNECTION_ADMIN (or the deprecated SUPER) (Too many connections). Below, max_connections=5 and five application connections sit idle.
$ mysql -h 127.0.0.1 -uapp -p'<password>' appdb -e "SELECT 1;"mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1040 (08004): Too many connections$ mysql -uroot -p'<password>' -e "SELECT CURRENT_USER();"mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER()root@localhost$ mysql -uroot -p'<password>' -e "SELECT 1;"mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1040 (HY000): Too many connections[2] Enable the dedicated admin interface for next time. With admin_address set at startup, the server accepts administrative connections on a separate address and port (default 33062); that interface has no connection limit and only admits accounts with SERVICE_CONNECTION_ADMIN (Administrative Connection Management). It is only enabled at startup, so it cannot help a server that is already full; it applies from the next restart.
[mysqld]admin_address=127.0.0.1admin_port=33062The test server was started with the same values as options, --admin-address=127.0.0.1 --admin-port=33062. With even the reserved slot taken, the admin port still let root in.
$ mysql -h 127.0.0.1 -P 33062 -uroot -p'<password>' -e "SELECT CURRENT_USER(), @@admin_port;"mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER() @@admin_portroot@% 33062[3] Check the numbers. If Max_used_connections has reached max_connections and Connection_errors_max_connections keeps growing, this error really happened. From here on, A holds the admin-port options above.
$ A="-h 127.0.0.1 -P 33062 -uroot -p<password>"$ mysql $A -e "SHOW GLOBAL STATUS WHERE Variable_name IN ('Threads_connected','Max_used_connections','Connection_errors_max_connections');"mysql: [Warning] Using a password on the command line interface can be insecure.Variable_name ValueConnection_errors_max_connections 2Max_used_connections 7Threads_connected 7Threads_connected 7 is five app connections, one root connection, and the current admin-port session, which sits outside the limit.
[4] Group connections by account and client address. Many Sleep rows with a large max_idle_s mean the app is not closing connections it no longer uses.
$ mysql $A -e "SELECT user, SUBSTRING_INDEX(host,':',1) AS client, command, COUNT(*) AS conns, MAX(time) AS max_idle_s FROM information_schema.processlist GROUP BY user, client, command ORDER BY conns DESC;"mysql: [Warning] Using a password on the command line interface can be insecure.user client command conns max_idle_sapp 127.0.0.1 Sleep 5 6root localhost Sleep 1 2event_scheduler localhost Daemon 1 14root 127.0.0.1 Query 1 0On a real server the client column splits by app server address, which tells you which host and which account fill the slots. If most rows are Query with long time, the problem is slow queries piling up, not leaked connections; look at the queries.
[5] Kill idle connections to free slots. Generate the KILL statements, read them, then run them. Killing a connection with an open transaction rolls that transaction back, so pick only Sleep rows of that account.
$ mysql $A -N -e "SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE user='app' AND command='Sleep' AND time >= 3;" 2>/dev/null > kill.sql$ cat kill.sqlKILL 11;KILL 12;KILL 13;KILL 14;KILL 15;$ mysql $A < kill.sqlmysql: [Warning] Using a password on the command line interface can be insecure.$ mysql -h 127.0.0.1 -uapp -p'<password>' appdb -e "SELECT 1 AS ok;"mysql: [Warning] Using a password on the command line interface can be insecure.ok1On a real server, replace time >= 3 with something clearly longer than a normal request (for example 300). If the app refills the slots right away, the cause is the connection pool. Check that maximum pool size × number of app servers stays below max_connections.
[6] Check wait_timeout so abandoned connections do not linger. The default is 28800 seconds (8 hours), so an unclosed connection holds a slot for 8 hours.
$ mysql $A -e "SHOW VARIABLES WHERE Variable_name IN ('wait_timeout','interactive_timeout');"mysql: [Warning] Using a password on the command line interface can be insecure.Variable_name Valueinteractive_timeout 28800wait_timeout 28800Lowering it also closes, from the server side, idle connections the pool keeps on purpose once they pass that time. Set the pool's idle timeout below wait_timeout before you lower it.
[7] If the numbers show the limit really is too small, raise it. SET PERSIST applies immediately and survives restarts. Raising it without fixing a leak only buys time until it fills again, so size it at the step [5] figure plus a small margin.
$ mysql $A -e "SET PERSIST max_connections = 300; SHOW VARIABLES LIKE 'max_connections';"mysql: [Warning] Using a password on the command line interface can be insecure.Variable_name Valuemax_connections 300$ mysql $A -e "RESET PERSIST max_connections;"mysql: [Warning] Using a password on the command line interface can be insecure.$ mysql $A -e "SELECT COUNT(*) FROM performance_schema.persisted_variables WHERE variable_name='max_connections';"mysql: [Warning] Using a password on the command line interface can be insecure.COUNT(*)0If the problem is that root cannot log in at all, see MySQL ERROR 1698 Access denied for root. If connections pile up waiting on locks and ERROR 1205 Lock wait timeout exceeded shows up too, find the transaction holding the lock with Fix MySQL ERROR 1205 Lock wait timeout exceeded. On PostgreSQL the same situation reads sorry, too many clients already — see Fix PostgreSQL FATAL: sorry, too many clients already.
Before raising max_connections, check how much memory the buffer pool takes in MySQL 8.4 innodb_buffer_pool_size and innodb_redo_log_capacity.
Analyze your MySQL slow log log in the browser ↗ — nothing is uploaded.