MySQL - Get a list of connections grouped by host
Topics: MySQL administration
The following SQL statement generates a list of current connections (except the query itself). To provide a connection count per hostname, it strips off the specific port numbers.
SELECT SUBSTRING_INDEX(host, ':', 1) AS hostname,
COUNT(*) AS connections
FROM performance_schema.processlist
WHERE id <> CONNECTION_ID()
GROUP BY hostname
ORDER_BY connections;
For MySQL versions older than 8.x, use this one:
SELECT SUBSTRING_INDEX(host, ':', 1) AS hostname,
COUNT(*) AS connections
FROM information_schema.PROCESSLIST
WHERE id <> CONNECTION_ID()
GROUP BY hostname
ORDER BY connections DESC;