mongoose: Unable to connect Atlas Mongodb instance
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.9.1
Node.js version
19.7.0
MongoDB server version
5.0.15
Typescript version (if applicable)
No response
Description
I am having a problem connecting Atlas Mongodb instance, don’t know if the problem is with mongoose or Atlas Mongodb instance, the problem is all my NodeJs application which are using Atlas MongoDB instance stop connecting to the database.
I remembered one of the application worked perfectly fine on 24th of Feb, but when I tried to run the same application again on 16 March 2023 it showed me an error given below
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 Connection.openUri (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/connection.js:825:32) at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:411:10 at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5 at new Promise (<anonymous>) at promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10) at Mongoose._promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:1285:10) at Mongoose.connect (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:410:20) at connectDB (/Users/aviralgupta/Development/NodeJs/MongoDB/config/dbConfig.js:8:24) at Object.<anonymous> (/Users/aviralgupta/Development/NodeJs/MongoDB/index.js:12:1) at Module._compile (node:internal/modules/cjs/loader:1275:14) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'ac-a6to6lu-shard-00-00.nfmpr49.mongodb.net:27017' => [ServerDescription], 'ac-a6to6lu-shard-00-01.nfmpr49.mongodb.net:27017' => [ServerDescription], 'ac-a6to6lu-shard-00-02.nfmpr49.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'atlas-tvdhx4-shard-0', maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined } Error in dbConfig.js
As it mentions that you're trying to access the database from an IP that isn't whitelistedI tried to add my current IP address (even though access from anywhere IP: 0.0.0.0/0 was already present ) but still shows the same problem.
Steps to Reproduce
index.js
const mongoose = require("mongoose");
const app = express();
const PORT = 3001;
// Change this to your Atlas Mongodb credentials
const MONGO_URL="<Your Atlas MongoDB URL>";
const connectDB = async () => {
try {
await mongoose.connect(MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
} catch (error) {
console.error(error);
console.log("Unable to connect DB");
}
}
connectDB();
mongoose.connection.once("open", () => {
console.log("DB connected");
app.listen(PORT, () => {
console.log("Server Started");
});
})
package.json
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.0.2"
}
}
Make sure to download express and mongoose package
Expected Behavior
It should be able to connect Atlas mongodb Instance.
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 22 (4 by maintainers)
@Aviral-Gupta101 either way, this isn’t the place to report issues connecting to Atlas with VS Code.
@lorand-horvath I’m able to connect to Atlas even without
?retryWrites=true&w=majority.I tried to chat to an Atlas support person, they weren’t much help unfortunately!
I am having the same issue. I have been trying to connect to mongo atlas with mongoose all day but get the same error as the OP.
I am using next.js built in api routes, but that uses Express.js like OP.