mongoose: Database connection failed after update mongoose from 8.6.4 to 8.7.0 or higher
Prerequisites
- I have written a descriptive issue title
Mongoose version
8.8.0
Node.js version
18.15.0
MongoDB version
7.0.15
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Debian 4.19.0-24-cloud-amd64
Issue
When I update mongoose to version 8.7.0 or higher, I have a database connection error on MongoDb Atlas. If I go back to mongoose version 8.6.4 everything works fine The problem is the same from Windows (dev) or Debian Linux (production) on AWS EC2 and on OVH VPS. The IPs are in the whitelist in mongodb atlas network access.
I did some tests by changing some options (like serverSelectionTimeoutMS) but it didn’t change the error.
Log:
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://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (/home/admin/dcare2-cloud/node_modules/mongoose/lib/connection.js:909:11)
at NativeConnection.openUri (/home/admin/dcare2-cloud/node_modules/mongoose/lib/connection.js:860:11) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'cluster0-shard-00-01.*******************.net:27017' => [ServerDescription],
'cluster0-shard-00-02.*******************.net:27017' => [ServerDescription],
'cluster0-shard-00-00.*******************.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-vzvxq2-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
The code for connection:
// dbUrl is defined like this on env file:
dbUrl:
"mongodb+srv://USER:PASSWORD@cluster0.*******************.net/myDB?retryWrites=true&w=majority",
module.exports = {
connection: async function (callback) {
const options = {
sanitizeFilter: true,
socketTimeoutMS: 300000, //300s (default = 0)
//serverSelectionTimeoutMS: 10000, //10s (default 30s)
minPoolSize: 10, //(default 5)
maxPoolSize: 30, //(default 100)
};
mongoose
.connect(env.dbUrl, options)
.then(() => {
console.info("Database connection ok");
callback(null);
})
.catch((error) => {
console.error("Database connection failed!");
console.error("Error: ", error);
callback(error);
});
mongoose.connection.once("open", function () {
console.info("MongoDB event open");
//console.log("MongoDB connected to [%s]", env.dbUrl);
});
mongoose.connection.on("connected", function () {
console.info("MongoDB event connected");
});
mongoose.connection.on("disconnected", function () {
console.warn("MongoDB event disconnected");
});
mongoose.connection.on("reconnected", function () {
console.info("MongoDB event reconnected");
});
mongoose.connection.on("error", function (err) {
console.error("MongoDB event error: " + err);
});
},
disconnection: async function (callback) {
mongoose
.disconnect()
.then(() => {
console.info("Database disconnection ok");
callback(null);
})
.catch((error) => {
console.error("Database disconnection failed!");
console.error("Error: ", error);
callback(error);
});
},
getStats: async function () {
return await mongoose.connection.db.stats();
},
};
Thank you for your help
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17
I encountered the same issue. I upgraded Mongoose from
^8.6.1to^8.8.3, but the service is unable to connect to the database consistently.I am using a serverless MongoDB Atlas database, deployed on Azure. The issue can be reproduced locally by attempting to connect to the database using Mongoose. The error log is as follows, and I hope this provides some help:
I have a similar issue. I am getting intermittent connection errors with Mongoose 8.7.0 and higher.
I have a multi-tenant design and use the following function to switch db’s and register the model(s) required:
With 8.7.0 the readyState comes in as “0” on the new db. With 8.6.4 it always came in a “1”. Not sure if this is connected to why it is suddenly having issues.
The frustrating part is that it seems random and intermittent. Some times the model can fetch without issue. Other times it fails with a “Operation ‘model.findOne()’ buffering timed out after 10000ms” error.