"Too Many Connections": How to Increase the MySQL Connection Count To Avoid This Problem
Join the DZone community and get the full member experience.
Join For Freeif you don't have enough connections open to your mysql server, your users will begin to receive a "too many connections" error while trying to use your service. to fix this, you can increase the maximum number of connections to the database that are allowed, but there are some things to take into consideration before simply ramping up this number.
items to consider
before you increase the connections limit, you will want to ensure that the machine on which the database is housed can handle the additional workload. the maximum number of connections that can be supported depends on the following variables:
- the available ram – the system will need to have enough ram to handle the additional workload.
- the thread library quality of the platform - this will vary based on the platform. for example, windows can be limited by the posix compatibility layer it uses (though the limit no longer applies to mysql v5.5 and up). however, there remains memoray usage concerns depending on the architecture (x86 vs. x64) and how much memory can be consumed per application process.
- the required response time - increasing the number could increase the amount of time to respond to request. this should be tested to ensure it meets your needs before going into production.
- the amount of ram used per connection - again, ram is important, so you will need to know if the ram used per connection will overload the system or not.
- the workload required for each connection - the workload will also factor in to what system resources are needed to handle the additional connections.
another issue to consider is that you may also need to increase the open files limit–this may be necessary so that enough handles are available.
checking the connection limit
to see what the current connection limit is, you can run the following from the mysql command line or from many of the available mysql tools such as phpmyadmin :
the show variables command.
this will display a nicely formatted result for you:
example result of the show variables command.
increasing the connection limit
to increase the global number of connections temporarily, you can run the following from the command line:
an example of setting the max_connections global.
if you want to make the increase permanent, you will need to edit the my.cnf configuration file. you will need to determine the location of this file for your operating system (linux systems often store the file in the /etc folder, for example). open this file add a line that includes max_connections, followed by an equal sign, followed by the number you want to use, as in the following example:
example of setting the max_connections
the next time you restart mysql, the new setting will take effect and will remain in place unless or until this is changed again.
easily scale a mysql database
instead of worrying about these settings on your own system, you could opt to use a service like morpheus , which offers databases as a service on the cloud. with morpheus, you can easily and quickly set up your choice of several databases (including mysql, mongodb, redis, and elasticsearch).
in addition, mysql and redis have automatic back ups, and each database instance is replicated, archived, and deployed on a high performance infrastructure with solid state drives.
Published at DZone with permission of Gen Furukawa, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments