简体   繁体   English

nodejs和mongodb图集使用mongoose的连接错误

[英]Connection error between nodejs and mongodb atlas using mongoose

I am trying to build a backend app using mongodb atlas.我正在尝试使用 mongodb 地图集构建后端应用程序。 I have followed the instruction but I got error like this我已按照说明进行操作,但出现这样的错误

{ MongooseServerSelectionError: connect ECONNREFUSED 18.140.224.177:27017
at new MongooseServerSelectionError 
...
  message: 'connect ECONNREFUSED 18.140.224.177:27017',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: null,
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'database-shard-00-01-ayo9w.mongodb.net:27017' => [ServerDescription],
        'database-shard-00-00-ayo9w.mongodb.net:27017' => [ServerDescription],
        'database-shard-00-02-ayo9w.mongodb.net:27017' => [ServerDescription] 
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: null,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: null },
  [Symbol(mongoErrorContextSymbol)]: {} }

and here is my code这是我的代码

const mongoose = require('mongoose');
mongoose.connect("mongodb+srv://thanh:Abcdefghijk@database-ayo9w.mongodb.net/test?retryWrites=true&w=majority",
    { useNewUrlParser: true, useUnifiedTopology: true, },
  ).then(() => console.log('Connected to database')).
  catch(err => console.log('Error in connection', err));

My whitelist entry in mongodb atlas is 0.0.0.0/0我在 mongodb 地图集中的白名单条目是 0.0.0.0/0

Change username and password with your database username and password.使用您的数据库用户名和密码更改用户名和密码。 Check mongoDB Atlas where you have created your database and collections.检查您创建数据库的 mongoDB Atlas 和 collections。

The reason for this is the internet connection.原因是互联网连接。 I just change my wifi connection and it works.我只是改变了我的wifi连接,它就可以了。 This is the link where I get the answer这是我得到答案的链接

Error at connecting to MongoDb Atlas Server 连接 MongoDb Atlas 服务器时出错

Your network or hosting has port 27017 blocked, that's why you can't access server.您的网络或主机的 27017 端口被阻止,这就是您无法访问服务器的原因。

  • To test if the port is blocked you can try this on terminal on mac/linux (curl portquiz.net:27017) or on cmd (nc -v portquiz.net 27017)要测试端口是否被阻止,您可以在 mac/linux (curl portquiz.net:27017) 或 cmd (nc -v portquiz.net 27017) 上的终端上尝试此操作

This should solve your issue.这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM