Processlist enhanced for MySQL 5.1 +

Developed In: SQL — Contributed by: Darren Cassar

This is an analogous show processlist written as a stored proc, which also enables killing all processes belonging to a particular user. It may come handy should a particular user take up all your connections on the server due to a bug on the application thus crippling your db.


Darren Cassar
SQL
  1. #######################################################################################
  2. ## ##
  3. ## Stored Procedure: process_list & kill user ##
  4. ## call process_list('show','%') show processlist for all users ##
  5. ## call process_list('show','root') show processlist for root user ##
  6. ## call process_list('kill','user1') kill connections for user1 ##
  7. ## ##
  8. ## by Darren Cassar http://www.mysqlpreacher.com ##
  9. ## ##
  10. #######################################################################################
  11.  
  12. DROP PROCEDURE IF EXISTS process_list;
  13.  
  14. DELIMITER $$
  15.  
  16. CREATE PROCEDURE `securich`.`process_list`( choice char(4), usernamein varchar(16), hostnamein varchar(60))
  17. BEGIN
  18.  
  19. DECLARE CURCONN int;
  20.  
  21. IF choice <> 'show' AND choice <> 'kill' then
  22. SELECT "wrong choice";
  23. END IF;
  24.  
  25. IF usernamein = '' then
  26. SET usernamein = '%';
  27. END IF;
  28.  
  29. IF hostnamein = '' then
  30. SET hostnamein = '%';
  31. END IF;
  32.  
  33. SET CURCONN=(SELECT connection_id());
  34.  
  35. IF choice = 'show' then
  36.  
  37. SELECT *
  38. FROM information_schema.processlist
  39. WHERE ID <> CURCONN AND
  40. USER LIKE usernamein AND
  41. ( HOST LIKE CONCAT(hostnamein ,':%') OR
  42. HOST LIKE hostnamein );
  43.  
  44. ELSEIF choice = 'kill' then
  45.  
  46. IF usernamein = 'root' then
  47. SELECT "Illegal username when killing processes";
  48. ELSE
  49. SET @CNT = (
  50. SELECT count(*)
  51. FROM information_schema.processlist
  52. WHERE ID <> CURCONN AND
  53. USER LIKE usernamein AND
  54. ( HOST LIKE CONCAT(hostnamein ,':%') OR
  55. HOST LIKE hostnamein )
  56. );
  57.  
  58. SET @VAR=1;
  59.  
  60. WHILE ( @VAR <= @CNT) DO
  61.  
  62. SET @TID = (
  63. SELECT id
  64. FROM information_schema.processlist
  65. WHERE ID <> CURCONN AND
  66. USER LIKE usernamein AND
  67. ( HOST LIKE CONCAT(hostnamein ,':%') OR
  68. HOST LIKE hostnamein ) LIMIT 1
  69. );
  70.  
  71. SET @k = CONCAT('kill ' , @TID);
  72. PREPARE killcom FROM @k;
  73. EXECUTE killcom;
  74. SET @k=NULL;
  75.  
  76. SET @VAR=@VAR+1;
  77.  
  78. END WHILE;
  79.  
  80. END IF;
  81.  
  82. END IF;
  83.  
  84. END$$
  85.  
  86. DELIMITER ;
  87.  

Current Tags

You must be logged in to tag this tool

I was getting this error because the daemon couldn't create a pid file in the requested place. It never told me that it couldn't, it just never wrote it.

accredited degree | Corllins University Scam

I faced a problem where mysql 5.0.19 (upgraded from 4.1.13) would just not connect from neither perl/java nor php. Command line connections were OK for root but not for other users even they had same privileges as the root user. Problem resolved by correcting the hosts file. Apparently if localhost isnt the first alias for 127.0.0.1, then you're in for trouble. Same situation with a postgres 8.1 installation. Good luck.

work experience degree | fake degree | college degree

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment