node.js MongoError:连接 0 到 localhost:27017 超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41394850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoError: connection 0 to localhost:27017 timed out
提问by Prata
events.js:141
throw er; // Unhandled 'error' event
MongoError: connection 0 to localhost:27017 timed out
at Function.MongoError.create (/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11)
at Socket.<anonymous> (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20)
at Socket.g (events.js:260:16)
at emitNone (events.js:67:13)
at Socket.emit (events.js:166:7)
at Socket._onTimeout (net.js:318:8)
at _runOnTimeout (timers.js:524:11)
at _makeTimerTimeout (timers.js:515:3)
at Timer.unrefTimeout (timers.js:584:5)
Well there is no error during connection, but when try to save some models/collections it runs for a while and then it throws this error. BTW I also have another node process connected to the same mongodb server. Any help is highly appreciated.
好吧,连接过程中没有错误,但是当尝试保存一些模型/集合时,它会运行一段时间,然后抛出此错误。顺便说一句,我还有另一个节点进程连接到同一个 mongodb 服务器。任何帮助都受到高度赞赏。
回答by Prata
const mongoose = require('mongoose');
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
const mongoURI = process.env.MONGODB_URI;
mongoose.connect(mongoURI, option).then(function(){
//connected successfully
}, function(err) {
//err handle
});
回答by Shreya Batra
Your query is taking a long time. And mongo itself has a default time out set. So it times out, if the query takes longer than the timeout time.
您的查询需要很长时间。并且 mongo 本身有一个默认的超时设置。所以它超时,如果查询花费的时间超过超时时间。
回答by Lucas Assmann
you have to use this configs on your connection: keepAlive: 300000, connectTimeoutMS: 30000
你必须在你的连接上使用这个配置:keepAlive: 300000, connectTimeoutMS: 30000
回答by Daniel Staleiny
If you are sure that you exported port -p 27017:2017and it still doesn't work.
如果您确定您导出了端口-p 27017:2017,但它仍然不起作用。
Check your VPN if it is not blocking Local Network sharing.
检查您的 VPN,如果它没有阻止本地网络共享。

