MySQL
show status
database connections
active connections
total connections

MySQL show connections: active (Threads_connected) vs total (Connections)

Codemia Knowledge Hub ·

SHOW STATUS LIKE 'Threads_connected' shows active (currently open) connections. SHOW STATUS LIKE 'Connections' shows the total cumulative count of connection attempts since the server started. For "how many clients are connected right now," use Threads_connected. For "how many connections has this server handled since the last restart," use Connections. Confusing the two is a common monitoring mistake.

Connection status variables

These are the connection-related status variables that matter most:

sql
1SHOW STATUS WHERE Variable_name IN (
2    'Connections',
3    'Threads_connected',
4    'Threads_running',
5    'Max_used_connections',
6    'Aborted_connects',
7    'Aborted_clients'
8);
VariableWhat It MeasuresResets On Restart
ConnectionsTotal connection attempts (successful + failed) since server startYes
Threads_connectedCurrently open connections right nowN/A (live gauge)
Threads_runningConnections actively executing a query right nowN/A (live gauge)
Max_used_connectionsHigh-water mark for simultaneous connections since server startYes
Aborted_connectsFailed connection attempts (bad password, too many connections, etc.)Yes
Aborted_clientsConnections that were closed without proper logoutYes

Threads_connected and Threads_running also differ. A connection can be open but idle, waiting for the application to send a query. Threads_connected counts all open connections (active + idle). Threads_running counts only those actively executing a statement.

Checking current connections

How many clients are connected now

sql
SHOW STATUS LIKE 'Threads_connected';
text
1+-------------------+-------+
2| Variable_name     | Value |
3+-------------------+-------+
4| Threads_connected | 42    |
5+-------------------+-------+

This tells you 42 client connections are currently open. Some may be actively querying, others may be idle.

How many are running queries

sql
SHOW STATUS LIKE 'Threads_running';
text
1+-----------------+-------+
2| Variable_name   | Value |
3+-----------------+-------+
4| Threads_running | 5     |
5+-----------------+-------+

Out of 42 connected clients, only 5 are executing a query at this moment. The other 37 are idle but holding open connections.

What each connection is doing

sql
SHOW PROCESSLIST;

Or for the full query text:

sql
SHOW FULL PROCESSLIST;

This lists every connected thread with its state, current query, and how long it has been in that state. It is the best way to find long-running queries, sleeping connections, and locked threads.

For a more queryable format:

sql
1SELECT id, user, host, db, command, time, state
2FROM information_schema.PROCESSLIST
3WHERE command != 'Sleep'
4ORDER BY time DESC;

This filters out sleeping (idle) connections and sorts by duration, showing the longest-running active queries first.

Connection limits and capacity

Maximum allowed connections

sql
SHOW VARIABLES LIKE 'max_connections';
text
1+-----------------+-------+
2| Variable_name   | Value |
3+-----------------+-------+
4| max_connections | 151   |
5+-----------------+-------+

The default is 151. In production, this is almost always too low.

The high-water mark

sql
SHOW STATUS LIKE 'Max_used_connections';
text
1+----------------------+-------+
2| Variable_name        | Value |
3+----------------------+-------+
4| Max_used_connections | 98    |
5+----------------------+-------+

This tells you the peak number of simultaneous connections since the last server restart. If this value is close to max_connections, you are at risk of hitting the connection limit.

Connection utilization percentage

This shows how close you are to the limit:

sql
1SELECT
2    (SELECT VARIABLE_VALUE FROM performance_schema.global_status
3     WHERE VARIABLE_NAME = 'Max_used_connections') AS peak_connections,
4    (SELECT VARIABLE_VALUE FROM performance_schema.global_variables
5     WHERE VARIABLE_NAME = 'max_connections') AS max_allowed,
6    ROUND(
7        (SELECT VARIABLE_VALUE FROM performance_schema.global_status
8         WHERE VARIABLE_NAME = 'Max_used_connections') /
9        (SELECT VARIABLE_VALUE FROM performance_schema.global_variables
10         WHERE VARIABLE_NAME = 'max_connections') * 100, 1
11    ) AS utilization_pct;

If utilization is consistently above 80%, increase max_connections or implement connection pooling.

Configuring max_connections

At runtime (no restart required)

sql
SET GLOBAL max_connections = 500;

This takes effect immediately but reverts on server restart.

In the configuration file (persistent)

ini
# /etc/mysql/my.cnf or /etc/my.cnf
[mysqld]
max_connections = 500

After editing, restart MySQL or reload the configuration.

Memory per connection

Each connection consumes memory. The rough formula is:

text
Memory per connection = sort_buffer_size + read_buffer_size + join_buffer_size + thread_stack

With default settings, each connection uses roughly 1-10 MB depending on the query. Setting max_connections = 10000 on a server with 4 GB of RAM will eventually cause out-of-memory issues if those connections are all active.

Connection pooling

Instead of raising max_connections indefinitely, use a connection pooler. The application maintains a pool of reusable connections, which keeps the total count manageable.

Example: HikariCP (Java)

java
1HikariConfig config = new HikariConfig();
2config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
3config.setUsername("app_user");
4config.setPassword("secret");
5config.setMaximumPoolSize(20);
6config.setMinimumIdle(5);
7config.setIdleTimeout(300000); // 5 minutes
8
9HikariDataSource ds = new HikariDataSource(config);

Example: Connection Pool in Python

python
1from mysql.connector.pooling import MySQLConnectionPool
2
3pool = MySQLConnectionPool(
4    pool_name="mypool",
5    pool_size=10,
6    host="localhost",
7    database="mydb",
8    user="app_user",
9    password="secret"
10)
11
12conn = pool.get_connection()
13cursor = conn.cursor()
14cursor.execute("SELECT 1")
15cursor.close()
16conn.close()  # Returns to pool, does not actually close

Monitoring connections over time

For ongoing monitoring, record these values periodically:

sql
1-- Snapshot query for monitoring systems
2SELECT
3    NOW() AS sample_time,
4    (SELECT VARIABLE_VALUE FROM performance_schema.global_status
5     WHERE VARIABLE_NAME = 'Threads_connected') AS connected,
6    (SELECT VARIABLE_VALUE FROM performance_schema.global_status
7     WHERE VARIABLE_NAME = 'Threads_running') AS running,
8    (SELECT VARIABLE_VALUE FROM performance_schema.global_status
9     WHERE VARIABLE_NAME = 'Connections') AS total_since_start,
10    (SELECT VARIABLE_VALUE FROM performance_schema.global_status
11     WHERE VARIABLE_NAME = 'Aborted_connects') AS aborted;

Feed this into Grafana, Datadog, or any time-series monitoring system to track connection trends and catch issues before they become outages.

Monitoring mistakes

Confusing Connections with Threads_connected. Connections is a cumulative counter that only increases. Threads_connected is a live gauge. If someone asks "how many connections do we have," they almost certainly mean Threads_connected.

Setting max_connections too high without considering memory. Each connection reserves memory buffers. A server with 1000 active connections and default buffer sizes can consume several gigabytes of RAM for connections alone, leaving insufficient memory for the InnoDB buffer pool.

Not monitoring Aborted_connects. A high rate of aborted connections often indicates authentication failures, connection storms from misbehaving applications, or clients being rejected because max_connections was reached. It is easy to overlook.

Ignoring idle connections. Applications that open connections without closing them inflate Threads_connected without doing useful work. Use wait_timeout to automatically close idle connections after a configurable period (default is 28800 seconds, which is 8 hours).

Using SHOW PROCESSLIST instead of information_schema.PROCESSLIST for programmatic access. SHOW PROCESSLIST truncates query text to 100 characters and is harder to filter. The information_schema version gives you the full query and can be filtered with standard SQL WHERE clauses.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design