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