必须使用含全部节点和replicaset=参数的副本集uri,如mongodb://user:pass@node1:27017,node2:27017,node3:27017/admin?replicaset=rs0&authsource=admin,并配合--mongodb.direct-connect=false启用副本集拓扑发现,否则mongodb_replset_health等指标缺失或为0。
☞☞☞☞点击夸克ai手把手教你,操作像呼吸一样简单!☜☜☜☜☜

mongodb_exporter 连接副本集时 URI 怎么写
必须用 MongoDB 副本集格式的连接串,不能只写单个节点地址。否则 mongodb_replset_health、mongodb_replset_member_state 等关键指标会缺失或为 0。
正确写法是把所有成员地址列全,并加上 ?replicaSet= 参数:
mongodb://user:pass@node1:27017,node2:27017,node3:27017/admin?replicaSet=rs0&authSource=admin
-
node1–node3是副本集所有成员的 host:port,顺序无关 -
rs0必须和 MongoDB 实际配置的replSetName完全一致(区分大小写) - 如果启用了 TLS,需额外加
&tls=true&tlsAllowInvalidCertificates=true - 不推荐省略
admin数据库名或authSource=admin,否则认证可能失败
为什么 mongodb_up=0 但日志没报错
常见于副本集主节点切换期间,或 exporter 启动时恰好连上了非主节点且未启用 directConnect=false(默认为 true)。
mongodb_exporter 默认使用 direct connect 模式,即只连 URI 里第一个地址,不自动发现副本集拓扑。这会导致它无法感知主从变化,也无法拉取 replSetGetStatus 等命令结果。
- 解决方法:显式关闭 direct connect,加参数
--mongodb.direct-connect=false - 同时确保 URI 中包含全部节点 +
replicaSet=xxx,否则仍无法建立 replica set session - 验证方式:curl http://localhost:9216/metrics | grep mongodb_up,观察是否稳定为 1
collector.replicasetstatus 收不到数据?检查权限和参数
该收集器依赖 replSetGetStatus 命令,需要 clusterMonitor 角色,且 exporter 必须以 replica set 模式连接(而非 direct)。即使连接成功,权限不足也会静默跳过采集。
- MongoDB 中确认监控用户有
{role:"clusterMonitor", db:"admin"}和{role:"read", db:"local"} - 启动命令中必须包含
--collector.replicasetstatus,默认不启用 - 若使用
percona/mongodb_exporter镜像,注意 0.39.0+ 版本已将该 collector 设为默认开启;旧版本需手动指定 - 错误现象:
mongodb_replset_health指标完全不出现,或值恒为 0 —— 先查 exporter 日志是否有failed to get replset status类提示
多个副本集共存时怎么避免端口冲突和指标混淆
一个 mongodb_exporter 实例只能监控一个 MongoDB 服务(单节点 / 副本集 / 分片集群中的某角色)。多副本集必须部署多个 exporter 实例,各自绑定不同监听端口,并在 Prometheus 中用 distinct labels 区分。
- 例如两个副本集
rs0和rs1,分别启动: ./mongodb_exporter --mongodb.uri="mongodb://...?replicaSet=rs0" --web.listen-address=":9216" --web.telemetry-path="/metrics"./mongodb_exporter --mongodb.uri="mongodb://...?replicaSet=rs1" --web.listen-address=":9217" --web.telemetry-path="/metrics"- Prometheus 的
scrape_configs中为每个 target 加上labels: {mongodbrs: "rs0"}或{mongodbrs: "rs1"} - 忽略这点会导致
mongodb_replset_member_state等指标混在一起,Grafana 查询时无法按副本集下钻
最易被忽略的是 directConnect=false 和 replicaSet=xxx 的组合——少一个,副本集指标就基本废掉。别只盯着用户名密码对不对。











