mongodb连接超时错误 [英] mongodb connection timed out error

查看:957
本文介绍了mongodb连接超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用mongodb数据库和node.js v12.0.10来连接和更新mongodb集合. 连接代码如下:

I have used mongodb database and node.js v12.0.10 for connecting and updating mongodb collection. connection code is the following:

 async.parallel({
        RE5: function (cb) {
            MongoClient.connect(config.riskEngineDB, function (err, r5DB) {
                cb(err, r5DB);
            })
        },
        MDB: function (cb) {
            MongoClient.connect(config.monitoringDB, function (err, mDB) {
                cb(err, mDB);
            })
        }
    },
    function (err, DBs) {
        assert.equal(null, err);
        console.log("Connected correctly to Dbs");
        // ..doing updates..

 })

运行一段时间后,脚本显示以下错误:

after some time running, script printed the following error:

  { [MongoError: connection 39 to 127.0.0.1:27017 timed out]
  name: 'MongoError',
  message: 'connection 39 to 127.0.0.1:27017 timed out' }

为您提供信息,我使用了mongodb连接的不同选项,但这没有任何意义.

For your information, I used different options of connections of mongodb but it didn't make sense.

推荐答案

我有类似的经历,因为查询花了太多时间来回复

I had a similar experience, due to a query which took too much time to reply

您在节点mongo驱动程序中有2个可配置的超时时间:

you have 2 configurable timeouts in node mongo driver:

connectTimeoutMSsocketTimeoutMS,均默认为30秒( http://mongodb.github.io/node-mongodb-native/2.2/reference/connecting/connection-settings/)

connectTimeoutMS and socketTimeoutMS, both defaulting to 30sec ( http://mongodb.github.io/node-mongodb-native/2.2/reference/connecting/connection-settings/ )

如果您的查询发送第一个结果所需的时间超过30秒,则它将以连接超时错误结束.

if your query takes longer than 30sec to send its first result, it'll end with a connection timed out error.

如果查询在两个结果之间花费的时间超过30秒,则可能由于池缩小而导致该连接关闭.

if your query takes more than 30sec between two results, it'll probably end with that connection closing due to pool shrinking.

您可能想增加超时时间,或者确保查询足够快(例如,创建索引).我建议加快查询速度,因为超时增加可能会降低性能.

You may want to increase timeouts, or make sure your query is fast enough ( create an index for example). I advise to speed up query, since increasing timeouts may have performances downsides.

这篇关于mongodb连接超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆