在 Debian 上监控 MongoDB,通常可以从 系统层、MongoDB 自身、以及可视化/告警 三个层面来做。下面按“由浅入深”的方式给你一个完整方案。
一、基础:确认 MongoDB 运行状态
1️⃣ systemd 服务状态
systemctl status mongod
查看是否运行、是否开机自启:
systemctl is-enabled mongod
2️⃣ 进程与端口
ps aux | grep mongod
ss -lntp | grep 27017
二、使用 MongoDB 自带监控命令(最重要)
1️⃣ mongostat(实时性能)
mongostat --host 127.0.0.1 --port 27017
常用字段说明:
-
insert/query/update/delete:操作频率 -
flushes:刷盘次数 -
vsize/res:内存使用 -
qr|qw:队列长度(重要)
✅ qr/qw 长期 > 0 说明有性能瓶颈
2️⃣ mongotop(集合级耗时)
mongotop --host 127.0.0.1
可看到:
- 哪个集合读/写最频繁
- 是否存在热点集合
3️⃣ mongo shell 内置命令
mongosh
db.serverStatus()
db.stats()
db.currentOp()
重点关注:
connectionsopcountersglobalLockmemory
三、系统级监控(Debian)
1️⃣ CPU / 内存 / IO
top
htop
iotop
MongoDB 是 内存+磁盘密集型,重点看:
- 内存是否接近耗尽
- IO wait 是否过高
2️⃣ 磁盘空间(非常重要)
MongoDB 很容易吃满磁盘:
df -h
du -sh /var/lib/mongodb
建议:
- 磁盘使用率 < 80%
- 单独挂载数据盘
四、日志监控(故障排查关键)
MongoDB 日志位置
/var/log/mongodb/mongod.log
实时查看:
tail -f /var/log/mongodb/mongod.log
常见关注点:
slow queryconnection refusedpage faultWT_CACHE
五、开启 MongoDB 慢查询日志(强烈建议)
编辑配置文件:
/etc/mongod.conf
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
重启:
systemctl restart mongod
查看慢查询:
db.system.profile.find().sort({ts:-1}).limit(5)
六、生产级监控(推荐)
✅ 方案 1:MongoDB Exporter + Prometheus + Grafana(最常用)
架构
MongoDB → mongodb_exporter → Prometheus → Grafana
安装 mongodb_exporter
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
✅ 方案 2:Percona Monitoring(企业级)
- 基于 PMM
- 支持 MongoDB / MySQL / PostgreSQL
- 图形化、告警完善
七、告警建议(必须)
至少监控以下指标:
- ✅ MongoDB 服务是否存活
- ✅ 磁盘使用率 > 80%
- ✅ 连接数接近 maxIncomingConnections
- ✅ 慢查询数量突增
- ✅ 复制集状态异常(如果用副本集)
八、常见监控组合推荐
| 场景 | 推荐 |
|---|---|
| 单机 / 小项目 | mongostat + 日志 |
| 生产环境 | Prometheus + Grafana |
| 企业级 | PMM |
| 云环境 | Atlas / 云监控 |
九、我可以继续帮你
你可以告诉我:
- MongoDB 是 单机 / 副本集 / 分片?
- Debian 版本(10 / 11 / 12)?
- 是否可接受 Prometheus?
- 是否已有 Grafana?
我可以直接给你 一份可用的 Prometheus 配置 + Grafana Dashboard ID。