How to monitor
How to monitor MongoDB
MongoDB backs a lot of apps — when it's unreachable or can't serve queries, everything above it fails. Here's how to monitor MongoDB reachability and health without exposing it.
MongoDB usually sits behind your app serving reads and writes. When it's unreachable, slow, or has lost its primary, the symptoms surface everywhere — failed requests, timeouts, stuck features — and it isn't always obvious the database is the cause. A direct check gives you a clear signal.
Here's how to monitor MongoDB reliably, without exposing it to the internet.
Two solid approaches
1. TCP reachability check (quick baseline)
A TCP check on MongoDB's port (default 27017) confirms it's reachable and accepting connections. This is useful when the database is on a publicly-reachable host. If MongoDB is private (as it usually should be), an external check can't reach it — use the heartbeat approach below instead.
2. Command heartbeat (stronger)
The best signal is a job that actually talks to MongoDB and reports health:
# Runs on a schedule inside your network
mongosh "$MONGO_URI" --quiet --eval 'db.runCommand({ ping: 1 }).ok' | grep -q 1 \
&& curl -fsS -m 10 https://statuscat.co/ping/your-mongo-heartbeat-id > /dev/null
- The
pingcommand proves the server can respond to queries. - The
&&means the ping only fires on success — so a failure becomes a missing heartbeat, and StatusCat alerts you. - Nothing is exposed publicly; the job reaches out.
For a replica set, connect to the set and confirm a primary is reachable — the heartbeat will stop if quorum is lost.
What else to watch
- The app's use of MongoDB — a
/healthendpoint that runs a cheap query will fail if the database is unhealthy (how to monitor an API). - The database host — a TCP/ICMP check catches infrastructure-level failures.
- Backups — a heartbeat on your backup job so a silent failure doesn't go unnoticed.
- Replication & disk — emit these from inside and let a failing heartbeat page you when a threshold is crossed.
Put it together
A solid MongoDB setup is usually a command heartbeat ("can it serve queries / is there a primary?"), a TCP/host check ("is it reachable?"), and a backup heartbeat ("are we protected?"). Wire alerts and escalation so the right person hears about it fast.
StatusCat supports TCP, ICMP and heartbeat monitors alongside HTTP/DNS/SSL checks, free for 50 monitors. New to monitoring? Start with what uptime monitoring is.