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

MongoDB集群连接字符串登录失败:认证错误排查及格式咨询

MongoDB集群连接字符串认证失败问题排查

连接字符串格式疑问

我的邮箱是bobsmith123@email.com,密码为password(URL编码后为p%40ssword),请问以下两个连接字符串哪个正确?

选项1:

const uri = "mongodb+srv://bobsmith123:p@40ssword@cluster0.mpnik7h.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";

选项2:

const uri = "mongodb+srv://bobsmith123%40email.com:p%40ssword@cluster0.mpnik7h.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";

遇到的错误信息

我确认MongoDB Cloud登录密码正确,但反复出现认证失败错误:

MongoServerError: bad auth : authentication failed     at Connection.sendCommand (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\connection.js:281:27)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     at async Connection.command (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\connection.js:304:26)     at async continueScramConversation (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\auth\scram.js:131:15)     at async executeScram (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\auth\scram.js:80:5)     at async performInitialHandshake (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\connect.js:101:13)     at async connect (C:\Users\User\Desktop\project\project-api\node_modules\mongodb\lib\cmap\connect.js:19:9) {   errorResponse: {     ok: 0,     errmsg: 'bad auth : authentication failed',     code: 8000,     codeName: 'AtlasError'   },   ok: 0,   code: 8000,   codeName: 'AtlasError',   connectionGeneration: 0,   [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' } }

我的连接代码

const MongoClient = require("mongodb").MongoClient;
const ServerApiVersion = require("mongodb").ServerApiVersion;
//uri is kept secret from users
const uri = require('./url.js').uri;
//const uri = process.env.MONGODB_URI;
console.log(uri);
const client = new MongoClient(uri, 
    {ServerApi: 
        {
            version: ServerApiVersion.v1,
            strict: true,
            deprecationErrors: true
        }
    }
);
console.log(client);
let db;
async function run() {
    try{
        await client.connect();
        await client.db("admin").command({ ping: 1 });
        console.log("Pinged your deployment. You successfully connected to MongoDB!");
        //db = client.db("Cluster0");
    }catch(error){
        console.log(error);
    }finally{
        await client.close();
    }
};
run();
module.exports = {db}

我尝试了上述两个URI均无法成功连接,请求帮助解决。


解决建议

  1. 确认连接字符串格式正确性

    • 使用邮箱作为MongoDB Atlas用户名时,邮箱中的@必须URL编码为%40,因此选项2的格式是正确的;选项1仅填写bobsmith123作为用户名,缺少完整邮箱域名,必然认证失败。
    • 密码中的特殊字符已正确URL编码(@替换为%40),这部分无需调整。
  2. 排查其他认证失败原因

    • 检查用户权限:确认该邮箱对应的Atlas用户已绑定目标集群,且拥有集群的读写权限。
    • IP白名单配置:确认本地IP已添加到Atlas的IP白名单中,可临时开启“允许所有IP访问”(仅测试用)排除IP限制问题。
    • 核对集群地址:检查cluster0.mpnik7h.mongodb.net是否与Atlas控制台显示的集群地址完全一致,避免拼写错误。
    • 配置文件校验:检查url.js中导出的uri是否存在空格、引号转义等格式问题,确保配置加载正确。
    • 驱动版本兼容:确认mongodb驱动版本与Atlas集群版本兼容,建议升级至最新稳定版。
  3. 验证连接字符串的实用技巧

    • 直接从Atlas控制台复制生成的连接字符串,替换用户名和编码后的密码,避免手动拼接出错。
    • 使用mongosh命令在终端测试连接:
      mongosh "mongodb+srv://bobsmith123%40email.com:p%40ssword@cluster0.mpnik7h.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
      
      若终端连接成功,说明问题出在代码配置而非字符串本身。

内容的提问来源于stack exchange,提问作者Dennis Balan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:26:03