LoginSignup
12
13

More than 5 years have passed since last update.

MySQLのコネクション数一覧を表示するSQL

Posted at

概要

MySQLのコネクション一覧を更新するSQLになります。下記SQLだと、Sleepとかのステータスももってきてしまいますが、
そのあたりは、適宜条件を追加していただければと思います。

定義

mysql> desc information_schema.processlist ;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ID      | bigint(4)   | NO   |     | 0       |       |
| USER    | varchar(16) | NO   |     |         |       |
| HOST    | varchar(64) | NO   |     |         |       |
| DB      | varchar(64) | YES  |     | NULL    |       |
| COMMAND | varchar(16) | NO   |     |         |       |
| TIME    | int(7)      | NO   |     | 0       |       |
| STATE   | varchar(64) | YES  |     | NULL    |       |
| INFO    | longtext    | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

SQL

select
  '[' || RTRIM(IFNULL(X.db,'N/A')) || ']' || RTRIM(X.host) || '@' || RTRIM(count(X.id)) AS CLIENT_NAME
from
(
  select
   replace(host,mid(host,instr(host,':'),length(host)),'') as host,
   db,
   id
  from
    information_schema.processlist
) as X
group by
  X.db,
  X.host
order by
  X.db asc,
  X.host asc;

出力結果

@以降がコネクション数
[]がホスト名

といった感じで表示しております。

CLIENT_NAME
----------------------------------
[N/A]@2
[N/A]192.168.0.249@1
[N/A]localhost@1
[master]192.168.0.126@20
[master]192.168.0.127@20
[master]192.168.0.128@20
[master]192.168.0.129@20
[master]192.168.0.130@20
[master]192.168.0.135@20
[master]192.168.0.136@20
[master]192.168.0.137@20
[master]192.168.0.138@20
[master]192.168.0.140@2
[master]192.168.0.144@21
[master]192.168.0.147@10
[master]192.168.0.167@20
[master]192.168.0.168@20
[master]192.168.0.184@20
[master]192.168.0.185@20
12
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13