Can't connect to mongodb atlas on nodejs - node.js

mongoose.connect(
process.env.DB_CONFIG,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
}
)
.then((x) => {
console.log(
`Connected to MongoDB!`
);
})
.catch((err) => {
console.error("Error connecting to mongo", err);
});
This is the piece of code I have been working on and this works fine until this day.
I don't know why and got an error like this:
Error connecting to mongo MongooseServerSelectionError: connection timed out
at NativeConnection.Connection.openUri (D:\projects\medium-clone\server\node_modules\mongoose\lib\connection.js:846:32)
at D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:351:10
at D:\projects\medium-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\projects\medium-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:1149:10)
at Mongoose.connect (D:\projects\medium-clone\server\node_modules\mongoose\lib\index.js:350:20)
at Object.<anonymous> (D:\projects\medium-clone\server\index.js:12:4)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: 'atlas-vwy17h-shard-0',
maxSetVersion: null,
maxElectionId: null,
servers: Map(3) {
'authcluster-shard-00-00.ris23.mongodb.net:27017' => [ServerDescription],
'authcluster-shard-00-01.ris23.mongodb.net:27017' => [ServerDescription],
'authcluster-shard-00-02.ris23.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: 30,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 9
}
}
I tried adding my ip to network access in mongodb dashboard but didn't work

Sometimes this happens because of your own internet connection when you use mobile hotspot. In this case adding your IP may never work unfortunately because your IP may probably change every time you reconnect. When I had the same issue I restarted my connection and allowed all IPs in access network. And also make sure that your connection is stable.

Related

Error in Connecting the MongoDB server with Node app MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

I am working on a MERN project and Earlier my mongo server was working fine, but now that I have restarted my app it is giving me error i.e is
`
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/connection.js:824:32)
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:381:10
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:1234:10)
at Mongoose.connect (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:380:20)
at connectDataBase (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/config/dataBase.js:7:10)
at Object.<anonymous> (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/server.js:17:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
My confige.env file looks like this
PORT = 4000
DB_URI = "mongodb://localhost:27017/Ecommerce"
JWT_SECRET=JBRON098SROINRIOUHOHSDOKFN948HIFRCN
JWT_EXPIRE=5d
COOKIE_EXPIRE = 5
My app.js file looks like this
const app = require('./app')
const dotenv = require('dotenv')
const connectDataBase = require("./config/dataBase")
//Handling uncaught exception
process.on("uncaughtException", (err) => {
console.log(`Error: , ${err.message}`);
console.log(`Shutting down server due to unhandled promise rejection`);
process.exit(1);
})
//config file
dotenv.config({path:"backend/config/config.env"});
//Calling the function to connect the database
connectDataBase();
const server = app.listen(process.env.PORT, ()=>{
console.log(`The server has started at http://localhost/${process.env.PORT}`);
})
I have also tried connecting with compass but it is still not working. I used a MacBook air m2 with macOS Ventura 13.0.1
I wanted to connect to the server using the app only but it is giving me the error
`
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/connection.js:824:32)
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:381:10
at /Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:1234:10)
at Mongoose.connect (/Users/vaibhav/Desktop/Development/MERN PROJECT/node_modules/mongoose/lib/index.js:380:20)
at connectDataBase (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/config/dataBase.js:7:10)
at Object.<anonymous> (/Users/vaibhav/Desktop/Development/MERN PROJECT/backend/server.js:17:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
`

Should my mongoose.connect setup that works on a Mac work without being customised for Windows?

I had built a really simple API using Node and Express. It runs perfectly on my Mac, but when I cloned the repo from Github and tried to run it on a Windows laptop I get the following error message in the terminal.
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\connection.js:819:32)
at C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\index.js:379:10
at C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\index.js:1224:10)
at Mongoose.connect (C:\Users\wande\Employee-Tracker\node_modules\mongoose\lib\index.js:378:20)
at Object. (C:\Users\wande\Employee-Tracker\index.js:12:10)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) { reason:
TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined }, code: undefined }
My mongoose.connect setup in my index.js is as follows.
mongoose.connect('mongodb://localhost:27017/employeeData', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('MONGO CONNECTION OPEN');
})
.catch(err => {
console.log('MONGO CONNECTION ERROR');
console.log(err);
})
As I have said it seems to run perfectly on my Mac but I'm hitting major issues on a Windows laptop.
Does my mongoose.connect code need to be configured differently for it to run on Windows?
Many thanks
Rob

Could not connect to any servers in your MongoDB Atlas cluster

My mongodb local connection is successful but when i deploy to node server of sftp it gives mongodb network error,so please give me right answerand support.
while trying to connect with mongodb i’m getting this error :
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://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (/root/Whatshapp/Chat/node_modules/mongoose/lib/connection.js:819:32)
at /root/Whatshapp/Chat/node_modules/mongoose/lib/index.js:379:10
at /root/Whatshapp/Chat/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/root/Whatshapp/Chat/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/root/Whatshapp/Chat/node_modules/mongoose/lib/index.js:1224:10)
at Mongoose.connect (/root/Whatshapp/Chat/node_modules/mongoose/lib/index.js:378:20)
at Object.<anonymous> (/root/Whatshapp/Chat/db/db.js:4:21)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map {
'cluster0-shard-00-00.x1fjc.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.x1fjc.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.x1fjc.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-j9ttqh-shard-0',
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
And mongodb network access is :

I have been trying to link my node to Mongo Db to local host but it keep throwing me error

const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
}
eroor
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32) at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
at main (E:\Web Dev\Backend\New folder\index.js:5:18)
at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:3:1)
at Module._compile (node:internal/modules/cjs/loader:1097:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
Enabling the IPV6 that MongoDB has disabled by default, by using the given command also I'm adding a reference for your understanding. mongod --ipv6
https://docs.mongodb.com/manual/core/security-mongodb-configuration/
I tried with the given piece of code and its working now,
mongoose.connect('mongodb://localhost:27017/db_name',
{
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
}
);
https://www.section.io/engineering-education/nodejs-mongoosejs-mongodb/

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster

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.
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://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\connection.js:832:32)
at C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:345:10
at C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:1135:10)
at Mongoose.connect (C:\Users\Admin\Desktop\Working\SB\node_modules\mongoose\lib\index.js:344:20)
at Object.<anonymous> (C:\Users\Admin\Desktop\Working\SB\app.js:305:10) at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879: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 {
'smile-shard-00-00.xeyqd.mongodb.net:27017' => [ServerDescription],
'smile-shard-00-01.xeyqd.mongodb.net:27017' => [ServerDescription],
'smile-shard-00-02.xeyqd.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
I have already update IP address to mongodb network access => IP Access list to 0.0.0.0/0
But the error has still occurred. I think this error is occurring due to the window firewall. But I have not any idea to resolve this.

Resources