MySQL view the number of connections per IP

  1. View the MySQL connection count statement for each 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. View the list of IPs and ports connected to MySQL:
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. Check which clients have maintained connections in the current MySQL database, and how many connections each client has maintained:
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. View the status of each IP of MySQL:
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) intercepts the string according to the keyword:
    str: the intercepted field;
    delim: the keyword;
    count: the number of times the keyword appears.
    For example, intercept the substring to the left of the second "." in the string "blog.jb51.net":
mysql> select substring_index("blog.jb51.net", ".", 2) as result;
+-----------+
| result    |
+-----------+
| blog.jb51 |
+-----------+
1 row in set (0.01 sec)

  Note: If the number of occurrences of the keyword is a negative number, such as -2, it is counted down from the end to the end of the string. As follows:

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

  Blog reference:

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/109464169