在 Debian 上进行 MongoDB 性能监控的实用方案
一 快速上手 内置工具
mongostat -h **localhost** -p **27017** -u **root** -p **your_password** --authenticationDatabase **admin**。mongotop -h **localhost** -p **27017** -u **root** -p **your_password** --authenticationDatabase **admin** --db **your_database_name**。db.serverStatus() 获取更细的服务器指标与状态。sudo apt-get update && sudo apt-get install -y **mongostat** **mongotop**。二 长期监控与可视化 Prometheus Grafana
prometheus.yml 的 scrape_configs 抓取目标。docker run -d -p **9216:9216** --name mongodb-exporter bitnami/mongodb_exporter;如使用包安装,注意不同发行包/版本监听端口可能为 9216 或 9302。scrape_configs:
- job_name: 'mongodb_exporter'
static_configs:
- targets: ['localhost:9216']
该方案适合长期留存指标、绘制趋势图与集中告警。
三 慢查询与日志
operationProfiling 段):operationProfiling:
slowOpThresholdMs: 100 # 记录超过 100ms 的操作
mode: slowOp # 仅记录慢操作
/var/log/mongodb/mongod.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 mongodb adm
}
四 系统层面与告警
groups:
- name: mongodb
rules:
- alert: HighConnectionCount
expr: rate(mongodb_ss_connections[1m]) > 1000
for: 10m
labels:
severity: warning
annotations:
summary: "High connection count on {{ $labels.instance }}"
description: "Connection count is above 1000 for more than 10 minutes."