MongoError:连接0到本地主机:27017超时

events.js:141扔呃; //未处理“错误”事件
MongoError:连接0到localhost:27017在Function.MongoError.create超时(/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11)
在sockets。 (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20)
在Socket.g(events.js:260:16)
在emitNone(events.js:67:13)
在Socket.emit(events.js:166:7)
在Socket._onTimeout(net.js:318:8)
在_runOnTimeout(timers.js:524:11)
在_makeTimerTimeout(timers.js:515:3)
在Timer.unrefTimeout(timers.js:584:5)

那么在连接时没有错误,但是当试图保存一些模型/集合时,它会运行一段时间,然后会抛出这个错误。 BTW我也有另一个节点进程连接到同一个MongoDB服务器。 任何帮助,高度赞赏。

您的查询需要很长时间。 而且mongo本身有一个默认的超时设置。 因此,如果查询花费的时间超过了超时时间,则超时。

var mongoose = require('mongoose'); var option = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }, replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } } }; var mongoURI = process.env.MONGODB_URI; mongoose.connect(mongoURI, option).then(function(){ //connected successfully }, function(err) { //err handle }); 

你必须在你的连接上使用这个configuration:keepAlive:300000,connectTimeoutMS:30000