Mongodb connection diconnected after 2-3 hrs.
My backend application is running on AWS Lamba on the top of API gateway. Its a monoletic application which has 20+ endpoints.
Network access - 0.0.0.0/0 (includes your current IP address)
I am trying to connect mongo from there - First 2-3 hrs it is working fine. Then I am getting this below error. -
ab64f03c-c8f9-43fb-8cb0-20197289717f INFO 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 (/var/task/node_modules/mongoose/lib/connection.js:791:11)
at NativeConnection.openUri (/var/task/node_modules/mongoose/lib/connection.js:766:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /var/task/db.js:12:7 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-4ugleph-shard-00-00.btwmhhb.mongodb.net:27017' => [ServerDescription],
'ac-4ugleph-shard-00-01.btwmhhb.mongodb.net:27017' => [ServerDescription],
'ac-4ugleph-shard-00-02.btwmhhb.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-xsqkyc-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
When I ran the same code from Ec2, it is working fine.
How I am connecting mongo -
const mongoose = require('mongoose');
const config = require('config');
const chalk = require('chalk');
// Mongodb ServerUrl
const connectUrl = 'const connectUrl = mongodb+srv://abbakid:<password>@abbakid-dev.erpttih.mongodb.net/Abbakid?retryWrites=true&w=majority';
(async () => {
try {
await mongoose.connect(connectUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log(chalk.yellow('MongoDB connected...', connectUrl));
} catch (err) {
console.log(err);
console.log(chalk.red('Error in DB connection: ' + err));
}
})();
module.exports = mongoose;
Any idea what I am doing wrong.