'Can't connect to MongoDB Atlas Cluster
I am trying to connect to mongoDB, this is the code that I am using:
const mongoose = require('mongoose');
const db = 'mongodb://<username>:<password>@xxxx-uaglu.mongodb.net/<dbname>?retryWrites=true&w=majority'
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log('MongoDB connected...');
} catch (err) {
console.error(err.message);
process.exit(1);
}
}
module.exports = connectDB;
In security added 0.0.0.0/0 to IP whitelist.
I keep getting the following error 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.
Does anyone know what I can do? This is just a personal project but it's frustrating that I can't move ahead with it.
Solution 1:[1]
I have solved your same problem changing the user database password with a password like name.number in the database access in the security section. Then copying that password in the MongoURI
Solution 2:[2]
in your database URI You got to replace with username that you created on mongodb Atlas same goes with the and . For example if your credentials are:- username: jonDoe, password: test@abcd, database name: myBlogs
mongodb://jonDoe:test@[email protected]/myBlogs?retryWrites=true&w=majority.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roberto Coscia |
| Solution 2 | user14831698 |
