Answers:
您需要一个一个地杀死它们,MySQL没有任何大量的kill命令。您可以使用任何语言编写脚本,例如在PHP中,您可以使用类似以下内容的脚本:
$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["Time"] > 200 ) {
$sql="KILL $process_id";
mysql_query($sql);
}
}
for i in {994..1145}; do mysql -uroot -p123456 -e "kill $i" ; done
大规模杀戮操作节省了时间。在MySql本身中执行此操作:
运行这些命令
mysql> select concat('KILL ',id,';') from information_schema.processlist
where user='root' and time > 200 into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;
---------edit------------
如果您不想存储在文件中,请存储在 variable
只需在命令提示符下运行
> out1=$(mysql -B test -uroot -proot --disable-column-names -e "select concat('KILL ',id,';') from information_schema.processlist where user='root' and time > 200;")
> out2= $(mysql -B test -uroot -proot --disable-column-names -e "$out1")
我还搜索了如何通过MySQL解析命令SHOW PROCESSLIST并在Shell中以单线结尾:
mysqladmin processlist -u <USERNAME> -p<PASSWORD> | \
awk '$2 ~ /^[0-9]/ {print "KILL "$2";"}' | \
mysql -u <USERNAME> -p<PASSWORD>
您可以在awk命令之前运行grep来过滤特定的数据库名称。
或者...在外壳...
service mysql restart
是的,我知道,我很懒,但是也很方便。
然后,它变得不那么简单了,只需在mysql提示符下执行即可。
kill USER username;
它将杀死提供的用户名下的所有进程。因为大多数人出于同一目的使用同一用户,所以它可行!
我已经在MariaDB上测试过此数据库,但不确定关于mysql。
KILL [CONNECTION | QUERY] processlist_id
。我在MySQL 5.6.34中尝试了您的查询,这被认为是语法错误。
mysql> kill user root; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'root' at line 1
可能您的答案仅是MariaDB特有的功能。
下面将创建一个简单的存储过程,该过程使用游标一个一个地杀死所有进程,当前正在使用的进程除外:
DROP PROCEDURE IF EXISTS kill_other_processes;
DELIMITER $$
CREATE PROCEDURE kill_other_processes()
BEGIN
DECLARE finished INT DEFAULT 0;
DECLARE proc_id INT;
DECLARE proc_id_cursor CURSOR FOR SELECT id FROM information_schema.processlist;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN proc_id_cursor;
proc_id_cursor_loop: LOOP
FETCH proc_id_cursor INTO proc_id;
IF finished = 1 THEN
LEAVE proc_id_cursor_loop;
END IF;
IF proc_id <> CONNECTION_ID() THEN
KILL proc_id;
END IF;
END LOOP proc_id_cursor_loop;
CLOSE proc_id_cursor;
END$$
DELIMITER ;
可以与SELECT
s一起运行以显示之前和之后的过程,如下所示:
SELECT * FROM information_schema.processlist;
CALL kill_other_processes();
SELECT * FROM information_schema.processlist;
我最近需要这样做,并且我想到了这个
-- GROUP_CONCAT turns all the rows into 1
-- @q:= stores all the kill commands to a variable
select @q:=GROUP_CONCAT(CONCAT('KILL ',ID) SEPARATOR ';')
FROM information_schema.processlist
-- If you don't need it, you can remove the WHERE command altogether
WHERE user = 'user';
-- Creates statement and execute it
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
这样,您无需使用单个命令即可存储到文件并运行所有查询。
以管理员身份登录Mysql:
mysql -uroot -ppassword;
并且比运行命令:
mysql> show processlist;
您将获得以下内容:
+----+-------------+--------------------+----------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+--------------------+----------+---------+------+-------+------------------+
| 49 | application | 192.168.44.1:51718 | XXXXXXXX | Sleep | 183 | | NULL ||
| 55 | application | 192.168.44.1:51769 | XXXXXXXX | Sleep | 148 | | NULL |
| 56 | application | 192.168.44.1:51770 | XXXXXXXX | Sleep | 148 | | NULL |
| 57 | application | 192.168.44.1:51771 | XXXXXXXX | Sleep | 148 | | NULL |
| 58 | application | 192.168.44.1:51968 | XXXXXXXX | Sleep | 11 | | NULL |
| 59 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+-------------+--------------------+----------+---------+------+-------+------------------+
您将看到不同连接的完整详细信息。现在,您可以按以下方式终止睡眠连接:
mysql> kill 52;
Query OK, 0 rows affected (0.00 sec)
您可以在不依赖操作系统的情况下执行以下解决方案:
步骤1:创建一个存储过程。
DROP PROCEDURE IF EXISTS kill_user_processes$$
CREATE PROCEDURE `kill_user_processes`(
IN user_to_kill VARCHAR(255)
)
READS SQL DATA
BEGIN
DECLARE name_val VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE friends_cur CURSOR FOR
SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE USER=user_to_kill;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
SELECT FOUND_ROWS() INTO num_rows;
the_loop: LOOP
FETCH friends_cur
INTO name_val;
IF no_more_rows THEN
CLOSE friends_cur;
LEAVE the_loop;
END IF;
SET @s = name_val;
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT name_val;
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
SELECT num_rows, loop_cntr;
END $$
DELIMITER ;
步骤2:调用存储过程,为其指定要杀死其进程的数据库用户的名称。如果愿意,可以重写存储过程以根据其他条件进行过滤。
呼叫 kill_user_processes('devdba');
mysqladmin pr -u 'USERNAME' -p'PASSWORD' | awk '$2~/^[0-9]+/{print $2}' | xargs -i mysqladmin -u 'USERNAME' -p'PASSWORD' kill {}
我们可以通过MySQL Workbench做到这一点。只需执行以下命令:
kill id;
例:
kill 13412
那将删除它。
#! /bin/bash
if [ $# != "1" ];then
echo "Not enough arguments.";
echo "Usage: killQueryByDB.sh <db_name>";
exit;
fi;
DB=${1};
for i in `mysql -u <user> -h localhost ${DB} -p<password> -e "show processlist" | sed 's/\(^[0-9]*\).*/\1/'`; do
echo "Killing query ${i}";
mysql -u <user> -h localhost ${DB} -p<password> -e "kill query ${i}";
done;
有时我有一些无法杀死的僵尸mysql进程(使用MAMP Pro)。
首先获得所有mysql进程的列表:
ps -ax | grep mysql
接下来用(将processId替换为先前命令结果中的第一列)杀死其中的每个人:
kill -9 processId
以下对我非常有用:
echo "show processlist" | mysql | grep -v ^Id | awk '{print $1}' | xargs -i echo "KILL {}; | mysql"
对于python语言,您可以这样做
import pymysql
connection = pymysql.connect(host='localhost',
user='root',
db='mysql',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
cursor.execute('SHOW PROCESSLIST')
for item in cursor.fetchall():
if item.get('Time') > 200:
_id = item.get('Id')
print('kill %s' % item)
cursor.execute('kill %s', _id)
connection.close()
如果您使用的是laravel,则适合您:
$query = "SHOW FULL PROCESSLIST";
$results = DB::select(DB::raw($query));
foreach($results as $result){
if($result->Command == "Sleep"){
$sql="KILL ". $result->Id;
DB::select(DB::raw($sql));
}
}
当然,您应该use Illuminate\Support\Facades\DB;
在命名空间之后使用它。
查询1从information_schema.processlist中选择concat('KILL',id,';'),其中user ='username'到输出文件'/tmp/a.txt'中;
查询2来源a.txt
这将使您能够杀死特定用户的show processlist中的所有查询。
sudo service mysqld restart