【问题标题】:How to connect to mongoDB Atlas using mongoose如何使用 mongoose 连接到 mongoDB Atlas
【发布时间】:2017-09-09 16:18:25
【问题描述】:

我正在尝试通过 Mongoose.connect() 连接到我在 mongoDB Atlas 上的集群,但每次我尝试连接时都会收到异常“MongoError:身份验证失败” 我知道 MongoDB Atlas 是新的 mongo 即服务,mongoose 还不支持它吗?。

【问题讨论】:

  • 您能否显示您正在使用的连接字符串(但请用占位符替换用户名和密码)。另外,看看at this
  • 'mongodb://username:@hcluster0-shard-00-00-he3ln.mongodb.net:27017,hcluster0-shard-00-01-he3ln.mongodb.net: 27017,hcluster0-shard-00-02-he3ln.mongodb.net:27017/?ssl=true&replicaSet=hcluster0-shard-0&authSource=admin'
  • <hcluster0‌​> 是您数据库的实际名称吗?此外,如果 password 包含在 URI 中具有特殊含义的任何字符(如 @+%/),则需要对这些字符进行编码。
  • 你的公网IP加入白名单了吗?
  • 是的,它是实际名称(但我只是将它用于测试并不重要,不用担心)。但我之前使用的是 mLab,我不需要编码任何东西。是的,我已将公共 IP 地址添加到白名单中。

标签: node.js mongodb express mongodb-atlas


【解决方案1】:

"mongodb+srv://:@cluster0.vvkuk.mongodb.net/" 同样在地图集,在安全进入网络访问,会有小按钮编辑和删除点击编辑,在编辑中,有两个选项第一个选项是添加当前IP地址和第二个选项将是允许访问从任何地方 选择第一个选项然后点击确认

【讨论】:

    【解决方案2】:
    const mongoAtlasUri =
      "mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
    
    try {
      // Connect to the MongoDB cluster
      mongoose.connect(
        mongoAtlasUri,
        { useNewUrlParser: true, useUnifiedTopology: true },
        () => console.log(" Mongoose is connected"),
      );
    } catch (e) {
      console.log("could not connect");
    }
    
    const dbConnection = mongoose.connection;
    dbConnection.on("error", (err) => console.log(`Connection error ${err}`));
    dbConnection.once("open", () => console.log("Connected to DB!"));
    

    【讨论】:

    • 请考虑在您的代码中添加注释/提示。
    【解决方案3】:

    MongoError: authentication failed - 这意味着你的名字或密码或数据库名不正确 -

    uri 样本 -

    const uri =
        "mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
    

    假设用户名是 - najim & 密码是 1234 & dbname 是 pets (注意 - 默认 dbname 是 test 但你可以写任何你想要的想要)然后我的 uri 将与上面的 credentails -

    const  mongoAtlasUri =
            "mongodb+srv://najim:1234@firstcluster.4rc4s.mongodb.net/pets?retryWrites=true&w=majority";
    

    连接moongoose

    try {
        // Connect to the MongoDB cluster
         mongoose.connect(
          mongoAtlasUri,
          { useNewUrlParser: true, useUnifiedTopology: true },
          () => console.log(" Mongoose is connected")
        );
    
      } catch (e) {
        console.log("could not connect");
      }
    

    【讨论】:

      【解决方案4】:
      try {
      mongoose.connect( uri, {useNewUrlParser: true, useUnifiedTopology: true}, () =>
      console.log("connected"));    
      }catch (error) { 
      console.log("could not connect");    
      }
      

      这个很好用,试试吧

      【讨论】:

        【解决方案5】:

        The answer in this related post 是正确的。你应该:

        • 不要将选项与连接字符串混合(如果这样做)
        • 确保您运行的 IP 已列入白名单,并且您的网络允许连接到 Atlas
        • 确保用户有足够的权限
        • 使用atlas提供的原样的连接字符串,只提供给

          mongoose.connect(uri);
          

        【讨论】:

        • 还要确保您使用的是最新的猫鼬版本?
        • 这行得通,但特殊字符需要作为文字输入,reference。我发现我的密码有这个问题
        猜你喜欢
        • 2020-02-22
        • 2020-07-25
        • 2019-03-14
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        • 2019-12-30
        • 1970-01-01
        • 2019-12-10
        相关资源
        最近更新 更多