简体   繁体   English

MongoDB Atlas 连接偶尔会从 Lambda 失败

[英]MongoDB Atlas connection occasionally fails from Lambda

I have an AWS Lambda that connects to Atlas through Mongoose.我有一个通过 Mongoose 连接到 Atlas 的 AWS Lambda。

It works fine on my dev environment (using a M0 Sandbox) but in prod, where I have an M2, every few requests fails with a MongooseServerSelectionError when I do the lambda's first query.它在我的开发环境(使用 M0 沙箱)上运行良好,但在生产环境中,我有一个 M2,当我执行 lambda 的第一个查询时,每隔几个请求都会失败并显示MongooseServerSelectionError It appears this only happens on (some) reused DB connections, never from a cold start.这似乎只发生在(一些)重用的数据库连接上,而不是冷启动。

My Network Access IP range is set to 0.0.0.0, so I don't think whitelisting is an issue.我的网络访问 IP 范围设置为 0.0.0.0,所以我认为白名单不是问题。

For what its worth, these are my connection options:就其价值而言,这些是我的连接选项:

conn = mongoose.createConnection(process.env.MONGO, {
      bufferCommands: false,
      bufferMaxEntries: 0,
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      socketTimeoutMS:4000,
      serverSelectionTimeoutMS: 1000
    });

Curious if anyone has run into a similar problem before?想知道是否有人遇到过类似的问题?

I am not sure if this is the answer you are looking for but it is recommended that you initialize your db connection outside your function context.我不确定这是否是您正在寻找的答案,但建议您在函数上下文之外初始化数据库连接。 this might be of use to you.这可能对你有用。 mongodb atlas documentation mongodb 图集文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM