简体   繁体   English

从 Node.js 与 MongoDB Atlas 连接的问题

[英]Issues connecting with MongoDB Atlas from Node.js

I have recently came across mongoDB Atlas so i'm not really familiar with it.我最近遇到了 mongoDB Atlas,所以我不太熟悉它。 I followed the necessary procedure to set it up and tried linking my Node.js code to it.我按照必要的程序进行了设置,并尝试将我的 Node.js 代码链接到它。 This is how I attempted to connect:这就是我尝试连接的方式:

const express = require("express");
const mongoose = require("mongoose");

const app = express();

app.use(express.json());

const db = require("./config/keys").mongoURI;

mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log("MongoDB successfully connected"))
    .catch(err => console.log(err));

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server up and running on port ${port} !`));

The mongoURI is stored in the config/keys.js file. mongoURI 存储在 config/keys.js 文件中。 I am sure that it is correct.我确信这是正确的。

This is the following error that I receive every time I use nodemon to run it:这是我每次使用 nodemon 运行它时收到的以下错误:

MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (/Users/Desktop/SEMESTER 2/Dummy/Server/node_modules/mongoose/lib/error/serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (/Users/Desktop/SEMESTER 2/Dummy/Server/node_modules/mongoose/lib/connection.js:823:32)
    at Mongoose.connect (/Users/Desktop/SEMESTER 2/Dummy/Server/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/Users/Desktop/SEMESTER 2/Dummy/Server/server.js:10:10)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'dummy-shard-00-01-3gxsb.gcp.mongodb.net:27017' => [ServerDescription],
      'dummy-shard-00-02-3gxsb.gcp.mongodb.net:27017' => [ServerDescription],
      'dummy-shard-00-00-3gxsb.gcp.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

I have already changed my list of IP addresses to include every IP address, it still doesn't work.我已经更改了我的 IP 地址列表以包含每个 IP 地址,但它仍然不起作用。

Do you guys know what is resulting in this?大家知道这是什么原因吗?

Found the reason why: Turns out the wifi I was connected to while trying to connect with MongoDB Atlas doesn't allow outbound connections to port 27017. I tried using other sources of wifi and it resolved the issue.找到原因:原来我在尝试连接 MongoDB 时连接的 wifi 不允许到端口 27017 的出站连接。我尝试使用其他 wifi 来源,它解决了这个问题。

I am writing this for helping others to save there time because I already wasted my time for solving this issues.我写这篇文章是为了帮助其他人节省时间,因为我已经浪费了我的时间来解决这个问题。 If you feel that your code is totally correct then also not connecting, THE BASIC SETTINGs YOU HAVE TO DO如果您觉得您的代码完全正确,那么也没有连接,您必须做的基本设置

  1. In Mongodb Atlas see if your network access is your IP address or prefer allow access to all.在 Mongodb Atlas 中查看您的网络访问是否是您的 IP 地址或者更喜欢允许访问所有人。

2.Check in Built in role that you selected admin option. 2.签入您选择管理员选项的内置角色。


3.Do check if you are not connected to any VPN because while making cluster you choose different country. 3.请检查您是否没有连接到任何VPN,因为在制作集群时您选择了不同的国家。


4.Make sure your connection string is in this format " mongodb+srv:// yourid : password @cluster0.0q8wi.mongodb.net/ databasename " 4.确保您的连接字符串格式为“ mongodb+srv:// yourid : password@cluster0.0q8wi.mongodb.net/databasename


  1. Make sure you read and obey the commands according to documentations given by the frameworks like Mongoose,Node.js,Express etc.确保根据 Mongoose、Node.js、Express 等框架提供的文档阅读并遵守命令。

6.Dont do more than one connection for the first time. 6.第一次不要做多个连接。

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

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