【问题标题】:Error connecting to Atlas Free Cluster (MongoDB)连接到 Atlas Free Cluster (MongoDB) 时出错
【发布时间】:2020-09-27 20:44:50
【问题描述】:

TL;DR:即使完全按照文档所说的操作,也无法连接到 Atlas Cluster。

您好,我阅读了getting started with Atlas 的文档,一切看起来都很好且简单。我确实按照这些步骤创建了一个免费集群,将我的 IP 列入白名单,然后尝试使用他们的示例应用进行连接:

const { MongoClient } = require("mongodb");

// Replace the following with your Atlas connection string                                                                                                                                        
const url = "mongodb+srv://<username>:<password>@clustername.mongodb.net/test?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true";
const client = new MongoClient(url);

async function run() {
    try {
        await client.connect();
        console.log("Connected correctly to server");

    } catch (err) {
        console.log(err.stack);
    }
    finally {
        await client.close();
    }
}

run().catch(console.dir);

但是当我尝试执行时出现以下错误:node connect.js

PS C:\Users\marjo\Documents\mongoDB Atlas> node connect
(node:11352) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [remote-doc-shard-00-02-otc5a.mongodb.net:27017] on first connect [MongoError: bad auth Authentication failed.
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\auth_provider.js:46:25
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\scram.js:240:11
    at _callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.messageHandler (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:378:5)
    at Connection.emit (events.js:315:20)
    at processMessage (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connection.js:384:10)
    at TLSSocket.<anonymous> (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connection.js:553:15)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'
}]
    at Pool.<anonymous> (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (events.js:315:20)
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\pool.js:561:14
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\pool.js:1008:9
    at callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:97:5)
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:396:21
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\auth_provider.js:66:11
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\scram.js:240:11
    at _callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.messageHandler (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:378:5)

我尝试使用 Atlas 中的连接字符串更改连接字符串:(因为它与文档略有不同)

const uri = "mongodb+srv://Marjo:<password>@remote-doc-otc5a.mongodb.net/<dbname>?retryWrites=true&w=majority";

但还是一样的结果。我的密码有一个 ! 字符,所以我用 %21 代替了它。我也用集群名称(Remote-Doc)替换并测试,但它仍然失败。

如果您能帮助我,我将不胜感激!

【问题讨论】:

  • 您创建了一个数据库用户,对吧?这与您用于 atlas ui 的 cloud/atlas 用户不同。

标签: node.js mongodb connection-string mongodb-atlas


【解决方案1】:

我认为您在解析密码时遇到了问题,也许它有特殊字符。

处理此问题的最佳方法是更改​​连接方式以将用户和密码作为选项传递。

您可以关注doc 并将您的 MongoClient 连接更改为以下内容:

const mongoclient = new MongoClient(new Server("remote-doc-otc5a.mongodb.net", 27017));

// Listen for when the mongoclient is connected
mongoclient.open(function (err, mongoclient) {

    // Then select a database
    const db = mongoclient.db("dbname");

    // Then you can authorize your self
    db.authenticate('username', 'password', (err, result) => {
        // On authorized result=true
        // Not authorized result=false

        // If authorized you can use the database in the db variable
    });
});

使用mongoose,您可以执行以下操作:

mongoose.connect('mongodb+srv://@remote-doc-otc5a.mongodb.net/test?retryWrites=true&w=majority', {
    user: 'USERNAME',
    pass: 'PASSWORD',
    useNewUrlParser: true,
    useUnifiedTopology: true
})

另外,请检查您是否使用帐户密码而不是集群/数据库密码。

您可以按照本教程检查您是否使用了正确的:MongoDB Atlas Setup - Digital Ocean

【讨论】:

    【解决方案2】:

    我刚刚将 Atlas 密码更改为没有特殊字符的简单密码,并且连接正常!我现在很惭愧

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 2018-09-28
      • 1970-01-01
      • 2021-01-05
      • 2020-02-26
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      相关资源
      最近更新 更多