MySQL Too many connections

MySQL too many connections.

Of course this simply means there are too many connections to the MySQL database. This can happen on a very busy site or even on a site with few visitors that contains poor coding that doesn't drop connections.

To fix it, you can go one of two ways. Through SSH using MySQL commands or by adding a directive to the global configuration file (/etc/my.cnf).


For editing the configuration file, simply add the below directive to the configuration file.


Then restart MySQL services:

[01:50:26] [root@localhost ~]# service mysql restart
Shutting down MySQL.......                                 [  OK  ]
Starting MySQL.                                            [  OK  ]


To change it via command line, you will need to be root and log into MySQL and use the command "set global max_connections=XXX":

[01:50:37] [root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 62
Server version: 5.1.65-cll MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set global max_connections=5000;
Query OK, 0 rows affected (0.00 sec)

You may also like...

2 Responses

  1. Sean Dempsey says:

    I found the default value set here (124) to be surprisingly low. However, is there any danger is changing the value to a number as high as 5000 (per your example above)? What are the pros/cons involved?

    • David says:

      It depends on the traffic you're receiving. Around 1000 connections is the maximum before you want to start load balancing and clustering. Setting it too high will result in the server crashing after it runs out of memory.

      If you have some pages or parts of your website or application that are not closing connections, you should review the programming and reasoning behind why those connections are staying open.

      A site like WordPress, for example, will:

      - Open the page.
      - Open connection to the database.
      - Query the database for info and receive said info.
      - Close connection to the database.
      - Display information / formatted, etc.

      If your programming is not pretty, you'll have an application or website that opens a connection and never closes it. Increasing the size of the max connections is a temporary fix to a much bigger problem in this scenario. If anything, you will want your database queries to be as quick as possible, utilize caching, and not let your server die due to running out of memory.

Leave a Reply

Your email address will not be published. Required fields are marked *