<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MySQL Preacher &#187; processes</title>
	<atom:link href="http://mysqlpreacher.com/wordpress/tag/processes/feed/" rel="self" type="application/rss+xml" />
	<link>http://mysqlpreacher.com/wordpress</link>
	<description>A MySQL blog, from a MySQL DBA</description>
	<lastBuildDate>Wed, 16 Jun 2010 13:13:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL processlist &#8211; (show/kill processes)</title>
		<link>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/</link>
		<comments>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 10:55:41 +0000</pubDate>
		<dc:creator>Darren Cassar</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[mysql stored procedure]]></category>
		<category><![CDATA[processes]]></category>
		<category><![CDATA[processlist]]></category>
		<category><![CDATA[sp]]></category>
		<category><![CDATA[stored proc]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://mysqlpreacher.com/wordpress/?p=208</guid>
		<description><![CDATA[It"s not the most common task in the world, but you might want to view processes from a particular user and once in a while you might even need to kill processes from a single user, be it during an attack or because you simply got a bug in an application bombarding your db server with connections!]]></description>
			<content:encoded><![CDATA[<p>It&#8221;s not the most common task in the world, but you might want to view processes from a particular user and once in a while you might even need to kill processes from a single user, be it during an attack or because you simply got a bug in an application bombarding your db server with connections!</p>
<p>Here is a small stored procedure which does exactly that!</p>
<blockquote><p><code lang="sql">call process_list("show","username","hostname");</code><br />
&#8211; shows all processes owned by username@hostname<br />
<code lang="sql">call process_list("kill","username","hostname");</code><br />
&#8211; kills all processes owned by username@hostname</p></blockquote>
<p>The code for this stored procedure can be found below. If you have any comments / suggestions feel free to comment below.</p>
<blockquote><p><code lang="sql">######################################################################<br />
##                                                                  ##<br />
##  Stored Procedure: process_list &amp; kill user                  ##<br />
##  call process_list("show","%") show processlist for all users    ##<br />
##  call process_list("show","root") show processlist for root user ##<br />
##  call process_list("kill","user1") kill connections for user1    ##<br />
##                                                                  ##<br />
##  by Darren Cassar http://www.mysqlpreacher.com                   ##<br />
##                                                                  ##<br />
######################################################################</code></p>
<p><code lang="sql">DROP PROCEDURE IF EXISTS process_list;</code></p>
<p><code lang="sql">DELIMITER $$</code></p>
<p><code lang="sql">CREATE PROCEDURE "process_list"( choice char(4), usernamein varchar(16), hostnamein varchar(60))<br />
BEGIN</p>
<p>DECLARE CURCONN int;</p>
<p>IF choice &lt;&gt; "show" AND choice &lt;&gt; "kill" then<br />
select "wrong choice";<br />
END IF;</p>
<p>IF usernamein = "" then<br />
set usernamein = "%";<br />
END IF;</p>
<p>IF hostnamein = "" then<br />
set hostnamein = "%";<br />
END IF;</p>
<p>SET CURCONN=(select connection_id());</p>
<p>IF choice = "show" then</p>
<p>select *<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein );</p>
<p>ELSEIF choice = "kill" then</p>
<p>IF usernamein = "root" then<br />
select "Illegal username when killing processes";<br />
ELSE<br />
SET @CNT = (<br />
select count(*)<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein )<br />
);</p>
<p>SET @VAR=1;</p>
<p>WHILE ( @VAR &lt;= @CNT) DO</p>
<p>SET @TID = (<br />
select id<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein ) limit 1<br />
);</p>
<p>SET @k = CONCAT("kill " , @TID);<br />
PREPARE killcom FROM @k;<br />
EXECUTE killcom;<br />
set @k=NULL;</p>
<p>SET @VAR=@VAR+1;</p>
<p>END WHILE;</p>
<p>END IF;</p>
<p>END IF;</p>
<p>END$$</p>
<p></code><code lang="sql">DELIMITER ;<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
