You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prometheus MongoDB Exporter部署问题求助:mongodb_up为0及连接、端口报错

问题分析与解决步骤

1. 修复MongoDB连接协议错误

报错里的连接URL使用了http://,但MongoDB的连接协议必须是mongodb://,这是导致无法创建会话的核心问题。启动exporter时需指定正确的连接字符串:

# 带认证的情况
mongodb_exporter --mongodb.uri="mongodb://test:testing@localhost:27017/admin"
# 无认证的情况
mongodb_exporter --mongodb.uri="mongodb://localhost:27017/admin"

2. 解决端口9216被占用问题

报错listen tcp localhost::9216: bind: address already in use说明该端口已被其他进程占用,可通过以下方式处理:

  • 查看占用端口的进程:
    lsof -i :9216
    # 或用netstat
    netstat -tulpn | grep 9216
    
  • 杀掉占用进程(替换<PID>为上述命令返回的进程ID):
    kill -9 <PID>
    
  • 或更换未被占用的端口启动exporter,比如9217:
    mongodb_exporter --web.listen-address=":9217" --mongodb.uri="mongodb://test:testing@localhost:27017/admin"
    

3. 修正Prometheus配置错误

你的scrape配置中错误混入了Prometheus自身的端口localhost:9090,正确配置应仅包含MongoDB Exporter的地址:

scrape_configs:
  - job_name: "mongodb_exporter"
    static_configs:
      - targets: ["localhost:9216"]  # 若更换了端口,需同步修改此处,比如localhost:9217

修改完成后重启Prometheus服务。

4. 验证连接与指标

  • 启动exporter后,访问http://localhost:9216/metrics(端口对应实际使用值),检查是否存在mongodb_up 1的指标,存在则表示连接成功。
  • 验证MongoDB认证信息:确保用户test拥有admin数据库的访问权限,密码testing正确。可先用客户端测试连接:
    mongosh "mongodb://test:testing@localhost:27017/admin"
    
    若连接失败,需检查MongoDB的用户配置,或暂时关闭认证测试连接是否正常。

内容的提问来源于stack exchange,提问作者Joyci Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:55:03