Skip to content

Monitoring guide

MySQL monitoring: key metrics and alerts

MySQL problems usually show up as slow queries, full connection limits or replicas falling behind. A handful of status variables and the slow log cover most of it.

4 min read14 sectionsUpdated

On this page

Key points

  • Threads_connected vs max_connections prevents error 1040.
  • The slow query log and performance_schema find expensive statements.
  • InnoDB buffer pool hit rate shows whether data fits in memory.
  • Replication lag decides how stale replicas are.

Core status checks

sql
SHOW GLOBAL STATUS WHERE Variable_name IN
  ('Threads_connected','Threads_running','Questions','Slow_queries',
   'Innodb_buffer_pool_reads','Innodb_buffer_pool_read_requests','Aborted_connects');

-- replication (8.0.22+)
SHOW REPLICA STATUS\G      -- Seconds_Behind_Source, Replica_IO_Running, Replica_SQL_Running

What the numbers mean

MetricHealthyWorry when
Threads_connected / max_connectionsComfortable headroomAbove 80%
Threads_runningLow single or double digitsSpikes: queries piling up
Buffer pool hit rate = 1 − reads ÷ read_requestsAbove 99% for OLTPDropping: working set does not fit
Slow_queries rateLow and stableRising after a deploy
Seconds_Behind_SourceNear 0Above your RPO, or NULL (replication stopped)

Finding expensive queries

sql
-- enable the slow log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 0.5;

-- top statements by total time (performance_schema)
SELECT DIGEST_TEXT, COUNT_STAR, ROUND(SUM_TIMER_WAIT/1e12, 1) AS total_s, ROUND(AVG_TIMER_WAIT/1e9, 1) AS avg_ms
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC LIMIT 10;

Collecting metrics continuously

The OpenTelemetry Collector's mysql receiver reads these status variables on a schedule (use a read-only user):

otel-collector.yamlyaml
receivers:
  mysql:
    endpoint: db:3306
    username: otel_monitor
    password: ${env:MYSQL_MONITOR_PASSWORD}
    collection_interval: 60s
exporters:
  otlphttp/vitalrix:
    encoding: json
    metrics_endpoint: ${env:VITALRIX_API}/api/v1/metrics/otlp/v1/metrics
    headers: { x-api-key: ${env:VITALRIX_KEY} }
service:
  pipelines:
    metrics: { receivers: [mysql], exporters: [otlphttp/vitalrix] }

MySQL monitoring with VitalRix

Collector metrics arrive over OTLP and can be charted on dashboards and used in alert rules. A TCP check confirms MySQL accepts connections from the network your app uses, and APM traces show which requests wait on MySQL and for how long, from the client spans your instrumented services create. See also MySQL too many connections and finding slow database queries.

Alerts to start with

  • Threads_connected above 80% of max_connections for 5 minutes.
  • Threads_running above a level you rarely see (for example 30) for 5 minutes.
  • Replication: Seconds_Behind_Source above your RPO, or replication threads not running.
  • Disk used above 80% on the data volume.
  • MySQL not answering a TCP check from the application network.
  • Database time per request rising in APM traces.

Replicas and read traffic

If read replicas serve application traffic, replication lag becomes user-visible: a user saves something and does not see it on the next page. Route read-your-own-writes requests to the primary, and alert when lag passes a few seconds, not only when replication stops.

The MySQL metrics that matter

MetricSourceStarting alert
Threads_connected vs max_connectionsSHOW GLOBAL STATUS / VARIABLESAbove 80%
Threads_runningSHOW GLOBAL STATUSSustained above the number of CPU cores
Slow queriesSlow query log (long_query_time)A rising rate after a deploy
Replication lagSeconds_Behind_Source in SHOW REPLICA STATUSAbove 30 seconds, or NULL (replication stopped)
InnoDB buffer pool hit ratio1 - Innodb_buffer_pool_reads / Innodb_buffer_pool_read_requestsBelow 99% on a hot database
Aborted connectsAborted_connectsA spike: wrong credentials or a network problem
DeadlocksSHOW ENGINE INNODB STATUS, or innodb_print_all_deadlocksAny repeated pattern
sql
-- turn on the slow query log without a restart
SET GLOBAL slow_query_log = ON;
SET GLOBAL long_query_time = 1;
-- what is running right now, longest first
SELECT id, user, time, state, left(info, 80) FROM information_schema.processlist WHERE command <> 'Sleep' ORDER BY time DESC LIMIT 10;
-- replication health (MySQL 8.0.22+; older versions: SHOW SLAVE STATUS)
SHOW REPLICA STATUS\G

What the incidents look like

  • "Too many connections" when application pools grow faster than max_connections: see MySQL too many connections.
  • Threads_running spikes with a pile of identical queries: usually a missing index or a cache that expired and sent every request to the database at once.
  • Replication stops after an error on the replica, and Seconds_Behind_Source becomes NULL; an alert on "lag above 30 s" alone never fires. Alert on NULL too.
  • Disk fills with binary logs when binlog_expire_logs_seconds is unset on an old server.

Pair database metrics with APM: a trace shows which endpoint sends the slow query, so the fix lands in the right code.

Frequently asked questions

What is a good buffer pool size?

On a dedicated database server, often 50 to 75% of RAM, so the working set fits. Check the hit rate rather than guessing.

Does monitoring add load to MySQL?

Reading status variables every minute is negligible. Long-running performance_schema queries should run off-peak.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.