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

Leave a Reply

Andrew Gatenby Freelance Web Designer, Developer & Geek