MySQL 查看每个 IP 的连接数

吾爱主题 阅读:168 2024-04-05 14:21:33 评论:0
  1. 查看 MySQL 每个 IP 的连接数语句:
mysql> SELECT SUBSTRING_INDEX(HOST,':',1) AS ip , COUNT(*) FROM information_schema.processlist GROUP BY ip;
+--------------+----------+
| ip           | COUNT(*) |
+--------------+----------+
| 106.54.90.27 |        3 |
+--------------+----------+
1 row in set (0.00 sec)
  1. 查看连接 MySQL 的 IP 和端口列表:
mysql> SELECT HOST AS ip FROM information_schema.processlist;
+--------------------+
| ip                 |
+--------------------+
| 106.54.90.27:32998 |
| 106.54.90.27:48800 |
| 106.54.90.27:54948 |
+--------------------+
3 rows in set (0.00 sec)
  1. 查看当前 MySQL 数据库中,有哪些客户端保持了连接, 每个客户端分别保持了多少连接:
mysql> SELECT substring_index(host, ':',1) AS host_name, state, count(*) FROM information_schema.processlist GROUP BY state, host_name;
+--------------+-----------+----------+
| host_name    | state     | count(*) |
+--------------+-----------+----------+
| 106.54.90.27 |           |        2 |
| 106.54.90.27 | executing |        1 |
+--------------+-----------+----------+
2 rows in set (0.00 sec)
  1. 查看 MySQL 每个 IP 的状态:
mysql> SELECT SUBSTRING_INDEX(HOST, ':',1) AS host_name, state FROM information_schema.processlist GROUP BY state, host_name;
+--------------+-----------+
| host_name    | state     |
+--------------+-----------+
| 106.54.90.27 |           |
| 106.54.90.27 | executing |
| 101.54.82.27 | query end |
+--------------+-----------+
3 rows in set (0.00 sec)
  1. SUBSTRING_INDEX(str, delim, count) 按关键字截取字符串:
    str: 被截取字段;
    delim: 关键字;
    count: 关键字出现的次数。
    举例如下,截取字符串 “blog.tuohang.net” 中第二个 “.” 左边的子串:
mysql> select substring_index("blog.tuohang.net", ".", 2) as result;
+-----------+
| result    |
+-----------+
| blog.tuohang |
+-----------+
1 row in set (0.01 sec)

  备注:如果关键字出现的次数是负数,如 -2,则是从后倒数,到字符串结束。如下所示:

mysql> select substring_index("blog.tuohang.net", ".", -2) as result;
+----------+
| result   |
+----------+
| tuohang.net |
+----------+
1 row in set (0.00 sec)

  博客参考:

  • MySQL查看所有连接的客户端ip
  • mysql 函数substring_index()
  • Mysql中查看每个IP的连接数

本文地址:https://blog.csdn.net/piaoranyuji/article/details/109464169

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容