How to Monitor MySQL Connections per IP Using SQL and Shell Commands
This guide explains how to monitor MySQL connection counts per IP by using a SQL query on information_schema.processlist and shell commands that parse the process list, providing a clear method to track each client’s connections.
To monitor the number of database connections per IP, instead of only counting the total connections, you can use the following SQL query:
SELECT SUBSTRING_INDEX(host, ':', 1) AS ip, COUNT(*) FROM information_schema.processlist GROUP BY ip;The query extracts the IP part from the host column and groups the results to count connections per IP.
Alternatively, you can achieve the same using shell commands that invoke MySQL and process the output with egrep, awk, sort, and uniq:
# /usr/local/mysql/bin/mysql -u root -h127.0.0.1 -e"show processlist\G;" | egrep "Host:" | awk -F: '{ print $2 }' | sort | uniq -cOr a more compact version:
# /usr/local/mysql/bin/mysql -u root -h127.0.0.1 --skip-column-names -e"show processlist;" | awk '{print $3}' | awk -F":" '{print $1}' | sort | uniq -cThese commands list the processlist, extract the host IP, and count occurrences to show the number of connections from each IP.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
