在 Debian 上监控 MongoDB,通常可以从 系统层、MongoDB 自身、以及可视化/告警 三个层面来做。下面按“由浅入深”的方式给你一个完整方案。
systemctl status mongod
查看是否运行、是否开机自启:
systemctl is-enabled mongod
ps aux | grep mongod
ss -lntp | grep 27017
mongostat --host 127.0.0.1 --port 27017
常用字段说明:
insert/query/update/delete:操作频率flushes:刷盘次数vsize/res:内存使用qr|qw:队列长度(重要)✅ qr/qw 长期 > 0 说明有性能瓶颈
mongotop --host 127.0.0.1
可看到:
mongosh
db.serverStatus()
db.stats()
db.currentOp()
重点关注:
connectionsopcountersglobalLockmemorytop
htop
iotop
MongoDB 是 内存+磁盘密集型,重点看:
MongoDB 很容易吃满磁盘:
df -h
du -sh /var/lib/mongodb
建议:
/var/log/mongodb/mongod.log
实时查看:
tail -f /var/log/mongodb/mongod.log
常见关注点:
slow queryconnection refusedpage faultWT_CACHE编辑配置文件:
/etc/mongod.conf
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
重启:
systemctl restart mongod
查看慢查询:
db.system.profile.find().sort({ts:-1}).limit(5)
MongoDB → mongodb_exporter → Prometheus → Grafana
wget https://github.com/percona/mongodb_exporter/releases/download/v0.40.0/mongodb_exporter-0.40.0.linux-amd64.tar.gz
tar -xzf mongodb_exporter-*.tar.gz
运行:
./mongodb_exporter --mongodb.uri="mongodb://localhost:27017"
Prometheus 拉取端口:
http://localhost:9216/metrics
✅ Grafana 官方有现成 MongoDB Dashboard
至少监控以下指标:
| 场景 | 推荐 |
|---|---|
| 单机 / 小项目 | mongostat + 日志 |
| 生产环境 | Prometheus + Grafana |
| 企业级 | PMM |
| 云环境 | Atlas / 云监控 |
你可以告诉我:
我可以直接给你 一份可用的 Prometheus 配置 + Grafana Dashboard ID。