首页主机资讯MongoDB在Ubuntu上的监控与告警设置

MongoDB在Ubuntu上的监控与告警设置

时间2025-11-21 12:28:03发布访客分类主机资讯浏览465
导读:Ubuntu上MongoDB监控与告警实操指南 一 监控方案总览 原生与轻量:使用 MongoDB 自带的 mongostat、mongotop 快速查看连接、操作、锁、读写热点;配合 db.serverStatus( 、db.colle...

Ubuntu上MongoDB监控与告警实操指南

一 监控方案总览

  • 原生与轻量:使用 MongoDB 自带的 mongostatmongotop 快速查看连接、操作、锁、读写热点;配合 db.serverStatus()db.collection.stats() 做细粒度诊断;图形化可用 MongoDB Compass 观察性能指标与查询执行计划。适合单机或快速排障。
  • 企业级与托管:使用 MongoDB Cloud Manager 获得全栈监控、备份与自动化运维能力,适合生产环境托管与合规需求。
  • 开源可观测栈:以 Prometheus + mongodb_exporter + Grafana 构建指标采集、存储、可视化与告警体系,适合自建平台与深度定制。
  • 传统监控平台:使用 Zabbix 通过自定义键值或模板采集 MongoDB 指标并做阈值告警,适合已有 Zabbix 体系的团队。

二 快速上手 原生工具与系统服务监控

  • 安装与运行状态:
    • 安装 MongoDB(Ubuntu 20.04/22.04 示例):sudo apt update & & sudo apt install -y mongodb-org
    • 启动与自启:sudo systemctl enable --now mongod
    • 查看状态与日志:sudo systemctl status mongod;journalctl -u mongod -f
  • 原生性能工具:
    • mongostat:每秒输出连接、插入/查询/更新/删除、getmore、command、flushes、vsize、res、faults、locked 等关键指标,用于快速定位吞吐与锁争用。
    • mongotop:按集合统计读写耗时,定位热点集合与慢集合。
    • Shell 诊断:
      • db.serverStatus():全局运行状态(连接、内存、网络、opcounters、wiredTiger、repl 等)
      • db.collection.stats():集合级统计(大小、文档数、索引、存储引擎细节)
  • 建议:将 mongostat/mongotop 输出接入日志或时序库,结合固定频率巡检脚本形成基线。

三 自建可观测栈 Prometheus Grafana 与 Alertmanager

  • 组件与端口约定:
    • mongodb_exporter(Percona 版):默认 9216 端口,采集 MongoDB 指标供 Prometheus 抓取。
    • Prometheus:默认 9090 端口,配置抓取任务与告警规则。
    • Alertmanager:默认 9093 端口,负责去重、分组、静默与路由通知。
    • Grafana:默认 3000 端口,用于可视化仪表盘。
  • 部署与配置要点:
    • 启动 mongodb_exporter(示例):
      docker run -d --name mongodb_exporter -p 9216:9216
      -e MONGODB_URI=mongodb://username:password@localhost:27017
      percona/mongodb_exporter:latest
    • Prometheus 抓取配置(prometheus.yml 片段):
      scrape_configs:
      • job_name: ‘mongodb’ static_configs:
        • targets: [‘< mongodb_host_or_exporter_host> :9216’]
    • Grafana:添加 Prometheus 数据源,导入 MongoDB 官方或社区仪表盘(如 Percona 提供的 MongoDB Exporter 仪表盘)。
  • 告警规则示例(rules.yml): groups:
    • name: mongodb-alerts rules:
      • alert: MongodbDown expr: mongodb_up == 0 for: 0m labels: { severity: critical } annotations: summary: “MongoDB Down on { { $labels.instance } } ” description: “MongoDB 实例不可达,当前值:{ { $value } } ”
      • alert: MongodbTooManyConnections expr: avg by(instance) (rate(mongodb_ss_connections{ conn_type=“current”} [1m])) / avg by(instance) (sum by(instance)(mongodb_ss_connections)) * 100 > 80 for: 2m labels: { severity: warning } annotations: summary: “MongoDB 连接数超过 80%({ { $labels.instance } } )” description: “当前连接占用率:{ { $value | humanizePercentage } } ”
      • alert: MongodbCursorsTimeouts expr: increase(mongodb_ss_metrics_cursor_timedout[1m]) > 100 for: 2m labels: { severity: warning } annotations: summary: “MongoDB 游标超时过多({ { $labels.instance } } )” description: “1 分钟内超时游标数:{ { $value } } ”
      • alert: MongodbVirtualMemoryUsage expr: (sum by(instance)(mongodb_ss_mem_virtual)) / (sum by(instance)(mongodb_ss_mem_resident)) > 3 for: 2m labels: { severity: warning } annotations: summary: “MongoDB 虚拟内存倍数过高({ { $labels.instance } } )” description: “虚拟/常驻内存比:{ { $value | humanizePercentage } } ”
  • 生效与验证:
    • 检查配置:docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml
    • 热加载:curl -X POST http://localhost:9090/-/reload
    • 在 Prometheus Web:Status → Targets 确认 mongodb 任务 UP;Alerts 页面查看规则与触发状态。

四 企业监控与替代方案

  • MongoDB Cloud Manager:注册账户后添加部署,即可获得 监控、备份、性能顾问、自动化修复 等能力,适合希望降低自建运维成本的团队。
  • Zabbix 方案:在 Zabbix Agent 上通过 UserParameter 执行 mongo shell 获取指标,如:
    • UserParameter=mongodb.connections, mongo --eval “printjson(db.serverStatus().connections)”
    • UserParameter=mongodb.queries, mongo --eval “printjson(db.serverStatus().opcounters)”
      在 Zabbix Server 创建监控项与触发器(如连接数阈值、查询速率异常等),适合已有 Zabbix 体系的统一监控。

五 关键告警阈值与优化建议

  • 建议阈值与处置要点(结合业务调优):
    • 实例宕机:mongodb_up == 0(立即处置,检查进程、端口、复制集状态)。
    • 连接数过高:连接占用率 > 80%(持续 2m),检查连接泄漏、连接池配置与应用重试逻辑。
    • 游标异常:游标超时数在 1m 内 > 100,检查未关闭游标、长事务、驱动版本与查询写法。
    • 虚拟内存倍数过高:虚拟/常驻 > 3,关注工作集是否超出内存、页面置换与 WiredTiger 缓存命中。
  • 性能与稳定性优化(配合监控落地):
    • 内存与存储:为 WiredTiger 设置合理 cacheSizeGB(常驻内存的 60%–70% 为起点),优先 SSD
    • 连接与网络:根据压测调优 net.maxIncomingConnections,避免过大连接风暴。
    • 慢查询治理:开启并分析 profiling,使用 explain(“executionStats”) 优化索引与查询计划。
    • 索引策略:为高频查询路径建立复合索引,定期清理无用索引,减少碎片。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: MongoDB在Ubuntu上的监控与告警设置
本文地址: https://pptw.com/jishu/753178.html
Ubuntu中PostgreSQL如何实现负载均衡 Ubuntu中PostgreSQL如何备份数据

游客 回复需填写必要信息