简体   繁体   中英

Issues connecting with MongoDB Atlas from Node.js

I have recently came across mongoDB Atlas so i'm not really familiar with it. I followed the necessary procedure to set it up and tried linking my Node.js code to it. 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. I am sure that it is correct.

This is the following error that I receive every time I use nodemon to run it:

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.

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.

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.

2.Check in Built in role that you selected admin option.


3.Do check if you are not connected to any VPN because while making cluster you choose different country.


4.Make sure your connection string is in this format " 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.

6.Dont do more than one connection for the first time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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