Skip to main content

How to Troubleshoot a MySQL “Too Many Connections” Error

The "Too Many Connections" error occurs when the number of active connections to your MySQL or MariaDB database exceeds the maximum limit allowed by the server configuration. This prevents new users or applications from establishing a connection to...

The "Too Many Connections" error occurs when the number of active connections to your MySQL or MariaDB database exceeds the maximum limit allowed by the server configuration. This prevents new users or applications from establishing a connection to the database.

Before you begin

You will need:

  • Access to your hosting control panel (such as cPanel or Windows Control Panel)

  • Access to a database management tool (such as phpMyAdmin or MySQL Workbench)

  • The ability to identify which application or script is generating the excessive connections

Check the current connection limit

To determine if your database is hitting its limit, you can check the current maximum allowed connections.

  1. Log in to your database management tool (e.g., phpMyAdmin).

  2. Run the following SQL command:

    SHOW VARIABLES LIKE 'max_connections';

  3. Note the value returned. If the number of active connections is consistently at or near this number, the error will occur.

Identify the source of the connections

If your database is frequently hitting the limit, it is often caused by "hanging" connections or a script that is not closing its connection after execution.

  1. Run the following SQL command to see current active connections:

    SHOW PROCESSLIST;

  2. Review the list for:

    • Sleep status: Connections that remain open but are not doing anything.

    • Long-running queries: Queries that take a long time to execute, occupying a connection slot.

    • Repeated entries: Multiple connections from the same IP address or for the same database.

Resolve the issue

Depending on what you find in the process list, take the following actions:

If you see many "Sleep" connections

This usually indicates that your application (e.g., WordPress or a custom script) is not closing connections properly.

  • Action: Check your application's configuration. For example, in WordPress, ensure you are using a standard, up-to-date version of the software.

  • Action: If you have a custom script, ensure that the database connection is closed at the end of the script execution.

If you see a single query taking a long time

A slow query can "clog" a connection, preventing others from using it.

  • Action: Identify the query and optimize it. You can use the EXPLAIN command to see how MySQL handles the query.

  • Action: Add indexes to the tables involved in the slow query to improve performance.

If you have a high-traffic site

If your site is legitimately receiving more traffic than the current max_connections limit allows, you may need to increase the limit.

  • Action: Contact Hostek support to discuss your specific plan limits and whether your current account can support a higher connection threshold.

Confirm the result

  • The "Too Many Connections" error no longer appears when you refresh your website or application.

  • The SHOW PROCESSLIST; command shows a manageable number of active connections during peak traffic.

Limitations

Increasing the max_connections limit is not a substitute for optimizing poorly written code or slow queries. If a script is poorly coded, it will eventually consume all available connections regardless of how high the limit is set.

Did this answer your question?