<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Debugging sleeping connections with MySQL</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tom Pittlik</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-564403</link>
		<dc:creator>Tom Pittlik</dc:creator>
		<pubDate>Thu, 21 May 2009 00:12:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-564403</guid>
		<description>I should also add - increasing the max_connections (mysql) and MaxClients (apache) variables to high values is not a solution! In fact it can just prolong the bottleneck as the server takes longer to crash.</description>
		<content:encoded><![CDATA[<p>I should also add &#8211; increasing the max_connections (mysql) and MaxClients (apache) variables to high values is not a solution! In fact it can just prolong the bottleneck as the server takes longer to crash.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Pittlik</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-564400</link>
		<dc:creator>Tom Pittlik</dc:creator>
		<pubDate>Thu, 21 May 2009 00:09:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-564400</guid>
		<description>I was seeing this occurring due to every apache connection opening a mysql connection (as is common in a PHP driven website). If there is a bottleneck, a vicious cycle occurs:

1. apache requests increase
2. mysql queries increase
3. mysql slow queries increase
4. apache connections start to wait on slow mysql queries
5. apache connections take longer to close
6. more apache connections are opened
7. with each apache request a new sleeping mysql connection is made
8. mysql &amp; apache reach max_connections
9. server slows to a crawl

Solution:
- Reduce the time a mysql connection is open during script execution - don&#039;t open a mysql connection in the header and close it in the footer. One approach is to create a query wrapper function and open a connection to the database only when the first query occurs - e.g:
function query ($query) {
if (!mysql_connection) mysql_connect();
return mysql_query();
}
- Reduce slow queries (always a good strategy!)</description>
		<content:encoded><![CDATA[<p>I was seeing this occurring due to every apache connection opening a mysql connection (as is common in a PHP driven website). If there is a bottleneck, a vicious cycle occurs:</p>
<p>1. apache requests increase<br />
2. mysql queries increase<br />
3. mysql slow queries increase<br />
4. apache connections start to wait on slow mysql queries<br />
5. apache connections take longer to close<br />
6. more apache connections are opened<br />
7. with each apache request a new sleeping mysql connection is made<br />
8. mysql &amp; apache reach max_connections<br />
9. server slows to a crawl</p>
<p>Solution:<br />
- Reduce the time a mysql connection is open during script execution &#8211; don&#8217;t open a mysql connection in the header and close it in the footer. One approach is to create a query wrapper function and open a connection to the database only when the first query occurs &#8211; e.g:<br />
function query ($query) {<br />
if (!mysql_connection) mysql_connect();<br />
return mysql_query();<br />
}<br />
- Reduce slow queries (always a good strategy!)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-564338</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 20 May 2009 23:04:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-564338</guid>
		<description>what the hell, here&#039;s the first part of the php script that this thing cut off due to bad html stripping it looks like:


$link = mysql_connect (&quot;localhost&quot;, &quot;root&quot;, &quot;password&quot;);

$query = &quot;show processlist;&quot;;

$result = mysql_query($query) or die (&quot;failed $query&quot;);

while ($row = mysql_fetch_array($result))
{
        if (($row[&#039;Time&#039;] &gt;</description>
		<content:encoded><![CDATA[<p>what the hell, here&#8217;s the first part of the php script that this thing cut off due to bad html stripping it looks like:</p>
<p>$link = mysql_connect (&#8221;localhost&#8221;, &#8220;root&#8221;, &#8220;password&#8221;);</p>
<p>$query = &#8220;show processlist;&#8221;;</p>
<p>$result = mysql_query($query) or die (&#8221;failed $query&#8221;);</p>
<p>while ($row = mysql_fetch_array($result))<br />
{<br />
        if (($row['Time'] &gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-564335</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 20 May 2009 23:02:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-564335</guid>
		<description>A better way than restarting your server or whatever is to set a MySQL value that does not allow connections to last over 30 min, or you can have a cron script that checks the db and kills really old sleeping connections.

my.cnf:
wait_timeout=180 # = 30min

php cron script:

#!/usr/bin/php -q
 1000 &amp;&amp; $row[&#039;Command&#039;] == &quot;Sleep&quot;) &amp;&amp; ($row[&#039;User&#039;] != &quot;Root&quot;))
        {
                $killed = 0;
                mysql_query(&quot;kill &quot;.$row[&#039;Id&#039;]) or ($killed = 1);
                if ($killed = 1)
                {
                        echo
                                &quot;Killed process number &quot;.$row[&#039;Id&#039;].&quot;, with a User of &quot;.$row[&#039;User&#039;]
                                .&quot; in database &quot;.$row[&#039;db&#039;].&quot;, which had a time of &quot;.$row[&#039;Time&#039;]
                                .&quot;, and a command of &quot;.$row[&#039;Command&#039;].&quot;\r\n\r\n&quot;;
                }
        }

}

@mysql_close($link);

?&gt;</description>
		<content:encoded><![CDATA[<p>A better way than restarting your server or whatever is to set a MySQL value that does not allow connections to last over 30 min, or you can have a cron script that checks the db and kills really old sleeping connections.</p>
<p>my.cnf:<br />
wait_timeout=180 # = 30min</p>
<p>php cron script:</p>
<p>#!/usr/bin/php -q<br />
 1000 &amp;&amp; $row['Command'] == &#8220;Sleep&#8221;) &amp;&amp; ($row['User'] != &#8220;Root&#8221;))<br />
        {<br />
                $killed = 0;<br />
                mysql_query(&#8221;kill &#8220;.$row['Id']) or ($killed = 1);<br />
                if ($killed = 1)<br />
                {<br />
                        echo<br />
                                &#8220;Killed process number &#8220;.$row['Id'].&#8221;, with a User of &#8220;.$row['User']<br />
                                .&#8221; in database &#8220;.$row['db'].&#8221;, which had a time of &#8220;.$row['Time']<br />
                                .&#8221;, and a command of &#8220;.$row['Command'].&#8221;\r\n\r\n&#8221;;<br />
                }<br />
        }</p>
<p>}</p>
<p>@mysql_close($link);</p>
<p>?&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-538914</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Tue, 14 Apr 2009 17:48:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-538914</guid>
		<description>Russell, sleeping threads are a problem. They occur when a connection is requested, but is not closed- the connection is still reserved by that thread and &quot;waiting for the client to send a new statement to it&quot;. If the sleep time of a thread exceeds the `wait_timeout` variable for mysql, the connection gets closed and recycled. Until then, it is locked.</description>
		<content:encoded><![CDATA[<p>Russell, sleeping threads are a problem. They occur when a connection is requested, but is not closed- the connection is still reserved by that thread and &#8220;waiting for the client to send a new statement to it&#8221;. If the sleep time of a thread exceeds the `wait_timeout` variable for mysql, the connection gets closed and recycled. Until then, it is locked.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Russell G.</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-500621</link>
		<dc:creator>Russell G.</dc:creator>
		<pubDate>Mon, 09 Mar 2009 20:58:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-500621</guid>
		<description>Are you sure that sleeping threads are a problem? I always thought that a thread showing a Command value of &quot;Sleep&quot; just meant that it had finished whatever request it was working on and was sitting idle, waiting for another request to be assigned to it.  I believe the Time value for those threads is how long it&#039;s been sitting idle.  The MySQL docs seem to say the same thing:

&quot;Sleep - The thread is waiting for the client to send a new statement to it.&quot;

From:
http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html

Even the program &quot;mytop&quot;, which displays the current commands being processed by the server, ignores threads with the Sleep command, I believe for the same reason.

I can see how a thread with a Command of &quot;Query&quot; could be a problem if it has a large Time value, but I don&#039;t think Sleep commands are a problem.  Or have I just completely misunderstood things?

Russell G.</description>
		<content:encoded><![CDATA[<p>Are you sure that sleeping threads are a problem? I always thought that a thread showing a Command value of &#8220;Sleep&#8221; just meant that it had finished whatever request it was working on and was sitting idle, waiting for another request to be assigned to it.  I believe the Time value for those threads is how long it&#8217;s been sitting idle.  The MySQL docs seem to say the same thing:</p>
<p>&#8220;Sleep &#8211; The thread is waiting for the client to send a new statement to it.&#8221;</p>
<p>From:<br />
<a href="http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/thread-commands.html</a></p>
<p>Even the program &#8220;mytop&#8221;, which displays the current commands being processed by the server, ignores threads with the Sleep command, I believe for the same reason.</p>
<p>I can see how a thread with a Command of &#8220;Query&#8221; could be a problem if it has a large Time value, but I don&#8217;t think Sleep commands are a problem.  Or have I just completely misunderstood things?</p>
<p>Russell G.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Web Services &#8212; Troubleshooting Sleeping Processes in MySQL</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-354409</link>
		<dc:creator>Web Services &#8212; Troubleshooting Sleeping Processes in MySQL</dc:creator>
		<pubDate>Thu, 11 Sep 2008 14:18:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-354409</guid>
		<description>[...] Debugging sleeping connections with MySQL [...]</description>
		<content:encoded><![CDATA[<p>[...] Debugging sleeping connections with MySQL [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Pittlik</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-184670</link>
		<dc:creator>Tom Pittlik</dc:creator>
		<pubDate>Mon, 05 Nov 2007 00:58:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-184670</guid>
		<description>I get this once in a blue moon - instead of tracking down what&#039;s causing the problem (since it&#039;s so rare) i made a simple cronjob that checks if dbconnect() fails for mysql_errno() reason 1040 (too many connections). If it does i run shell_exec(&quot;httpd restart&quot;) and the sleeping connections are gone.</description>
		<content:encoded><![CDATA[<p>I get this once in a blue moon &#8211; instead of tracking down what&#8217;s causing the problem (since it&#8217;s so rare) i made a simple cronjob that checks if dbconnect() fails for mysql_errno() reason 1040 (too many connections). If it does i run shell_exec(&#8221;httpd restart&#8221;) and the sleeping connections are gone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-57819</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 22 Feb 2007 09:32:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-57819</guid>
		<description>Guruprasad,

I&#039;d check if these connections are associated with open file descriptors.   If they are it is likely these are persistent connections which may be reused. If not it is likely  these connections are still closing by kernel, sometimes it takes longer time for some reason.</description>
		<content:encoded><![CDATA[<p>Guruprasad,</p>
<p>I&#8217;d check if these connections are associated with open file descriptors.   If they are it is likely these are persistent connections which may be reused. If not it is likely  these connections are still closing by kernel, sometimes it takes longer time for some reason.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guruprasad</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-57523</link>
		<dc:creator>Guruprasad</dc:creator>
		<pubDate>Thu, 22 Feb 2007 01:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-57523</guid>
		<description>Just in case if some one is tryin this on BSD box,
netstat -ntp might not work, u can use lsof instead like,

lsof -i -P &#124; grep  OR </description>
		<content:encoded><![CDATA[<p>Just in case if some one is tryin this on BSD box,<br />
netstat -ntp might not work, u can use lsof instead like,</p>
<p>lsof -i -P | grep  OR</p>
]]></content:encoded>
	</item>
</channel>
</rss>
