Yanıtlar:
Onları birer birer öldürmeniz gerekiyor, MySQL'in büyük bir öldürme komutu yok. Herhangi bir dilde komut dosyası yazabilirsiniz, örneğin PHP'de aşağıdaki gibi bir şey kullanabilirsiniz:
$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
Toplu öldürme operasyonu zaman kazandırır. MySql'in kendisinde yapın:
Bu komutları çalıştırın
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------------
dosyada saklamak istemiyorsanız, bir variable
Sadece komut isteminizde çalıştırın
> 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")
Ayrıca MySQL ile SHOW PROCESSLIST komutunun nasıl ayrıştırılacağını araştırdım ve bir Kabukta tek satırlık bir sonuçla bitirdim:
mysqladmin processlist -u <USERNAME> -p<PASSWORD> | \
awk '$2 ~ /^[0-9]/ {print "KILL "$2";"}' | \
mysql -u <USERNAME> -p<PASSWORD>
Belirli bir veritabanı adını filtrelemek için awk komutundan önce grep çalıştırabilirsiniz .
Veya ... kabukta ...
service mysql restart
Evet, biliyorum, tembelim ama kullanışlı da olabilir.
Bundan daha basit olmaz, sadece bunu mysql komut isteminde çalıştırın.
kill USER username;
Sağlanan kullanıcı adı altındaki tüm işlemleri öldürecektir. çünkü çoğu insan tüm amaç için aynı kullanıcıyı kullanıyor, işe yarıyor!
Bunu MariaDB'de test ettim, mysql hakkında emin değilim.
KILL [CONNECTION | QUERY] processlist_id. Sorgunuzu MySQL 5.6.34'te denedim ve bir sözdizimi hatası olarak kabul edildi.
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 Muhtemelen cevabınız yalnızca MariaDB'ye özgü bir özelliktir.
Aşağıdakiler, şu anda kullanılmakta olan işlem haricinde tüm işlemleri tek tek öldürmek için bir imleç kullanan basit bir saklı yordam oluşturacaktır:
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 ;
Önceki SELECTve sonraki süreçleri aşağıdaki gibi göstermek için s ile çalıştırılabilir :
SELECT * FROM information_schema.processlist;
CALL kill_other_processes();
SELECT * FROM information_schema.processlist;
Son zamanlarda bunu yapmam gerekiyordu ve bunu buldum
-- 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;
Bu şekilde, tüm sorguları tek bir komutla dosyalamak ve çalıştırmak için depolamanıza gerek kalmaz.
TÜM SEÇİLEN SORGULARI ÖLDÜR
select concat('KILL ',id,';')
from information_schema.processlist
where user='root'
and INFO like 'SELECT%' into outfile '/tmp/a.txt';
source /tmp/a.txt;
Mysql'de yönetici olarak oturum açın:
mysql -uroot -ppassword;
Ve komut çalıştırdıktan sonra:
mysql> show processlist;
Aşağıdaki gibi bir şey alacaksınız:
+----+-------------+--------------------+----------+---------+------+-------+------------------+
| 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 |
+----+-------------+--------------------+----------+---------+------+-------+------------------+
Farklı bağlantıların tüm ayrıntılarını göreceksiniz. Şimdi uyku bağlantısını aşağıdaki gibi kapatabilirsiniz:
mysql> kill 52;
Query OK, 0 rows affected (0.00 sec)
Bu, tüm MySQL işlemlerini öldürmek için benim için çalıştı (MySQL sunucusu 5.5):
mysql -e "show full processlist;" -ss | awk '{print "KILL "$1";"}'| mysql
İşte işletim sistemine güvenmeden uygulayabileceğiniz bir çözüm:
ADIM 1: Bir saklı yordam oluşturun.
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 ;
ADIM 2: İşlemlerini sonlandırmak istediğiniz bir veritabanı kullanıcısının adını vererek saklı yordamı çağırın. İsterseniz başka ölçütlere göre filtrelemek için saklı yordamı yeniden yazabilirsiniz.
ARAMAK 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 ile yapabiliriz. Sadece şunu çalıştırın:
kill id;
Misal:
kill 13412
Bu onu kaldıracak.
#! /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;
Aşağıdakiler benim için harika çalıştı:
echo "show processlist" | mysql | grep -v ^Id | awk '{print $1}' | xargs -i echo "KILL {}; | mysql"
Komutu flush tables, aslında toplu problemin olduğu tüm aktif olmayan bağlantıları öldürmek için kullandım .
python dili için bunu yapabilirsin
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 kullanıyorsanız, o zaman bu tam size göre:
$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));
}
}
Elbette bunu use Illuminate\Support\Facades\DB;ad alanınızdan sonra kullanmalısınız .
Sorgu 1 information_schema.processlist'ten concat ('KILL', id, ';') seçin; burada user = 'username' outfile '/tmp/a.txt';
Sorgu 2 kaynağı a.txt
Bu, gösteri işlem listesindeki tüm sorguları belirli bir kullanıcıya göre sonlandırmanızı sağlayacaktır.
sudo service mysqld restart