Back to Directory
How to Monitor Recent

How to Monitor a MySQL Database & Check Connection Health

SAuthor

How to Monitor a MySQL Database Uptime & Connection Health

MySQL is a widely used relational database management system. If your MySQL server runs out of file descriptors, hits connection limits (max_connections), or runs out of disk storage space, your web app will return connection errors and crashes.

Since MySQL servers are isolated inside secure private database networks behind firewalls, you cannot probe them directly from the public internet. This guide explains how to monitor MySQL database health securely and set up alerts before outages impact users.


Method 1: Set Up an Application-Level Database Health Endpoint

The most secure approach to check a private MySQL instance is to execute a cheap query check inside a backend application health endpoint.

Node.js (Express & mysql2) Health Route Example

Expose a health checker endpoint in your Node app:

const express = require('express');
const mysql = require('mysql2/promise');
const app = express();

const pool = mysql.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0,
  connectTimeout: 2000 // Fail fast if MySQL hangs
});

app.get('/api/healthz/db', async (req, res) => {
  try {
    // Execute a fast, low-cost query check to verify connection
    const [rows] = await pool.query('SELECT 1');
    res.status(200).json({ status: 'OK', database: 'connected' });
  } catch (err) {
    console.error('MySQL database health check failed:', err);
    res.status(500).json({ status: 'ERROR', message: 'Database connection failed' });
  }
});

app.listen(3000);

Configure Pingzo to Probe the MySQL Health Endpoint

  1. Log in to your Pingzo Dashboard.
  2. Create a new HTTP/HTTPS Monitor pointing to your endpoint (e.g. https://your-api.com/api/healthz/db).
  3. Set checking intervals to 1 minute and set up WhatsApp Alerts to page your engineering team if the API returns an HTTP 500 error code.

Method 2: Push Monitoring (Heartbeat) for MySQL Backups

If you run background mysqldump jobs to back up tables, use Heartbeat (Push) Monitoring in Pingzo:

  • Configure a Heartbeat Monitor on Pingzo.
  • Add a curl ping at the end of your dump backup script:
#!/bin/bash
mysqldump -u dbuser -p"dbpassword" dbname > backup.sql && \
curl -fsS https://ping.pingzoapp.com/ping/your-unique-heartbeat-id

If the database is locked, the backup script fails, the curl webhook is skipped, and Pingzo alerts you.


Key MySQL Metrics to Monitor

Track these internal metrics in your server dashboard:

  • Threads_connected: Current count of active client connections. Alert if this approaches max_connections.
  • Slow_queries: The count of queries taking longer than long_query_time seconds. Spike indicates indexing bottlenecks.
  • Aborted_connects: The number of failed attempts to connect to MySQL. High rates indicate network issues or authentication leaks.

Summarize with AI

Instantly generate a summary of this page using your favorite LLM

pz-console
// MySQL CLI Connection
$mysqladmin -u root -p ping
// mysql2 Query Check
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection(process.env.DATABASE_URL);
const [rows] = await connection.query('SELECT 1');
[pingzo-agent] monitoring active: MySQL
[pingzo-agent] status: 200 OK | latency: 85ms
DevOps Uptime Check

Uptime monitoring built for MySQL developers

Configure HTTP health routes, inspect SSL status, and track cron workers from our console dashboard. Get direct texts on WhatsApp.

Start Monitoring Free