Mongodb 连接在 2-3 小时后断开。
网络访问 - 0.0.0.0/0
我的后端应用程序在 API 网关顶部的 AWS Lamba 上运行。它是一个具有 20 多个端点的单一应用程序。
我正在尝试从那里连接 mongo - 前 2-3 小时工作正常。然后我收到以下错误。 -
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
}
当我从 Ec2 运行相同的代码时,它工作正常。
我如何连接 mongo -
const mongoose = require('mongoose');
const config = require('config');
const chalk = require('chalk');
// Mongodb ServerUrl
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;
你找到解决办法了吗?因为我几乎遇到了 San 问题