skip to Main Content

I am getting error while connecting to mongoDB. I don’t know what went wrong. please help me to fix this.

note:-I have configure "Allow access from Anywhere"
Below is the error message.

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (D:REBOOTAll projectsE-commercedemonode_modulesmongooselibconnection.js:805:11)
at NativeConnection.openUri (D:REBOOTAll projectsE-commercedemonode_modulesmongooselibconnection.js:780:11)
at async Server. (D:REBOOTAll projectsE-commercedemosrcserver.js:6:3) {
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
servers: Map(3) {
‘ac-tncrtmi-shard-00-01.pin5pfl.mongodb.net:27017’ => ServerDescription {
address: ‘ac-tncrtmi-shard-00-01.pin5pfl.mongodb.net:27017’,
type: ‘Unknown’,
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 74743438,
lastWriteDate: 0,
error: MongoNetworkError: connect EACCES 64:ff9b::3e5:eb13:27017
at connectionFailureError (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:367:20)
at TLSSocket. (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:290:22)
at Object.onceWrapper (node:events:629:26)
at TLSSocket.emit (node:events:514:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect EACCES 64:ff9b::3e5:eb13:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -4092,
code: ‘EACCES’,
syscall: ‘connect’,
address: ’64:ff9b::3e5:eb13′,
port: 27017
},
[Symbol(errorLabels)]: Set(1) { ‘ResetPool’ }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
‘$clusterTime’: null
},
‘ac-tncrtmi-shard-00-02.pin5pfl.mongodb.net:27017’ => ServerDescription {
address: ‘ac-tncrtmi-shard-00-02.pin5pfl.mongodb.net:27017’,
type: ‘Unknown’,
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 74743023,
lastWriteDate: 0,
error: MongoNetworkError: connect EACCES 64:ff9b::b849:212b:27017
at connectionFailureError (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:367:20)
at TLSSocket. (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:290:22)
at Object.onceWrapper (node:events:629:26)
at TLSSocket.emit (node:events:514:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect EACCES 64:ff9b::b849:212b:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -4092,
code: ‘EACCES’,
syscall: ‘connect’,
address: ’64:ff9b::b849:212b’,
port: 27017
},
[Symbol(errorLabels)]: Set(1) { ‘ResetPool’ }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
‘$clusterTime’: null
},
‘ac-tncrtmi-shard-00-00.pin5pfl.mongodb.net:27017’ => ServerDescription {
address: ‘ac-tncrtmi-shard-00-00.pin5pfl.mongodb.net:27017’,
type: ‘Unknown’,
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 74743026,
lastWriteDate: 0,
error: MongoNetworkError: connect EACCES 64:ff9b::22ca:d412:27017
at connectionFailureError (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:367:20)
at TLSSocket. (D:REBOOTAll projectsE-commercedemonode_modulesmongodblibcmapconnect.js:290:22)
at Object.onceWrapper (node:events:629:26)
at TLSSocket.emit (node:events:514:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect EACCES 64:ff9b::22ca:d412:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -4092,
code: ‘EACCES’,
syscall: ‘connect’,
address: ’64:ff9b::22ca:d412′,
port: 27017
},
[Symbol(errorLabels)]: Set(1) { ‘ResetPool’ }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
‘$clusterTime’: null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: ‘atlas-2nwske-shard-0’,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}

Node.js v18.17.1
[nodemon] app crashed – waiting for file changes before starting…

Below is my code in 3 separate file index.js, db.js server.js

index.js

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

const app = express();
app.use(express.json());
app.use(cors());

app.get("/", (req, res) => {
  return res
    .status(200)
    .send({ message: "Welcome to ecommerce API-node", status: true });
});


module.exports = app;

server.js File

const app = require(".");
const { connectDb } = require("./config/db");

const PORT = 5454;
app.listen(PORT, async () => {
  await connectDb();
  console.log("eCommerce API listening on PORT: ", PORT);
});

db.js

const mongoose = require("mongoose");
const mongoDbUrl =
  "mongodb+srv://xxxxxxxxxx:*************@cluster0.------------.mongodb.net/?retryWrites=true&w=majority";

const connectDb = () => {
  return mongoose.connect(mongoDbUrl);
};

module.exports = { connectDb };

database is not connecting to application.

I didn’t try any different step.

2

Answers


  1. One of the reason is that your MongoDB Atlas Network IP is restricting you to access the database as per error you posted.

    Fix: Open the MongoDB Atlas and Select to the Network Access from the sidebar and make sure that your IP is whitelisted or not. If not then simply get your IP by searching on Google "What is my IP".

    Add that IP in MongoDB Atlas like this "75.166.157.162/32".

    Login or Signup to reply.
  2. Note that the error message is calling out an IPv6 address:

    EACCES 64:ff9b::3e5:eb13:27017
    

    That appears to be the IPv4 address of the Atlas server embedded in an IPv6 address with the prefix 64:ff9b::

    Make sure your routes are set up to handle that address, and make sure that the public IPV4 address that the connection exits your network on has been added to the whitelist in Atlas.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search