Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find root cause for "too many connections" error in MySQL/PHP

I'm running a web service which runs algorithms that serve millions of calls daily and run some background processing as well. Every now and than I see "Too many connections" error in attempts to connect to the MySQL box" for a few seconds. However this is not necessarily attributed to high traffic times or anything I can put my finger on.

I want to find the bottleneck causing it. Other than in the specific times this happens the server isn't too loaded in terms of CPU and Memory, and has 2-3 connections (threads) open and everything works smoothly. (I use Zabbix for monitoring)

Any creative ideas on how to trace it?

like image 927
Nir Avatar asked Apr 15 '10 09:04

Nir


People also ask

How do I fix too many connections errors in MySQL?

If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other clients. The permitted number of connections is controlled by the max_connections system variable. To support more connections, set max_connections to a larger value.

What causes MySQL too many connections?

The MySQL “Too many connections” error occurs when more queries are sent to a MySQL database than can be processed. The error can be fixed by setting a new number of maximum connections in the configuration file or globally.

How do I find the maximum DB connections in MySQL?

To see the current value of max_connections , run this command: SHOW VARIABLES LIKE "max_connections"; By default, it's set to 151. But MySQL actually allows up to max_connections + 1 , which is 151 + 1 for the default setting.

How many max connections can MySQL handle?

By default 151 is the maximum permitted number of simultaneous client connections in MySQL 5.5. If you reach the limit of max_connections you will get the “Too many connections” error when you to try to connect to your MySQL server. This means all available connections are in use by other clients.


4 Answers

try to have an open mysql console when this happens and issue a

SHOW PROCESSLIST;
to see what queries are being executed. Alternatively you could enable logging slow queries (in my.cnf insert this line:
log-slow-queries=/var/log/mysql-log-slow-queries.log

in the [mysqld] section and use

set-variable=long_query_time=1
to define what's the minimum time a query should take in order to be considered slow. (remember to restart mysql in order for changes to take effect)
like image 96
matei Avatar answered Oct 29 '22 14:10

matei


What MySQL table type are you using? MyISAM or InnoDB (or another one)? MyISAM will use table level locking, so you could run into a scenario where you have a heavy select running, followed by an update on the same table and numerous select queries. The last select queries will then have to wait until the update is finished (which in turn has to wait until the first - heavy - select is finished).

For InnoDB a tool like innotop could be useful to find the cause of the deadlock (see http://www.xaprb.com/blog/2006/07/31/how-to-analyze-innodb-mysql-locks/).

BTW The query that is causing the lock to occur should be one of those not in locked state.

like image 44
wimvds Avatar answered Oct 29 '22 13:10

wimvds


The SHOW OPEN TABLES command will display the lock status of all the tables in MySQL. If one or more of your queries is causing the connection backlock, combining SHOW PROCESSLIST and the open tables should narrow it down as to exactly which query is holding up the works.

like image 33
Marc B Avatar answered Oct 29 '22 14:10

Marc B


Old topic. However, I just had this issue and it was because I had a mysqldump script scheduled for 3 times per day. At these times, if my web application was also getting a fair amount of usage, all of the web application queries just queued themselves up on top of each other while the mysqldump was locking all of the tables in the database. The best option is to setup a replication slave on a separate machine, and take your backups from the slave rather than from the production server.

like image 34
Paul Avatar answered Oct 29 '22 14:10

Paul