Help us understand the problem. What is going on with this article?

MySQL DBのコネクション数の確認とか

More than 1 year has passed since last update.

mysql クライアント等で以下を実行

現在の接続しているスレッド数

mysql> show status like 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 3     |
+-------------------+-------+
1 row in set (0.00 sec)

あるいは SELECT文 で

> SELECT * FROM information_schema.PROCESSLIST;

プロセスリスト(処理中の接続)を表示する

mysql> show processlist;
+------+------+-----------------+---------------------------+---------+------+-------+------------------+
| Id   | User | Host            | db                        | Command | Time | State | Info             |
+------+------+-----------------+---------------------------+---------+------+-------+------------------+
|   68 | root | localhost:36562 | hogehogehogehogehogehogeh | Sleep   |   30 |       | NULL             |
|   69 | root | localhost:36563 | NULL                      | Sleep   |   40 |       | NULL             |
| 2268 | root | localhost       | hogehogehogehogehogehogeh | Query   |    0 | init  | show processlist |
+------+------+-----------------+---------------------------+---------+------+-------+------------------+
3 rows in set (0.00 sec)

不要なコネクションをkill

mysql> kill 69;
Query OK, 0 rows affected (0.00 sec)

60秒以上経過しているコネクションを抽出

RDS の場合'rdsadmin'は除外

mysql> SELECT * FROM information_schema.PROCESSLIST WHERE time >= 60 and user != 'rdsadmin';

そのIDをカンマ区切りで抽出

mysql> SELECT GROUP_CONCAT(id) FROM information_schema.PROCESSLIST WHERE time >= 60 and user != 'rdsadmin';

まとめて kill

$ mysqladmin kill xx,xx,xx -h host -u user -p

(おまけ)起動してからの累積接続数

mysql> show status like 'Conn%'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 85    |
+---------------+-------+
1 row in set (0.01 sec)

(おまけ)プライマリ/レプリカの判別

SHOW GLOBAL VARIABLES LIKE 'innodb_read_only';

プライマリは OFF, レプリカは ON

参考

snaka
VB,Java,.NET,Unity(C#)とやってきて、今はRubyときどきJavaScriptでサーバサイド中心。2020年は Go言語, TypeScript やっていきたい。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした