Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4
ERROR 1040 happens at connect time, before any SQL runs. The server counts open connections and refuses a new one once the count reaches max_connections.
We set max_connections to 2 on MySQL 8.4, opened two sessions, and tried a third login.
01Open connections reached max_connections
With SET GLOBAL max_connections = 2, sessions A (lab_a) and B (lab_b) connected and ran a query normally. Session C, logging in as app_user with a correct password, was refused with ERROR 1040 (08004): Too many connections. The account and password were fine; there was simply no free slot.
SHOW VARIABLES LIKE 'max_connections' showed 2, and Max_used_connections showed 3, so the limit had been reached. After SET GLOBAL max_connections = 200, all three sessions connected.
Setup
CREATE USER app_user IDENTIFIED BY 'App#2026';
SET GLOBAL max_connections = 2;Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)SELECT CURRENT_USER() AS who;+---------+
| who |
+---------+
| lab_a@% |
+---------+
1 row in set (0.00 sec)SELECT CURRENT_USER() AS who;+---------+
| who |
+---------+
| lab_b@% |
+---------+
1 row in set (0.00 sec)$ mysql -h 127.0.0.1 -u app_user -p'App#2026'SELECT CURRENT_USER() AS who;ERROR 1040 (08004): Too many connectionsSHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Max_used_connections';+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2 |
+-----------------+-------+
1 row in set (0.01 sec)
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 3 |
+----------------------+-------+
1 row in set (0.00 sec)SET GLOBAL max_connections = 200;Query OK, 0 rows affected (0.00 sec)Before raising the limit, confirm with Max_used_connections that it was actually reached.
SELECT CURRENT_USER() AS who;+---------+
| who |
+---------+
| lab_a@% |
+---------+
1 row in set (0.00 sec)SELECT CURRENT_USER() AS who;+---------+
| who |
+---------+
| lab_b@% |
+---------+
1 row in set (0.00 sec)$ mysql -h 127.0.0.1 -u app_user -p'App#2026'SELECT CURRENT_USER() AS who;+------------+
| who |
+------------+
| app_user@% |
+------------+
1 row in set (0.00 sec)FAQ
How do I check the current max_connections in MySQL?
Run SHOW VARIABLES LIKE 'max_connections';. To see how close you have come to it, check SHOW STATUS LIKE 'Max_used_connections'; — if it is at or above the limit, ERROR 1040 was caused by the limit.
Is ERROR 1040 a wrong password problem?
No. In our test the same app_user and password were refused with 1040 and then connected fine once the limit was raised. A wrong password gives ERROR 1045 instead.
What is the SQLSTATE of ERROR 1040?
08004 (server rejected the connection), as shown in ERROR 1040 (08004): Too many connections.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13