MySQL reaches its maximum allowed concurrent connections and rejects new connection attempts. This error commonly occurs when max_connections is too low, connections aren't properly closed, or traffic spikes exceed current capacity.
ERROR 1040 occurs when a MySQL server has reached its maximum allowed number of client connections. By default, MySQL allows 151 concurrent connections (150 normal + 1 reserved for SUPER users). When all available connection slots are in use, the server denies new connection requests with this error. The actual limit is controlled by the max_connections system variable. Each active connection consumes server memory, so this error often indicates either a misconfigured limit for your application's needs, connection pooling issues, or abnormally high traffic.
Connect to MySQL as an admin user and check how many connections are active:
SHOW PROCESSLIST;
SHOW STATUS LIKE "Threads_connected";
SHOW STATUS LIKE "max_connections";If Threads_connected equals max_connections, your server is at capacity. Review the PROCESSLIST output to identify sleeping or long-running queries.
If safe to do so, terminate idle or sleeping connections to free up slots:
-- View idle connections
SHOW PROCESSLIST WHERE Command = "Sleep" AND Time > 300;
-- Kill a specific connection (replace CONN_ID with actual ID)
KILL CONNECTION_ID;
-- Or use pt-kill from Percona Toolkit for bulk operations
pt-kill --victims=oldest --busy-time=300 --sleep-time=300This immediately frees connection slots for new clients.
If you need an immediate fix and have admin access:
SET GLOBAL max_connections = 500;This takes effect immediately without restarting MySQL, but resets to the configured value on next server restart. Use this to buy time while implementing permanent fixes.
Edit your MySQL configuration file (usually /etc/my.cnf or /etc/mysql/my.cnf) and locate the [mysqld] section:
[mysqld]
max_connections = 500Use a value appropriate for your server's memory: max_connections = (Available_RAM_MB - Global_Buffers_MB) / Per_Connection_Memory_MB. Most Linux systems handle 500-1000 connections without issues. Then restart MySQL:
sudo systemctl restart mysql
# or
sudo service mysql restartThe wait_timeout setting (default 28800 seconds / 8 hours) determines how long MySQL holds idle connections. Reduce this to automatically free connections:
[mysqld]
wait_timeout = 300
interactive_timeout = 300This closes idle connections after 5 minutes. Adjust based on your application's needs. Restart MySQL for the changes to take effect.
Use a connection pool to limit application connections to MySQL. This prevents applications from opening excessive connections:
In Node.js (mysql2/promise):
const mysql = require("mysql2/promise");
const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "password",
database: "mydb",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});In Python (SQLAlchemy):
from sqlalchemy import create_engine
engine = create_engine(
"mysql+pymysql://user:password@localhost/db",
pool_size=10,
max_overflow=20
)Connection pooling acts as a intermediary between applications and MySQL, reusing connections efficiently.
For high-traffic applications, use a proxy like ProxySQL to handle connection multiplexing:
# Install ProxySQL
sudo apt install proxysql
# Configure to limit backend connections
# Applications connect to ProxySQL (port 6033) instead of MySQL (port 3306)
# ProxySQL multiplexes many client connections into fewer MySQL connectionsProxySQL can buffer requests, reuse connections, and distribute load across multiple MySQL servers.
MySQL pre-allocates memory for each connection based on thread_stack, sort_buffer_size, and other per-connection variables. Therefore, simply increasing max_connections without monitoring memory can exhaust RAM and crash the server. The maximum theoretical limit for max_connections is 100,000, but practical limits depend on available memory. One connection is always reserved for users with CONNECTION_ADMIN or SUPER privilege—this allows administrators to connect even when the connection limit is reached. On shared hosting, the open files limit (ulimit -n) may also cap connections before max_connections is reached. Review your system's limits with ulimit -a. For diagnosis, use SHOW ENGINE INNODB STATUS to identify long-running transactions and SHOW PROCESSLIST to find sleeping connections consuming slots.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL