MySQL
Database Management
SQL Queries
Connection Info
Database Administration

MySQL show current connection info

System Design practice on Codemia

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

Practice system design

Introduction

When people ask for “current connection info” in MySQL, they may mean one of two things: details about the session they are currently using, or a list of active connections on the server. MySQL supports both, and the right command depends on whether you are debugging your own session or monitoring the whole instance.

Get Information About the Current Session

For your own connection, a few built-in functions are often enough.

sql
SELECT CONNECTION_ID(), CURRENT_USER(), USER(), DATABASE();

This returns useful session details:

  • 'CONNECTION_ID() is the thread or session id'
  • 'CURRENT_USER() is the authenticated MySQL account'
  • 'USER() shows the client-provided account and host string'
  • 'DATABASE() shows the currently selected database'

If you are debugging permissions, the difference between CURRENT_USER() and USER() is especially important because authentication and account matching may not be what you assume.

Show Active Connections with SHOW PROCESSLIST

If you want to inspect current sessions on the server, use SHOW PROCESSLIST.

sql
SHOW PROCESSLIST;

Or, if you want the full query text rather than a truncated version:

sql
SHOW FULL PROCESSLIST;

This output includes columns such as:

  • 'Id for the connection id'
  • 'User for the MySQL account'
  • 'Host for the client source'
  • 'db for the selected database'
  • 'Command for the session activity such as Sleep or Query'
  • 'Time for how long the session has been in that state'
  • 'State for the current stage of execution'
  • 'Info for the SQL text'

That is usually the fastest way to see whether the server is busy, blocked, or full of idle connections.

Query Connection Data as a Table

If you prefer filtering and ordering like a normal query, use INFORMATION_SCHEMA.PROCESSLIST.

sql
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO
FROM INFORMATION_SCHEMA.PROCESSLIST
ORDER BY TIME DESC;

This is useful when you want to find long-running sessions or isolate specific users.

For example, long-running queries can be filtered like this:

sql
1SELECT ID, USER, HOST, DB, TIME, INFO
2FROM INFORMATION_SCHEMA.PROCESSLIST
3WHERE COMMAND != 'Sleep'
4  AND TIME > 10
5ORDER BY TIME DESC;

Check Connection Counts and Limits

Sometimes the real question is not “who is connected?” but “how many connections are in use right now?”

These status variables help:

sql
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Threads_running';
SHOW VARIABLES LIKE 'max_connections';

Threads_connected shows current open connections. Threads_running shows how many threads are actively doing work. max_connections shows the configured limit.

Those three values are useful when you are diagnosing connection pool issues or server saturation.

Identify Your Current Session in the Process List

You can combine session and server views by filtering the process list with your own connection id.

sql
SELECT *
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE ID = CONNECTION_ID();

That gives you the process-list row for the session you are using right now.

Common Pitfalls

A common mistake is using SHOW PROCESSLIST when the real need is only your own connection details. In that case, CONNECTION_ID() and related functions are simpler.

Another pitfall is assuming USER() and CURRENT_USER() mean the same thing. They often match, but in authentication debugging they can differ.

Developers also sometimes focus only on Threads_connected and forget Threads_running, which is more informative when the server appears slow.

Finally, SHOW PROCESSLIST may require privileges to see all sessions. If the output looks incomplete, permissions may be the reason.

Summary

  • Use CONNECTION_ID(), CURRENT_USER(), USER(), and DATABASE() for your current session.
  • Use SHOW PROCESSLIST or SHOW FULL PROCESSLIST to inspect active server connections.
  • Query INFORMATION_SCHEMA.PROCESSLIST when you need filtering or ordering.
  • Check Threads_connected, Threads_running, and max_connections for connection health.
  • Filter the process list by CONNECTION_ID() if you want the server-side row for your own session.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

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

Practice system design

All Rights Reserved.