插件窝 干货文章 MySQL如何查询客户端连接情况

MySQL如何查询客户端连接情况

class 连接 MySQL 查询 611    来源:    2024-10-28

MySQL查询客户端连接情况

查询MySQL当前所有连接及状态

mysql> SELECT host, state FROM information_schema.processlist;
+----------------------+-----------+
| host                 | state     |
+----------------------+-----------+
| localhost            | executing |
| 183.192.226.255:1842 |           |
| 183.192.226.255:1841 |           |
+----------------------+-----------+
3 rows in set (0.00 sec)

查询MySQL当前连接IP、状态及连接数量

mysql> SELECT substring_index(host, ':', 1) AS ip, state, count(*) FROM information_schema.processlist GROUP BY ip, state;
+-----------------+-----------+----------+
| ip              | state     | count(*) |
+-----------------+-----------+----------+
| 183.192.226.255 |           |        2 |
| localhost       | executing |        1 |
+-----------------+-----------+----------+
2 rows in set (0.00 sec)

查询MySQL当前连接IP及其连接数量

mysql> SELECT substring_index(host, ':', 1) AS ip, count(*) FROM information_schema.processlist GROUP BY ip;       
+-----------------+----------+
| ip              | count(*) |
+-----------------+----------+
| 183.192.226.255 |        2 |
| localhost       |        1 |
+-----------------+----------+
2 rows in set (0.01 sec)

查询MySQL最大连接数

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 600   |
+-----------------+-------+
1 row in set (0.01 sec)

查询MySQL当前连接数

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

总结

以上为个人经验,希望对您有所帮助。