Reputation: 115
I am having an issue which I tried to fix for a long time.
I am trying to connect to Mongo Atlas cloud from nodejs
viamongoose
.
It's not my first time but I just can't find the answer.
Mongoose version: 5.9.22
Here is my code:
const express = require('express')
const mongoose = require('mongoose')
const bodyparser = require('body-parser')
const app = express()
app.use(bodyparser.json())
mongoose.connect("mongodb+srv://dudvil1:[email protected]/shopping_list?
retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true
})
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('mongo connected'))
.catch(err => console.log(err));
const port = process.env.PORT || 5000
app.listen(port, () => console.log(`server started in port ${port}`))
My whitelist settings include only 0.0.0.0/0 My username and password in the Database access is very simple and without any special characters, but always get the same 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 (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\connection.js:826:32)
at Mongoose.connect (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\index.js:335:15)
at Object.<anonymous> (C:\Users\Dudu\Desktop\MERN\server.js:10:10)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
message: "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/",
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'cluster0-shard-00-02.guxmm.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-00.guxmm.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.guxmm.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
Thanks a lot.
Upvotes: 6
Views: 27479
Reputation: 9
so thanks, that helped! So something really happened with my provider, and he started assigning me a dynamic IP address
Upvotes: 0
Reputation: 823
If you are using it for learning or testing purposes, you can select the "Allow Access from Anywhere" option. It's quick and easy.
Upvotes: 1
Reputation: 41
I had the same problem. don't add your ip address from your terminal or network preference. Instead...
Go to MongoDb Network Access, Then click edit on the ip address, or add Ip address if you haven't added any.
Click add the current IP address then click confirm... vuala you're set.
Upvotes: 0
Reputation: 1439
I had the same problem with my strapi app, I solved the problem adding 2 options in my database config:
'AUTHENTICATION_DATABASE', null
'DATABASE_SSL', true
In strapi database.js:
options: {
authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
ssl: env.bool('DATABASE_SSL', true),
},
Upvotes: 1
Reputation: 71
use current IP address 1)Go to network access on mongodb 2)select current Ip ADDRESS
Upvotes: 0
Reputation: 326
You can try the following steps:
This way you will be able to access your database from any IP address.
Let me know if it works out for you :)
Upvotes: 26