使用mongoose的Node.js和MongoDB Atlas之间的连接错误

时间:2020-04-15 05:21:57

标签: node.js express mongoose mongodb-atlas

我正在尝试使用mongodb地图集构建后端应用程序。我已按照指示进行操作,但出现了这样的错误

{ 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)]: {} }

这是我的代码

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));

我在mongodb地图集中的白名单条目是0.0.0.0/0

3 个答案:

答案 0 :(得分:0)

使用您的数据库用户名和密码更改用户名和密码。检查mongoDB Atlas,您在其中创建了数据库和集合。

答案 1 :(得分:0)

原因是互联网连接。我只要更改我的wifi连接即可。这是我得到答案的链接

Error at connecting to MongoDb Atlas Server

答案 2 :(得分:0)

您的网络或主机的端口27017被阻止,因此您无法访问服务器。

  • 要测试端口是否被阻塞,可以在mac / linux的终端上尝试 (curl portquiz.net:27017)或cmd(nc -v portquiz.net 27017)

这应该可以解决您的问题。