Database Administration — Tue Apr 07

← Home | ← database-admin

MySQL - DBA Toolkit - Active Sessions and Threads

Tue Apr 07 — See every active session and thread at a glance — who is running what, for how long, and where your connection headroom sits.
#mysql #dba #monitoring #tool-kit

Active Sessions: Know Who Is In Your Database Right Now

Connection count is a lagging indicator. By the time an alert fires, you are already in trouble.

The better habit is checking active sessions as part of your routine — before deployments, during slow periods, whenever something feels off. What queries are running? Who has been sitting in a transaction for three minutes? Which application server is holding ten connections open and doing nothing?

This script answers all of that in one pass.

ActiveSessionsandThreads.sql
    Loading…
  

Reading the Results

Active Queries

ColumnWhat to Check
userWhich app user or service owns the connection
hostSource IP or hostname — useful for tracing back to an app server
dbWhich database the session is attached to
commandQuery = actively running; Sleep = idle (filtered out above)
secondsHow long the current command has been running
stateInternal execution stage — e.g. Sending data, Waiting for lock
queryThe actual SQL being executed (may be truncated)

Thread and Connection Metrics

VariableWhat It Tells You
Threads_connectedTotal open connections right now
Threads_runningConnections actively executing — the one that matters under load
Threads_cachedConnections held in the thread cache for reuse
Threads_createdCumulative thread creates — high values mean cache is too small
Max_used_connectionsPeak concurrent connections since last restart
max_connectionsThe configured ceiling — compare against Max_used_connections

What to Watch For

SignalWhat It Means
Long-running queries (high seconds)Slow query, missing index, or a stuck transaction
state = Waiting for lockA blocking transaction is holding a lock
Threads_running close to Threads_connectedVery few idle connections — server is under pressure
Max_used_connections near max_connectionsYou are close to the connection ceiling — risk of refused connections
Many connections from one hostApplication connection pool misconfigured or leaking
command = KilledA query was explicitly killed — worth investigating why

Gareth Winterman