Skip to main content

How to See How Many Connections MySQL Has Now and at Its Peak

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Three questions tell you if MySQL is near its connection limit: how many now, how many at the worst moment, and who has them. Each is one statement.

Step 1: How Many Now​

mysql>
SHOW GLOBAL STATUS LIKE 'Threads_connected';
Output
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 3 |
+-------------------+-------+
1 row in set (0.004 sec)

Your own session is one of those.

Step 2: The Peak Since MySQL Started​

mysql>
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
SHOW GLOBAL STATUS LIKE 'Max_used_connections_time';
Output
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 4 |
+----------------------+-------+
1 row in set (0.001 sec)

+---------------------------+---------------------+
| Variable_name | Value |
+---------------------------+---------------------+
| Max_used_connections_time | 2026-11-06 06:20:40 |
+---------------------------+---------------------+
1 row in set (0.002 sec)

This is the number that matters most, because a burst may have come and gone. To start counting again, FLUSH STATUS resets it to however many are connected now.

Step 3: The Limit​

mysql>
SHOW VARIABLES LIKE 'max_connections';
Output
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.003 sec)

If Max_used_connections is close to this, you've come close to ERROR 1040. How to Change max_connections in MySQL and Keep It After a Restart raises it, if the server has the memory.

Step 4: Who Has Them​

As an administrator:

mysql>
SELECT PROCESSLIST_USER AS user, PROCESSLIST_HOST AS host, COUNT(*) AS connections
FROM performance_schema.threads
WHERE PROCESSLIST_USER IS NOT NULL
GROUP BY PROCESSLIST_USER, PROCESSLIST_HOST
ORDER BY connections DESC;
Output
+-----------------+-----------+-------------+
| user | host | connections |
+-----------------+-----------+-------------+
| inventory_app | localhost | 2 |
| event_scheduler | localhost | 1 |
| root | localhost | 1 |
+-----------------+-----------+-------------+
3 rows in set (0.003 sec)

Threads_connected, Max_used_connections, max_connections of 151, and a table of connections by user and host.

event_scheduler is one of MySQL's own background threads, and doesn't count against the limit.

Reading It with a Connection Pool​

A pool keeps connections open between requests, so a high number isn't a sign of trouble by itself. Here's a .NET API after 50 requests at once, with MySQL's count from the root session:

Output
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 51 |
+-------------------+-------+
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 51 |
+----------------------+-------+

Fifty connections, all idle. They leave the pool about three minutes later, as How Long Does MySql.Data Keep Idle Connections in the Pool? measures. To see what they're doing, PROCESSLIST_COMMAND in that same table says Sleep or Query.

Common Mistakes​

MistakeWhat happensDo this instead
Checking Threads_connected onlyYou miss the burst at 3 a.m.Check Max_used_connections too
Using SHOW PROCESSLISTMySQL 9.7 warns that it's deprecatedUse performance_schema
Reading Threads_running as connectionsThat's only the busy onesUse Threads_connected

Troubleshooting​

SymptomLikely causeFix
Lots of Sleep connections from one hostA pool doing its job, or a large oneSize it
ERROR 1142 on performance_schema.threadsYour account can't read itUse an administrator
Max_used_connections is one above max_connectionsAn administrator used the spare connectionThat's expected

Conclusion​

Threads_connected for now, Max_used_connections for the peak, max_connections for the limit, and performance_schema.threads for who has them.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.