[英]Connection Timeout for mongodb using mongoose
I have a web application running on Node, express and MongoDB.我有一个在 Node、express 和 MongoDB 上运行的 Web 应用程序。 I use mongoose as the ODM.
我使用猫鼬作为 ODM。 When i tested my application with mongodb version v3.0.1 it runs fine and throws no errors.
当我使用 mongodb 版本 v3.0.1 测试我的应用程序时,它运行良好并且不会引发任何错误。 But when i run the same code v3.2.10 i get a connection timeout after some time.
但是当我运行相同的代码 v3.2.10 时,我会在一段时间后出现连接超时。
I get the following Error :我收到以下错误:
Error: connection timeout at null.<anonymous> (/webapp/node_module/mongoose/lib/drivers/node-mongodb-native/connection.js:186:17)
I use mongoose.connect for the db connection to the local mongodb instance.我使用 mongoose.connect 连接到本地 mongodb 实例的数据库。 Has anything changed in the way of connection ?
连接方式有什么变化吗?
I had this problem a while ago.不久前我遇到了这个问题。 It all depends on which version of
mongoose
and mongodb-core
you are using.这完全取决于您使用的是哪个版本的
mongoose
和mongodb-core
。 Right now, you have to specify the following parameters:现在,您必须指定以下参数:
mongoose.connect("mongodb://user:password@address/db", {
server: {
socketOptions: {
socketTimeoutMS: 0,
connectionTimeout: 0
}
}
});
However, just yesterday, the correct parameters where然而,就在昨天,正确的参数在哪里
mongoose.connect("mongodb://user:password@address/db", {
server: {
socketOptions: {
socketTimeoutMS: 0,
connectTimeoutMS: 0
}
}
});
I don't really know what to believe in anymore..我真的不知道该相信什么了。。
I realize that this is an old question, but the accepted answer now contains deprecated code.我意识到这是一个老问题,但现在接受的答案包含已弃用的代码。 To set a connection timeout with modern versions of Mongoose, you now need to do:
要使用现代版本的 Mongoose 设置连接超时,您现在需要执行以下操作:
mongoose.connect(uri, {
useUnifiedTopology: true,
serverSelectionTimeoutMS: 1000,
})
socketTimeoutMS
and connectionTimeout
only work when useUnifiedTopology
is set to false
. socketTimeoutMS
和connectionTimeout
仅在useUnifiedTopology
设置为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.