Andrew Gatenby

Increase max_connections in MySQL

This is a really quick “howto” guide based on experience in the office, where we had pages and queries that were unable to run properly, purely because of the number of queries that we were trying to pass to MySQL at once.

The MySQLD process was refusing any more than the default number of connections, and returning the error message:

Warning: mysql_connect(): Too many connections in /home/sites/domain.com/inc/classes/db.php on line 19
Unable to connect to database server!

To increase the number of simultaneous connections that MySQL can handle, login via SSH and edit the /etc/my.cnf file. Under the [mysqld] section of this configuration file, find the line (in among a few others) that says “max_connections”:

[mysqld]
....
max_connections=100
....

The MySQL default for max_connections is 100, but we opted to up it to 200 and monitor any impact this change had on the server processing as a whole. To do this, simply change the value in this file, save the file and exit. You’ll then need to restart the MySQLD process, by a command that would look something like this:

/etc/init.d/mysqld restart

So far, so good and there’s been no knock-on effect of MySQL not being overloaded with requests by increasing the possible number it could simultaenously process. But depending on your server and website, it might be worthwhile to increase it slightly and monitor the impact. You might find that a 15-20% increase in max_connections should solve your problem, and not run the risk of having MySQL overloaded with connections.

Bookmark or share this article:

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • description
  • Reddit
  • StumbleUpon

Category: MySQL

Tagged:

2 Responses

  1. You can set that variable without a restart:

    http://www.mysqldba.co.uk/articles/2008/09/12/diagnosing-too-many-connections-errors/

    In addition:

    http://www.mysqldba.co.uk/articles/2008/06/19/let-sleeping-dogs-die-automatically-close-idle-connections/

    Most interfaces will automatically reconnect, so keeping the number of idle connections down won’t hurt, and if you’re using a connection pooling system you could get away with a drastically reduced number of open connections.

  2. Jonty says:

    Great article

    I could not find max_connections in my my.cnf file, but I added it in and it worked fine

    It may be worth adding in how to change “max connect errors” which is currently set to 10.

Leave a Reply