If you have servers which have multiple application servers connected to them, you often need to see things like who's connected, how many connections they have, and which users. Using SHOW PROCESSLIST doesn't work that well, since it gives you a row for each server.
And it is achieved using a simple query such as this one:+-----------------+-----------------+----------+ | host_short | users | count(*) | +-----------------+-----------------+----------+ | slave1 | repl | 1 | | slave2 | repl | 1 | | localhost | event_scheduler | 1 | | 111.111.222.111 | root, foo | 2 | | 111.111.222.222 | appuser, bar | 3 | | 111.111.222.333 | appuser, moshe | 9 | +-----------------+-----------------+----------+
SELECT SUBSTRING_INDEX(host, ':', 1) AS host_short,
GROUP_CONCAT(DISTINCT USER) AS users,
COUNT(*)
FROM information_schema.processlist
GROUP BY host_short
ORDER BY COUNT(*),
host_short;
A final note: I'm not sure what version of MySQL this query needs to function, but it works great on MySQL 5.5, and should work just as well on MySQL 5.1.
Hi Shlomo,
ReplyDeleteInstead of doing LEFT(..,IF...LOCATE...LENGTH)
You can simply write
SUBSTRING_INDEX(host, ':', 1)
This will return the correct result even if there's no ':' in the text.
You're right, that's a much simpler way :)
ReplyDeleteUpdated the query.
Thanks!
Great query, thx.
ReplyDeleteI'll integrate it at my admin menu.
thx usefull query , a small variation using ROLLUP is also handy
ReplyDeleteSELECT SUBSTRING_INDEX(host, ':', 1) AS host_short,
GROUP_CONCAT(DISTINCT USER) AS users,
COUNT(*)
FROM information_schema.processlist
GROUP BY host_short WITH ROLLUP
This query is awesome, just saying.
ReplyDeleteExcellent post. It was helpful to me today.
DeleteHi Shlomo:
ReplyDeleteI dont understand very well "information_schema.processlist". You said that I can see who is connected to mysql server right? I dont know what I must to do with the result of the query you posted. The users fields that your query out put are the sames as the fields of your 'username' fields of your tables, or are the connection´s username? Thanks...