HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

View active MySQL connections per user

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
activeperuserviewmysqlconnections

Problem

I need a query that will give me the active or open number of connections to a certain database for a certain user. I have been searching for hours and have come up with nothing so far. I am close, so here is what I have tried.

-
SHOW STATUS WHERE variable_name = 'Threads_connected';

-
SHOW STATUS LIKE '%onn%';

-
SELECT * FROM information_schema.processlist WHERE USER='database_user';

I work for a web hosting company, and one of our clients keeps hitting his max_user_connections limit, so in troubleshooting why I need to know how many connections his user is using right now since he is no a shared server. I am currently using MySQL (InnoDB) version 5.5.36. Any help would be greatly appreciated!

Solution

What you need is a breakdown by user and hostname along with a total

SELECT IFNULL(usr,'All Users') user,IFNULL(hst,'All Hosts') host,COUNT(1) Connections
FROM
(
    SELECT user usr,LEFT(host,LOCATE(':',host) - 1) hst
    FROM information_schema.processlist
    WHERE user NOT IN ('system user','root')
) A GROUP BY usr,hst WITH ROLLUP;


This will handle host address that have a colon separating hostname and port number

I hope everybody is not logging in as root

Code Snippets

SELECT IFNULL(usr,'All Users') user,IFNULL(hst,'All Hosts') host,COUNT(1) Connections
FROM
(
    SELECT user usr,LEFT(host,LOCATE(':',host) - 1) hst
    FROM information_schema.processlist
    WHERE user NOT IN ('system user','root')
) A GROUP BY usr,hst WITH ROLLUP;

Context

StackExchange Database Administrators Q#115309, answer score: 25

Revisions (0)

No revisions yet.