数据库 - 如何杀死Mysql“show processlist”中的所有进程?

因为我在那里看到了很多流程,而且"时间" 列显示所有这些值的大值。

TIMEX asked 2019-07-17T23:46:25Z

24个解决方案

192 votes

大规模杀戮操作可以节省 在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")

Angelin Nadar answered 2019-07-17T23:47:18Z

98 votes

你需要逐个杀死它们,MySQL没有任何大规模杀戮命令。 您可以使用任何语言编写脚本,例如在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);

}

}

Michal Čihař answered 2019-07-17T23:46:38Z

16 votes

我还搜索了如何通过MySQL解析命令SHOW PROCESSLIST并以Shell中的单行结束:

mysqladmin processlist -u -p | \

awk '$2 ~ /^[0-9]/ {print "KILL "$2";"}' | \

mysql -u -p

mysqladmin processlist将打印一个带有线程ID的表;

awk将从第二列解析数字(线程ID)并生成MySQL KILL命令;

最后对mysql的最后一次调用将执行传递的命令。

您可以在awk命令之前运行grep来过滤特定的数据库名称。

m8t answered 2019-07-17T23:48:09Z

12 votes

或者......在shell中......

service mysql restart

是的,我知道,我很懒,但它也很方便。

EcchiOli answered 2019-07-17T23:48:39Z

11 votes

它并不简单,只需在mysql提示符下执行此操作即可。

kill USER username;

它会在提供的用户名下杀死所有进程。 因为大多数人为了各种目的使用相同的用户,它的工作原理!

我在MariaDB上测试了这个,不确定mysql。

Maulik Gangani answered 2019-07-17T23:49:16Z

5 votes

如果您没有information_schema:

mysql -e "show full processlist" | cut -f1 | sed -e 's/^/kill /' | sed -e 's/$/;/' ; > /tmp/kill.txt

mysql> . /tmp/kill.txt

mikesl answered 2019-07-17T23:49:47Z

5 votes

以下将创建一个简单的存储过程,该过程使用游标逐个终止所有进程,但当前正在使用的进程除外:

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 ;

它可以与SELECTs一起运行,以显示之前和之后的过程,如下所示:

SELECT * FROM information_schema.processlist;

CALL kill_other_processes();

SELECT * FROM information_schema.processlist;

Steve Chambers answered 2019-07-17T23:50:18Z

4 votes

以admin身份登录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)

Suresh Kamrushi answered 2019-07-17T23:51:03Z

4 votes

杀死所有选择查询

select concat('KILL ',id,';')

from information_schema.processlist

where user='root'

and INFO like 'SELECT%' into outfile '/tmp/a.txt';

source /tmp/a.txt;

user3269995 answered 2019-07-17T23:51:27Z

3 votes

这是一个可以在不依赖操作系统的情况下执行的解决方案:

第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');

Dan Spiegel answered 2019-07-17T23:52:12Z

3 votes

这段剪辑为我(MySQL服务器5.5)工作以杀死所有MySQL进程:

mysql -e "show full processlist;" -ss | awk '{print "KILL "$1";"}'| mysql

Fedir RYKHTIK answered 2019-07-17T23:52:37Z

2 votes

我最近需要这样做,我想出了这个

-- 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;

这样,您就不需要使用单个命令存储到文件并运行所有查询。

Iberê answered 2019-07-17T23:53:08Z

2 votes

mysqladmin pr -u 'USERNAME' -p'PASSWORD' | awk '$2~/^[0-9]+/{print $2}' | xargs -i mysqladmin -u 'USERNAME' -p'PASSWORD' kill {}

user3721740 answered 2019-07-17T23:53:25Z

2 votes

我们可以通过MySQL Workbench来实现。 只需执行此操作:

kill id;

例:

kill 13412

这将删除它。

Aroon answered 2019-07-17T23:53:58Z

1 votes

我将bash和mysql结合起来:

for i in $(mysql -Ne "select id from information_schema.processlist where user like 'foo%user' and time > 300;"); do

mysql -e "kill ${i}"

done

wiebel answered 2019-07-17T23:54:22Z

0 votes

一个简单的方法是重启mysql服务器..打开&#34; services.msc&#34; 在Windows Run中,从列表中选择Mysql。 右键单击并停止服务。 然后再次启动,除了一个(默认的保留连接)之外,所有进程都将被终止

shashi009 answered 2019-07-17T23:54:47Z

0 votes

#! /bin/bash

if [ $# != "1" ];then

echo "Not enough arguments.";

echo "Usage: killQueryByDB.sh ";

exit;

fi;

DB=${1};

for i in `mysql -u -h localhost ${DB} -p -e "show processlist" | sed 's/\(^[0-9]*\).*/\1/'`; do

echo "Killing query ${i}";

mysql -u -h localhost ${DB} -p -e "kill query ${i}";

done;

Diego Andrés Díaz Espinoza answered 2019-07-17T23:55:05Z

0 votes

有时我有一些僵尸mysql进程无法被杀死(使用MAMP Pro)。

首先获取所有mysql进程的列表:

ps -ax | grep mysql

接下来杀死它们中的每一个(用上一个命令结果中的第一列替换processId):

kill -9 processId

Nico Prat answered 2019-07-17T23:55:43Z

0 votes

以下对我有用:

echo "show processlist" | mysql | grep -v ^Id | awk '{print $1}' | xargs -i echo "KILL {}; | mysql"

Timon de Groot answered 2019-07-17T23:56:07Z

0 votes

我使用命令flush tables来杀死实际上存在质量问题的所有非活动连接。

Meltzer answered 2019-07-17T23:56:32Z

0 votes

对于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()

BohanZhang answered 2019-07-17T23:56:57Z

0 votes

如果您正在使用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;。

Ziaur Rahman answered 2019-07-17T23:57:28Z

-1 votes

检查流程清单: -

show processlist;

获取进程ID运行

杀死processid;

satyandera answered 2019-07-17T23:58:12Z

-4 votes

查询1从information_schema.processlist中选择concat(&#39; KILL&#39;,id,&#39;;&#39;),其中user =&#39; username&#39; 进入outfile&#39; /tmp/a.txt' ;;

查询2来源a.txt

这将使您能够杀死特定用户在show processlist中的所有查询。

Saurav Sood answered 2019-07-17T23:58:51Z

Logo

一站式 AI 云服务平台

更多推荐