MySQL connection errors are among the most common application issues. This guide covers the most frequent errors and their resolutions.
Error: Too Many Connections
Symptoms: ERROR 1040 (HY000): Too many connections
Cause: The number of simultaneous connections has reached max_connections.
Check current connections:
$mysql -u root -p -e "SHOW STATUS LIKE 'Threads_connected';"+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 151 |
+-------------------+-------+$mysql -u root -p -e "SHOW VARIABLES LIKE 'max_connections';"+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+Resolution: Increase max_connections:
$mysql -u root -p -e "SET GLOBAL max_connections = 300;"To make it permanent, edit /etc/mysql/mysql.conf.d/mysqld.cnf:
[mysqld]
max_connections = 300[!NOTE] Increasing
max_connectionsrequires more RAM. Each connection uses approximately 1 MB. Consider using a connection pooler (ProxySQL, PgBouncer) for high-traffic applications.
Error: Access Denied
Symptoms: ERROR 1045 (28000): Access denied for user 'user'@'host'
Cause: Wrong password, user does not exist, or the user is not allowed to connect from that host.
Check user grants:
$mysql -u root -p -e "SELECT user, host FROM mysql.user WHERE user='myapp_user';"+------------+-----------+
| user | host |
+------------+-----------+
| myapp_user | localhost |
+------------+-----------+If the application connects from 10.0.1.10 but the user is only allowed from localhost, add the correct host:
CREATE USER 'myapp_user'@'10.0.1.10' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'10.0.1.10';
FLUSH PRIVILEGES;Error: Lost Connection
Symptoms: ERROR 2013 (HY000): Lost connection to MySQL server during query
Cause: Query timeout or packet size exceeded.
Check and increase timeouts:
$mysql -u root -p -e "SHOW VARIABLES LIKE 'wait_timeout';"[mysqld]
wait_timeout = 600
interactive_timeout = 600
max_allowed_packet = 64MCheck MySQL Error Log
$tail -50 /var/log/mysql/error.logMonitor Slow Queries
Enable the slow query log to find performance bottlenecks:
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2$tail -f /var/log/mysql/slow.log