温馨提示×

怎样监控debian mongodb

小樊
36
2026-09-26 15:48:32
栏目: 云计算

在 Debian 上监控 MongoDB,可以从系统层、MongoDB 自带工具、第三方监控三个层面来做。下面按实用程度给你一个清晰方案。


一、MongoDB 自带监控(最基础)

1. mongosh / mongo 命令行

mongosh

进入后常用命令:

db.serverStatus()      # 整体状态(连接、内存、锁、操作)
db.stats()             # 当前库状态
db.currentOp()         # 当前正在执行的操作
db.runCommand({ top: 1 })  # 集合读写统计

2. MongoDB 日志

日志位置(常见):

/var/log/mongodb/mongod.log

实时查看:

tail -f /var/log/mongodb/mongod.log

二、系统级监控(Debian)

3. 基础命令

top / htop
iostat -x 1
df -h
free -m

4. systemd 状态

systemctl status mongod
journalctl -u mongod -f

三、内置 Web / 指标接口(推荐)

5. MongoDB Exporter + Prometheus(生产推荐)

安装 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 抓取

scrape_configs:
  - job_name: mongodb
    static_configs:
      - targets: ['localhost:9216']

Grafana 面板

  • 导入 Grafana MongoDB Dashboard(ID: 12079)

四、图形化 / 商业方案

6. MongoDB Atlas / Ops Manager

  • Atlas:云托管,自带监控
  • Ops Manager:企业版,适合自建集群

7. 轻量 GUI

  • MongoDB Compass(官方)
  • Studio 3T

五、关键监控指标(重点看这些)

  • 连接数:connections.current
  • 内存:mem.resident
  • 慢查询:profiler
  • 复制延迟(副本集):replication.lag
  • 锁等待:locks
  • 磁盘 IO

六、慢查询监控(非常重要)

db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.find().sort({ ts: -1 }).limit(10)

推荐组合(实用)

✅ 开发:mongosh + 日志
✅ 生产:Prometheus + Grafana + Exporter
✅ 企业:Ops Manager

如果你告诉我:

  • 是 单机 / 副本集 / 分片
  • Debian 版本
  • 是否允许装 Prometheus

我可以直接给你一套可复制的监控配置。

0 踩