连接到Atlas Free Cluster(MongoDB)时出错 [英] Error connecting to Atlas Free Cluster (MongoDB)

查看:101
本文介绍了连接到Atlas Free Cluster(MongoDB)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:即使按照文档说的做,也无法连接到Atlas Cluster。

TL;DR: Can't connect to Atlas Cluster even after doing exactly what docs said.

所以我看了开始使用Atlas ,一切似乎都很好&简单。我确实按照步骤操作,创建了一个免费集群,将我的IP列入了白名单,然后尝试使用其示例应用程序进行连接:

Hi, so I read the docs of getting started with Atlas and everything seemed nice & easy. I did follow the steps, created a free cluster, whitelisted my IP, and then tried to connect using their sample app:

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

But the following error occurred when I tried to execute with: 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中的一个更改连接字符串:(因为它与文档有些许不同)

I tried changing the connection string with the one from Atlas: (because it was different from the docs by a tiny bit)

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

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




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

But still the same result. My password had a !character so I put %21 instead of it. I also replaced with cluster name (Remote-Doc) and test but it still failed.

I'd appreciate if you could help me!

推荐答案

我认为您在解析密码时遇到问题,也许其中包含特殊字符。

I think that you are having problem with the parse of your password, maybe it has special characters.

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

The best way to handle this is change the way that you are connecting to pass the user and password as options.

您可以按照 doc ,然后将MongoClient的连接更改为以下内容:

You can follow the doc and change your MongoClient conection for something like this:

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 您可以执行以下操作:

And with mongoose you can do something like this:

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

还要检查您是否使用的不是帐户密码,而不是群集/数据库密码。

Also check if you are not using the account password instead of the cluster/database password.

您可以按照本教程检查是否使用了正确的方法: MongoDB Atlas设置-数字海洋

You can follow this tutorial to check if you are using the correct one: MongoDB Atlas Setup - Digital Ocean.

这篇关于连接到Atlas Free Cluster(MongoDB)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆