Skip to content

Connections by command

Open sessions grouped by what they are doing, against max_connections.

MySQL · SQL
SELECT command,       COUNT(*) AS sessions,       (SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = 'max_connections') AS max_connectionsFROM information_schema.processlistGROUP BY commandORDER BY sessions DESC

This query is part of the "MySQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query groups the open sessions in the process list by their command and returns how many sessions each group has. It also returns the max_connections setting on every row, so you can compare the total with the limit.

Run it when the application reports Too many connections, when connection counts creep up over the day, or before you change the pool size in an app.

How to read each column

  • command: what the thread is doing. Sleep is idle and waiting for the client, Query is executing a statement, Binlog Dump is a replica reading the binary log, Connect is a replica connecting.
  • sessions: how many sessions have that command.
  • max_connections: the server limit, the same on every row.

Add the sessions column over all rows to get the total. The query counts itself as one Query session. MySQL also reserves one extra connection above max_connections for an account with the CONNECTION_ADMIN or SUPER privilege.

What is normal and what is not

The default max_connections is 151. Rules of thumb:

  • A total above 80 percent of max_connections leaves little room for a traffic spike.
  • Most sessions in Sleep is common with connection pools, but hundreds of them with few in Query suggest a pool that is too large or an application that does not return connections.
  • Many sessions in Query at once points to a slow statement or a lock, not to a connection problem. Check the long queries list.
  • The count of Binlog Dump should match your number of replicas.

What to do about it

First fix the cause. Reduce the pool size in the application, set sensible idle timeouts and close connections in the code. Raising the limit is the last step, because every connection uses memory.

SET GLOBAL max_connections = 300;

-- Close idle sessions after 5 minutes (new connections only):
SET GLOBAL wait_timeout = 300;

-- The highest number reached since startup:
SHOW GLOBAL STATUS LIKE 'Max_used_connections';

Put the same values in my.cnf to keep them after a restart. Idle sessions can be closed one by one with KILL CONNECTION and the process id.

Limits of this query

  • It is a snapshot. A spike that lasted ten seconds will not show. Max_used_connections gives the peak since startup.
  • It does not say which user or host holds the sessions. Group by user and host to find the one using the most.
  • Without the PROCESS privilege the account sees only its own sessions, and the count will be low.
  • In MySQL 8.0, information_schema.processlist is deprecated in favour of performance_schema.processlist. It still works.

Track it in DataRunner

Schedule the query hourly and keep the results in a Google Sheet through DataRunner. A chart over a few weeks shows whether the count grows with traffic or with a leak. The same result can go by e-mail, Slack, Telegram, Discord, WhatsApp (beta) or a signed webhook.

Frequently asked questions

How do I count connections in MySQL?
Group the process list by command, or run SHOW STATUS LIKE Threads_connected. The query on this page returns the sessions per command and the max_connections limit side by side.
How do I fix Too many connections in MySQL?
Find out who holds the sessions, usually idle ones from an application pool. Reduce the pool, set a lower wait_timeout and fix code that does not close connections. Raise max_connections only after that, with memory in mind.
What is the default max_connections in MySQL?
The default is 151. Each connection uses memory for its buffers, so a very high value can exhaust RAM under load.
Are Sleep connections a problem?
One idle connection costs little. Hundreds of them can fill max_connections and block new clients. They also hide the open transactions that hold locks, so review idle sessions that stay for a long time.