My guess is that you have some long running queries in your application. When they are executed they cause the connection to stay checked out of the pool for a long time (relative to the usual usage pattern), this causes your pool to become exhausted, grow, and continue to grow up to its maximum, at which point any remaining workers block waiting on connections to be released.
The first thing will be to track down when this happens, that is, is it a cyclical event, or random. If its the former you're in luck, as you can be ready time it happens. If you can't determine a pattern then you'll have to be vigilant.
You may be able to figure this from looking at your website monitoring logs, or sar
from your database to see if there are any correlating spikes.
If you can catch your database when its under load, you should execute the following commands on the mysql server
show innodb status;
show processlist;
The former will print out diagnostic information about the innodb engine (you are using innodb right?), the latter will print out the first few hundred chars of the query that was executing. Look for queries that have been running for a long time, queries generating temporary tables on disk, and queries that are blocked on a resource.
After that, the hard work begins. Use EXPLAIN
to estimate the cost of the query, and the resources it uses. Avoid queries that require sorting on disk via a tmp table. Look for long running reporting jobs, or other scheduled maintenance tasks that periodically lock or saturate your database. It could be something as simple as the backup task, or a job that rolls up old purchase order data.
I recommend having these three settings in your /etc/my.cnf
log_slow_queries
log-queries-not-using-indexes
set-variable = long_query_time=1
For a web application doing 20-30 requests per second, you can't afford to have anything show up in these logs.
btw, IMHO its pointless to increase your connection pool's size beyond your original size as this will only delay the onset of pool exhaustion by at best, a few seconds, and only put more pressure on your db right when it doesn't need it.
This sounds like maybe connections aren't being cleaned up in a timely manner. "show processlist" should show the connections if that's the case. Looking at the MySQL documentation, it appears this may be common with PHP unless certain PHP parameters are tweaked. You may want to look through the article and comments at http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html
Another possibility brought up by the very slow TCP connections is perhaps MySQL (or tcpwrappers) is trying to do hostname lookup for access and the lookup is slow. This seems unlikely when using unix sockets, but in case it's trying to match localhost or system hostnames it may be worth looking into whether any access rules could be moved to IP or removed.
Best Answer
Though this is getting a bit stackoverflow'y, here goes:
Probably because you don't close your connections in the code. If so, I would recommend you switch to
mysql_pconnect()
, or just addmysql_close()
to the end of all requested php-pagesIf all the connections to the mysql server is in state:
TIME_WAIT
, try lowering thewait_timeout
variable in your mysqld configuration. Check out the MySQL documentation for more infoUPDATE: As ChristopherEvans pointed out, you can connect directly to the mysql socket instead of using IP endpoints, to avoid running out of unused ports on the local interface