温馨提示×

MongoDB性能监控在Ubuntu上怎么做

小樊
40
2025-11-25 00:56:41
栏目: 云计算

Ubuntu上MongoDB性能监控实操指南

一 快速上手的内置与命令行工具

  • 使用 mongostat 实时查看关键吞吐与锁状态:
    • 命令示例:mongostat --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
    • 关注字段:insert/query/update/delete(操作速率)、getmore(游标批量取)、command(命令执行)、flushes(落盘频率)、mapped(内存映射)、faults(缺页)、locked(锁占比)、qr/qw(队列读写)、ar/aw(活跃会话)。
  • 使用 mongotop 跟踪集合级别的读写耗时:
    • 命令示例:mongotop --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
    • 关注点:按 ns(库.集合)维度查看 read/write 时间占比,快速定位热点集合。
  • 在 mongo shell 中查看全局与集合级指标:
    • 全局状态:db.serverStatus()(返回 connections、mem、metrics、opcounters、wiredTiger 等大量指标)
    • 集合统计:db.collection.stats()(返回 count、size、storageSize、totalIndexSize、indexSizes 等)
    • 查询诊断:db.collection.find({…}).explain(“executionStats”)(查看 executionStats.executionTimeMillis、totalKeysExamined、totalDocsExamined、stage 等)

二 图形化与托管监控

  • MongoDB Compass(官方 GUI):
    • 安装示例:wget https://downloads.mongodb.com/compass/mongodb-compass_1.26.0amd64.deb && sudo dpkg -i mongodb-compass1.26.0_amd64.deb
    • 连接实例后可直观查看 性能指标、索引、查询分析 等,适合开发/运维日常巡检。
  • MongoDB Cloud Manager(官方托管监控与备份):
    • 注册账户后添加实例,使用 仪表板与告警 进行持续监控与容量规划。

三 自建可观测性平台 Prometheus Grafana

  • 组件与数据流:MongoDB Exporter → Prometheus(抓取/存储) → Grafana(可视化/告警)
  • 安装与启动(示例):
    • Prometheus(下载解压后启动):./prometheus --config.file=prometheus.yml
    • Grafana(Ubuntu APT 源安装):sudo apt update && sudo apt install -y grafana && sudo systemctl start grafana-server && sudo systemctl enable grafana-server
  • 配置抓取(prometheus.yml 片段):
    • scrape_configs:
      • job_name: ‘mongodb’ static_configs:
        • targets: [‘<mongodb_host>:<exporter_port>’]
  • Grafana 侧:添加 Prometheus 数据源,导入 MongoDB 社区仪表盘(如 ID 2583),即可查看 连接数、操作速率、锁、WiredTiger 缓存命中、复制延迟 等。

四 关键指标与告警建议

  • 连接与队列:监控 connections.current/availableqr/qw,持续接近上限或队列增长可能预示阻塞或并发不足。
  • 操作与错误:关注 opcounters.insert/query/update/delete 的突增与 asserts.regular/warning 异常。
  • 内存与页面错误:观察 mem.resident、mem.virtual、faults,缺页升高常提示工作集超过内存或索引/数据未充分缓存。
  • 落盘与 WiredTiger:关注 flushes(落盘次数)与 wiredTiger.cache.*(bytes currently in cache、pages read/write),评估缓存命中与 I/O 压力。
  • 复制健康:副本集关注 replSetGetStatus 中的 optimeDate、health、lastHeartbeat,设置 复制延迟阈值告警
  • 慢查询:开启并分析 profiling,结合 db.currentOp()explain(“executionStats”) 定位高成本查询与缺失/低效索引。

五 安全与连通性配置要点

  • 启用身份验证:在 /etc/mongod.conf 中设置 security.authorization: enabled,并使用具备相应角色的用户进行监控采集。
  • 网络与防火墙:如需远程采集,配置 net.bindIp(如 0.0.0.0 或白名单),并放行 27017 端口;仅开放必要来源 IP。
  • 最小权限采集:为监控专用账户授予只读/监控所需角色,避免使用高权限账户进行日常采集。

0