Sunday, May 26, 2013

How much is bad code contributing to mysql connection exhaustion?

Sleeping connections happen when the client code exits without closing the mysql connection.   

That happens a lot more often than you might think - developers are happy to open database connections, but think they'll get closed "automatically" when the page is done being rendered.

In apache that's not true - the apache process keeps running and handling other requests.  So the connection stays open, wasted, until the server finally closes it after a timeout, or until the apache child process gets recycled (having handled MaxRequestsPerChild requests - often in the thousands)

Here's a quick way to review what the current ratio of sleeping to total connections is for MySQL.

mysqladmin processlist|sed '4,$p'|grep -v ^+|awk -F\| '{c++} index($6,"Sleep") > 0 {s++} END {print s " sleeping connections out of " c}'

No comments:

Post a Comment