MongoDB Mastery
MongoDB Mastery lessons / Module 28: Monitoring & Diagnostics

Monitoring with Percona Monitoring and Management (PMM)

Level 4: Database Engineer20 min

Deploy PMM Server and agents, add MongoDB services with the right user, and use the dashboards and Query Analytics to find bottlenecks.

This lesson is interactive. Run every example against an in-browser MongoDB, see explain() plans drawn as trees, and take the quiz.
Open the interactive lesson
You will learn to
  • Describe PMM Server and PMM Client components
  • Create the monitoring user and enable a query source
  • Read the key dashboards
  • Use Query Analytics to rank query shapes

conceptThe concept

PMM is an open-source monitoring platform. PMM Server (usually a container) stores metrics and runs Grafana dashboards, alerting and Query Analytics (QAN). PMM Client (pmm-agent plus exporters) runs on each database host and ships metrics and query samples.

For MongoDB, pmm-admin add mongodb registers a service; metrics come from the MongoDB exporter, and QAN reads the profiler (--query-source=profiler) or slow-query logs. Typical dashboards: instances overview and summary, replica set summary (lag, elections, oplog window), WiredTiger (cache, tickets, eviction), sharded cluster summary, and node-level CPU, disk and memory.

whyWhy it matters

Self-hosted MongoDB needs the same visibility that a managed service offers: per-query-shape latency, replication health and storage engine internals in one place. PMM provides that without licence costs and integrates with the Percona Operator.

Use caseReal-world scenario

Latency rises every day at 09:00. QAN ranks query shapes by total time: one find on orders by status and createdAt dominates, with a high documents-examined-to-returned ratio. Its example query plus explain confirms a blocking sort; an ESR index fixes it, and the WiredTiger dashboard shows application-thread eviction dropping back to zero.

Try itExamples

Run and edit these in the interactive lesson.

Monitoring user and role
use admin
try { db.dropUser("pmm") } catch (e) {}
try { db.dropRole("explainRole") } catch (e) {}
db.createRole({
  role: "explainRole",
  privileges: [{ resource: { db: "", collection: "" }, actions: ["dbHash", "find", "listIndexes", "listCollections", "collStats", "dbStats", "indexStats"] },
               { resource: { db: "", collection: "system.version" }, actions: ["find"] }],
  roles: []
})
db.createUser({ user: "pmm", pwd: "change-me", roles: [{ role: "explainRole", db: "admin" }, { role: "clusterMonitor", db: "admin" }, { role: "read", db: "local" }] })
db.getUser("pmm").roles.map(r => r.role + "@" + r.db)
Register the service
# on the database host, after installing pmm-client
pmm-admin config --server-insecure-tls --server-url=https://admin:****@pmm.internal:443
pmm-admin add mongodb --username=pmm --password=**** \
  --host=127.0.0.1 --port=27017 --cluster=prod-rs0 \
  --replication-set=rs0 --environment=prod \
  --query-source=profiler --service-name=prod-rs0-node1
pmm-admin list
Enable a sampled profiler for QAN
db.setProfilingLevel(1, { slowms: 50, sampleRate: 0.5 })
db.getProfilingStatus()

internalsUnder the hood

The profiler query source adds load proportional to profiled operations, so keep slowms or the sample rate sensible on busy clusters. Dashboards are only as good as the scrape interval; for second-level incidents, combine PMM with FTDC data. The monitoring user needs clusterMonitor, read access to local for oplog metrics, and a small custom role for collection and index statistics.

Avoid / preferDo this, not that

Avoid
pmm-admin add mongodb --username=root --password=...
Prefer
pmm-admin add mongodb --username=pmm --password="$PMM_PW" --query-source=profiler

Monitoring needs read access to diagnostics, not root. A dedicated user with clusterMonitor (plus the reads Query Analytics needs) limits damage if the agent host is compromised.

Avoid
db.setProfilingLevel(2)   // profile everything for Query Analytics
Prefer
db.setProfilingLevel(1, { slowms: 100 })

Level 2 records every operation and adds measurable write overhead. Level 1 with a sensible threshold captures the queries worth fixing.

PitfallsCommon mistakes

  • Profiling every operation (level 2) on a busy production cluster to feed QAN.
  • Monitoring only the primary; lag and eviction problems often start on secondaries.
  • Giving the monitoring user root or readWriteAnyDatabase instead of a minimal role.

LimitsWhen not to use it

PMM is a monitoring and query analytics stack; it does not replace alert routing and on-call tooling, or capacity planning on its own. Keep its retention and scrape intervals reasonable: second-level scrapes across many nodes make PMM itself the bottleneck.

SecuritySecurity notes

Give the PMM user only monitoring roles, serve the PMM server over HTTPS behind SSO, and change the default admin password at install. Query Analytics shows real query text and examples, which can contain customer data: restrict who can open QAN.

ChallengeTry it yourself

Prepare this node for PMM Query Analytics: enable the profiler for operations over 100 ms and create a pmm_lab user with clusterMonitor and read on local. Show the profiling status and the user's roles.

Show one solution
One solution
db.setProfilingLevel(1, { slowms: 100 })
const admin = db.getSiblingDB("admin")
try { admin.dropUser("pmm_lab") } catch (e) {}
admin.createUser({ user: "pmm_lab", pwd: "change-me", roles: [{ role: "clusterMonitor", db: "admin" }, { role: "read", db: "local" }] })
printjson({ profile: db.getProfilingStatus(), roles: admin.getUser("pmm_lab").roles.map(r => r.role) })
In production use passwordPrompt() or a secret store instead of a literal password.

InterviewQuestions you should be able to answer

How would you set up monitoring for a self-hosted MongoDB fleet?

Deploy PMM Server (or Prometheus plus Grafana with the MongoDB exporter), install agents on every node, create a least-privilege monitoring user, enable a sampled profiler or log-based query source for Query Analytics, and alert on availability, replication lag versus oplog window, cache eviction, tickets, disk latency and free space, and backup freshness. Keep FTDC for second-by-second post-mortems.

QuizCheck your understanding

4 questions with instant feedback in the interactive lesson; a wrong answer goes into your review deck.