Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Emmanuel71's avatar

Too many connections in MySQL

In a Laravel 9 app I regurarly get a 'Too many connections' MySQL error. The app is not in production yet but is already on a live server for final testing purposes.

Sometimes the error stays away for days and then pops back up unexpectedly. I'm under the impression it mostly occurs in a scheduled job that runs every 5 minutes. However, that's about the only thing running on the site at the moment.

Yesterday, the error occured again while I was testing and the site went down for about half an hour and then got responsive again after disabling cron and a call to the isp for a php server reset. The same codebase and db on our dev server with the same isp never gives this problem. The isp suggests to always properly close the db connections in code. However, I think this happens automatically in Laravel/php after script completion. The MySQL version is 5.7.

When the site was down, I was able to connect with my MySQL tool and 'show processlist' only showed 2 lines. However now I see that "SHOW STATUS WHERE variable_name = 'Max_used_connections'" has a value of 151, exceeding the max value of 150.

I'm really puzzled about this one. Any ideas? Thx.

0 likes
2 replies
LaryAI's avatar
Level 58

The "Too many connections" error in MySQL can occur due to a variety of reasons, such as a high traffic website, long-running queries, or a misconfigured MySQL server. Here are a few steps you can take to troubleshoot and resolve the issue:

  1. Check the MySQL server configuration: Ensure that the MySQL server is configured to handle the expected number of connections. You can check the maximum number of connections allowed by running the following command in the MySQL console:
SHOW VARIABLES LIKE 'max_connections';

If the value is less than the number of connections your application requires, you can increase it by modifying the MySQL configuration file (my.cnf or my.ini) and restarting the MySQL server.

  1. Check the application code: Ensure that your application is properly closing database connections after use. In Laravel, database connections are automatically closed at the end of each request, but if you are using long-running scripts or scheduled jobs, you may need to manually close the connections. You can do this by calling the disconnect method on the database connection:
DB::disconnect('connection_name');
  1. Optimize database queries: Long-running queries can tie up database connections and cause the "Too many connections" error. Ensure that your queries are optimized and use indexes where necessary. You can also consider using a database connection pool to limit the number of connections to the database.

  2. Monitor database connections: Use a tool like MySQL Workbench or phpMyAdmin to monitor the number of active connections to the database. If you notice a sudden spike in connections, investigate the cause and take appropriate action.

  3. Consider upgrading your MySQL server: If none of the above steps resolve the issue, consider upgrading your MySQL server to a version that can handle more connections or using a load balancer to distribute the load across multiple MySQL servers.

By following these steps, you should be able to resolve the "Too many connections" error in MySQL.

Emmanuel71's avatar

Any humans have other suggestions? Only thing that's not clear to me is when you should use DB::disconnect. The cron job only executes a quick update query, so I think a disconnect is not neccessary? Also show status where variable_name = 'Threads_connected'; command shows a value between 5 and 80 so far. At the same time show processlist never shows more than 2 lines.

Please or to participate in this conversation.