Databases 2 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Monitor MySQL Connections per IP Using SQL and Shell Commands

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 -c

Or 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 -c

These commands list the processlist, extract the host IP, and count occurrences to show the number of connections from each IP.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLmysqlShell Commandsconnection monitoringIP tracking
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.