Atlas MongoDB连接 [英] Atlas MongoDB connection

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

问题描述

我正在尝试使用以下URI(由mongodb连接字符串提供)连接到Atlas MongoDB

I am trying to connect to Atlas MongoDB with the following URI (provided by mongodb connection string )

    module.exports = {
        mongoURI:'mongodb+srv://<user>:<password>@cluster0-un6sk.mongodb.net/test? 
    retryWrites=true'
    };

    //connect to mongoose
    mongoose
        .connect(db)
        .then( ()=>console.log('mongoDB connected'))
        .catch(err => console.log(err));

我收到以下错误消息:

{ MongoNetworkError: connection 3 to cluster0-shard-00-00-un6sk.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\KARTIT Ismail\Desktop\devconnector\node_modules\mongodb-core\lib\connection\connection.js:352:9)
    at Object.onceWrapper (events.js:273:13)
    at TLSSocket.emit (events.js:182:13)
    at _handle.close (net.js:606:12)
    at TCP.done (_tls_wrap.js:386:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

推荐答案

确保已在"Atlas控制"面板中将IP列入白名单.

Make sure you have whitelisted your IP in the Atlas Control panel.

如果希望允许来自任何主机的访问,则可以输入0.0.0.0.

You can enter 0.0.0.0, if you wish to allow access from any host.

然后您将具有如下所示的连接字符串:

Then you can have a connection string like below:

var connectionString= 'mongodb://<username>:<password>@<clustername>/<dbname>?ssl=true&replicaSet=<replica setname>&authSource=admin';

var db = mongoose.connect(connectionString).catch((error) => { console.log(error); });

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

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