<?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>Percona&#039;s MySQL &#38; InnoDB performance and scalability blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 16:45:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: vishnu rao</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-857697</link>
		<dc:creator>vishnu rao</dc:creator>
		<pubDate>Thu, 15 Dec 2011 17:49:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-857697</guid>
		<description>hello everyone,

in our application server, after we start a transaction starts - we make sure we execute the following query using the connection object obtained from our pool. [i.e. the following query is the first query to be executed]

&quot;select CONNECTION_ID() &quot;
refer to http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_connection-id

This gives us the thread id which mysql would assign to this transaction.

we then log the following details - thread id &amp; name of the application module which wants to start the db transaction.

when we see threads in sleep state(from show processlist) we note down the id &amp;  immediately look at our logs to identify the application module which has the corresponding id &amp; debug the module code to see what it does. in most cases - we found our application thread starts of a database transaction and does a few queries and then makes a call to some other external system like a MQ server and gets stuck, &amp; since the transaction is still active(i.e. not commited/rollbacked) - the thread in mysql goes to sleep state.

hope this helps

-vishnu rao
@flipkart.com</description>
		<content:encoded><![CDATA[<p>hello everyone,</p>
<p>in our application server, after we start a transaction starts &#8211; we make sure we execute the following query using the connection object obtained from our pool. [i.e. the following query is the first query to be executed]</p>
<p>&#8220;select CONNECTION_ID() &#8221;<br />
refer to <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_connection-id" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_connection-id</a></p>
<p>This gives us the thread id which mysql would assign to this transaction.</p>
<p>we then log the following details &#8211; thread id &amp; name of the application module which wants to start the db transaction.</p>
<p>when we see threads in sleep state(from show processlist) we note down the id &amp;  immediately look at our logs to identify the application module which has the corresponding id &amp; debug the module code to see what it does. in most cases &#8211; we found our application thread starts of a database transaction and does a few queries and then makes a call to some other external system like a MQ server and gets stuck, &amp; since the transaction is still active(i.e. not commited/rollbacked) &#8211; the thread in mysql goes to sleep state.</p>
<p>hope this helps</p>
<p>-vishnu rao<br />
@flipkart.com</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naveen</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-849516</link>
		<dc:creator>Naveen</dc:creator>
		<pubDate>Thu, 01 Dec 2011 15:28:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-849516</guid>
		<description>We are having a problem with Sleeping queries slowing down our servers - its become quiet frequent now and we are trying to find out why - the problem in tracking down is that the system is a Social Networking Engine Software (Elgg) - we do not know the very internals of the software. We tried to use the approach above, however our mysql and web servers are on 2 separate servers - this poses additional problems as we cannot exactly locate from which ip the requests are coming to the mysql server - it will always be the web server. 
Also when we try the netstat command on the mysql server to see the process, it just displays mysqld as Established - is there a better way to track where these queries are originating for such a case?
currently we are waiting for the problem to occur again and then dynamically enable Mysql general query log to see what queries are being thrown to the server - we have isolated this application to a single box 
any suggestions on how to tackle this situation ? 

thank you for your valuable posts</description>
		<content:encoded><![CDATA[<p>We are having a problem with Sleeping queries slowing down our servers &#8211; its become quiet frequent now and we are trying to find out why &#8211; the problem in tracking down is that the system is a Social Networking Engine Software (Elgg) &#8211; we do not know the very internals of the software. We tried to use the approach above, however our mysql and web servers are on 2 separate servers &#8211; this poses additional problems as we cannot exactly locate from which ip the requests are coming to the mysql server &#8211; it will always be the web server.<br />
Also when we try the netstat command on the mysql server to see the process, it just displays mysqld as Established &#8211; is there a better way to track where these queries are originating for such a case?<br />
currently we are waiting for the problem to occur again and then dynamically enable Mysql general query log to see what queries are being thrown to the server &#8211; we have isolated this application to a single box<br />
any suggestions on how to tackle this situation ? </p>
<p>thank you for your valuable posts</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Albert nichols</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-807568</link>
		<dc:creator>Albert nichols</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:16:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-807568</guid>
		<description>How long does a sleep have to stay for in order for me to worry?</description>
		<content:encoded><![CDATA[<p>How long does a sleep have to stay for in order for me to worry?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-804681</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 15 Apr 2011 04:43:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-804681</guid>
		<description>Hey peoples.

Anyone interested in this feature I have requested?

http://bugs.mysql.com/bug.php?id=60476</description>
		<content:encoded><![CDATA[<p>Hey peoples.</p>
<p>Anyone interested in this feature I have requested?</p>
<p><a href="http://bugs.mysql.com/bug.php?id=60476" rel="nofollow">http://bugs.mysql.com/bug.php?id=60476</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Jackson</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-801741</link>
		<dc:creator>Steve Jackson</dc:creator>
		<pubDate>Tue, 15 Mar 2011 14:07:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-801741</guid>
		<description>A popular way to show what is running a query is to attach tracking info in a comment in the SQL.

However, as it is at the moment, tracking info in an SQL comment is useless in sleep state as the INFO column in processlist is NULL when connection is sleeping (understandable as there is actually no query executing)

Therefore, I would like to see a &quot;Last Query&quot; in the processlist, which simply maintains the last query name that was executed on that connection. Then tracking info in an SQL comment becomes much more useful in these &quot;sleeping thread&quot;, and other scenarios...

This would be a VERY powerful feature if you ask me.

I have seen it mentioned before, but it was closed as they did not consider it to be worthy of their attention...

http://forge.mysql.com/worklog/task.php?id=384

Cheers

//Steve</description>
		<content:encoded><![CDATA[<p>A popular way to show what is running a query is to attach tracking info in a comment in the SQL.</p>
<p>However, as it is at the moment, tracking info in an SQL comment is useless in sleep state as the INFO column in processlist is NULL when connection is sleeping (understandable as there is actually no query executing)</p>
<p>Therefore, I would like to see a &#8220;Last Query&#8221; in the processlist, which simply maintains the last query name that was executed on that connection. Then tracking info in an SQL comment becomes much more useful in these &#8220;sleeping thread&#8221;, and other scenarios&#8230;</p>
<p>This would be a VERY powerful feature if you ask me.</p>
<p>I have seen it mentioned before, but it was closed as they did not consider it to be worthy of their attention&#8230;</p>
<p><a href="http://forge.mysql.com/worklog/task.php?id=384" rel="nofollow">http://forge.mysql.com/worklog/task.php?id=384</a></p>
<p>Cheers</p>
<p>//Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PaulW</title>
		<link>http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/comment-page-1/#comment-799455</link>
		<dc:creator>PaulW</dc:creator>
		<pubDate>Mon, 21 Feb 2011 11:34:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/02/08/debugging-sleeping-connections-with-mysql/#comment-799455</guid>
		<description>Great info, thanks for the post, I&#039;m off to try and sort our sleeping connections now.

Not sure if it&#039;s just me but on Mac with Safari and Chrome the code section is a bit screwy. The closing tegs don&#039;t seem to work. After the line &quot;This port can be used to identify the process which owns connection in question:&quot; the rest of the article is in a code block</description>
		<content:encoded><![CDATA[<p>Great info, thanks for the post, I&#8217;m off to try and sort our sleeping connections now.</p>
<p>Not sure if it&#8217;s just me but on Mac with Safari and Chrome the code section is a bit screwy. The closing tegs don&#8217;t seem to work. After the line &#8220;This port can be used to identify the process which owns connection in question:&#8221; the rest of the article is in a code block</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-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 (&#8220;localhost&#8221;, &#8220;root&#8221;, &#8220;password&#8221;);</p>
<p>$query = &#8220;show processlist;&#8221;;</p>
<p>$result = mysql_query($query) or die (&#8220;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(&#8220;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>
</channel>
</rss>

