Home » Resolving: 'Too many connections' Error in MySQL / MariaDB Databases
Web & Code

Resolving: 'Too many connections' Error in MySQL / MariaDB Databases

✨ Quick Answer

The Error 1040: Too many connections error immediately stops queries to your database and occurs when the number of open connection threads from your application exceeds the maximum directive defined in the internal settings of the MySQL or MariaDB server.

Quick Diagnostics

Cause
Reached max_connections limit in MySQL/MariaDB
Solution
Temporarily increase limit: SET GLOBAL max_connections = 500;
Cause
Hanging persistent connections not closed by the application
Solution
Set max_connections = 500 in /etc/mysql/my.cnf and restart service

The Error 1040: Too many connections error immediately stops queries to your database and occurs when the number of open connection threads from your application exceeds the maximum directive defined in the internal settings of the MySQL or MariaDB server.

Step-by-Step Solution

  1. 1

    Step 1: Access the engine and diagnose inactive threads

    If the web server still allows you to interact via console using the admin account, inspect which query processes are holding up the data traffic:

    BASH
    # Log in to the database engine
    mysql -u root -p
    
    # Execute the command to list processes
    SHOW PROCESSLIST;
    

    (Look for queries with a prolonged "Sleep" state. If there are hundreds of them, the application is not closing connection channels after responding).

  2. 2

    Step 2: Adjust global limits in the configuration file

    Open the database server configuration file (/etc/mysql/my.cnf or /etc/my.cnf.d/server.cnf in MariaDB) and increase the maximum limit of concurrent connections allowed by your hardware:

    PLAINTEXT
    [mysqld]
    max_connections = 250
    interactive_timeout = 180
    wait_timeout = 60
    

    (Note: Reducing wait_timeout forces the engine to kill inactive, unused connections after 60 seconds, automatically freeing up slots).

  3. 3

    Step 3: Restart the service to free memory

    BASH
    sudo systemctl restart mysql   # For MySQL
    sudo systemctl restart mariadb # For MariaDB
    

Prevention Advice

Recommended security practices:

  • Do not arbitrarily increase the max_connections directive to astronomical values (such as 1000 or 2000) if your hardware has limited physical RAM. Each connection channel consumes processor resources and cache memory; exceeding the server's capacities will cause the system to freeze due to a lack of pagefile space or trigger critical crashes. Implement connection pooling in your web application to reuse channels instead of opening a new one for every query.
Author • Web Designer & App Creator

Rodolfo Castro

Web designer, app developer, and founder of SoporteCero. Specializing in UI/UX architecture, digital products, and modern web environments. Every tutorial and guide on SoporteCero is thoroughly tested and verified in our technical lab to ensure reliable, up-to-date solutions.