连接到 Atlas Free 集群时出错 (MongoDB)

Error connecting to Atlas Free Cluster (MongoDB)

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) 替换并测试,但它仍然失败。

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

我认为您的密码解析有问题,可能它包含特殊字符。

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

您可以按照 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
})

此外,请检查您是否使用帐户密码而不是 cluster/database 密码。

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

我刚刚将 Atlas 密码更改为一个没有特殊字符的简单密码,连接成功了!我现在很惭愧