web-dev-qa-db-ja.com

mysqlのスリーププロセスを自動終了するシェルスクリプト

Mysqlのスリーププロセスを強制終了する方法

 + ------ + ----------- + ----------- + ------------- ----------- + --------- + ------ + ---------------- + ---- -------------------------------------------------- ------------------------------------- + 
 | Id |ユーザー|ホスト| db |コマンド|時間|州|情報| 
 + ------ + ----------- + ----------- + ----------- ------------- + --------- + ------ + ---------------- +- -------------------------------------------------- --------------------------------------- + 
 | 2477 |ステージユーザー| localhost | jj_production_11102013 |クエリ| 0 |終了| SELECT * FROM wp_comments WHERE blog_id = 1071 ORDER BY comment_date_gmt DESC LIMIT 0、50 | 
 | 3050 |ステージユーザー| localhost | jj_production_11102013 |クエリ| 0 |ソート結果| SELECT * FROM wp_comments WHERE blog_id = 1071 ORDER BY comment_date_gmt DESC LIMIT 0、50 | 
 | 3052 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 336 | | NULL | 
 | 3056 |ステージユーザー| localhost | NULL |クエリ| 0 | NULL |プロセスリストを表示| 
 | 3057 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 301 | | NULL | 
 | 3058 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 299 | | NULL | 
 | 3059 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 298 | | NULL | 
 | 3061 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 273 | | NULL | 
 | 3068 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 251 | | NULL | 
 | 3072 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 233 | | NULL | 
 | 3111 |ステージユーザー| localhost | jj_production_11102013 |睡眠| 1 | | NULL | 
 + ------ + ----------- + ----------- + ----------- ------------- + --------- + ------ + ---------------- +- -------------------------------------------------- --------------------------------------- + 
 11行のセット( 0.00秒)

このスリーププロセスは、他の遅いクエリのようにサイトのパフォーマンスに影響しますか?

13
Vishal Kamal

やった。

Kill_sleep.shファイルを作成します。

mysql -u<user> -p<password> -h<Host> -e "select concat('KILL ',id,';')  into outfile '/tmp/sleep_processes.txt' from information_schema.processlist where Command = 'Sleep'"
mysql -u<user> -p<password> -h<Host> -e "source /tmp/sleep_processes.txt;"
rm -rf /tmp/sleep_processes.txt

そしてkill_sleep.shをcronジョブに設定します。

11
Vishal Kamal

MySQLサーバーでコマンドを実行している場合はVishalの回答が適切に機能しますが、サーバーにリモートで接続している場合、またはSOURCEまたはSELECT ... INTO OUTFILEを実行する権限がない場合は機能しません(例: 。AmazonのRDS)。ただし、これらの機能に依存しないように書き直すことは可能で、どこでも機能します。

mysql -h<Host> -u<user> -p -e "SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE Command = 'Sleep'" > sleep.txt
cat sleep.txt | xargs -I% mysql -h<Host> -u<user> -p -e "%"
5
Ben Dowling

構文は次のとおりです。

KILL thread_id

あなたの場合:

  mysql > KILL 3057

しかし、すべてのスリーププロセスを削除するには、1つのコマンドを使用できません。tmpテーブルのすべてのプロセスを取得してループした後、プロセスリスト全体をループする必要があります。

select concat('KILL ',id,';') from information_schema.processlist where Command='Sleep';

select concat('KILL ',id,';') from information_schema.processlist where Command='Sleep' into outfile '/tmp/a.txt';

参照元 ここ

2
Jhanvi

Perconaツール:

pt-kill --match-command Sleep --idle-time 100 --victims all  --interval 30 --kill

これにより、「スリープ」状態で100秒以上アイドル状態にあるすべての接続が検出され、それらが強制終了されます。 --interval 30は、30秒ごとにこれを実行し続けます。したがって、画面-S ptkillを開き、その画面で上記のコマンドを実行し、次にctrl-A、Dを実行してターミナルを切り離して終了すると、接続のクリーンアップが続行されます。

https://www.percona.com/doc/percona-toolkit/2.1/pt-kill.html

0
John Peterson