猫鼬输出错误“错误:连接已关闭"; [英] mongoose output the error "Error: connection closed"

查看:99
本文介绍了猫鼬输出错误“错误:连接已关闭";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了关于mongoose连接mongodb的一个奇怪的问题,它产生了如下的详细错误

I stumble upon a curious problem about mongoose connect the mongodb, it generate the detail errors as the following

    e:\Mentor_Resources\node\node_twitter_bootstrap>node app
Express server listening on port 3000
Trace: error occure when start to connect dbError: connection closed
    at e:\Mentor_Resources\node\node_twitter_bootstrap\server\module\word.js:14:
17
    at Connection.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_mod
ules\mongoose\lib\connection.js:201:5)
    at Db.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_modules\mon
goose\node_modules\mongodb\lib\mongodb\db.js:247:16)
    at Server.connect.connectionPool.on.server._serverState (e:\Mentor_Resources
\node\node_twitter_bootstrap\node_modules\mongoose\node_modules\mongodb\lib\mong
odb\connection\server.js:413:7)
    at EventEmitter.emit (events.js:115:20)
    at connection.on.connectionStatus (e:\Mentor_Resources\node\node_twitter_boo
tstrap\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connect
ion_pool.js:108:15)
    at EventEmitter.emit (events.js:91:17)
    at Socket.closeHandler (e:\Mentor_Resources\node\node_twitter_bootstrap\node
_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:401:
12)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket._destroy.destroyed (net.js:364:10)

猫鼬的代码段是:

var mongoose = require('mongoose');

mongoose.connect("mongodb://localhost/word-sentence",function(err) {
    if(err)
        console.trace('error occure when start to connect db' + err);
});

我确定mongodb已打开,并且我多次重启了mongodb,但是错误仍然存​​在,因此我重新启动了Windows XP,然后重试该问题消失了,一切都很好,所以我想知道为什么?

i am sure the mongodb is open, and i restart mongodb for several times,but the error is still exist, so I reboot my Windows XP , and try again the problem disappear,everything is ok, so I want to know why?

推荐答案

在运行时间更长的应用程序中的池连接返回connection closed时,这是一个常见问题.

This is a common problem when pooled connections in longer running applications return connection closed.

猫鼬文档建议将keepAlive添加到传递给功能.

The mongoose documentation recommends adding keepAlive to the options object you pass into the connect function.

这是一个示例(如果您不使用它,可以删除replset)

Here's an example (you can remove replset if you're not using this),

// include keep alive for closing connections,
// http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
var mongoOptions =
{
    db: {safe: true},
    server: {
        socketOptions: {
            keepAlive: 1
        }
    },
    replset: {
        rs_name: 'myReplSet',
        socketOptions: {
            keepAlive: 1
        }
    }
};

mongoose.connect( YOUR_URI, mongoOptions );

mongoose.connection.on('error', function(err) {
    console.log('Mongo Error:\n');
    console.log(err);
}).on('open', function() {
    console.log('Connection opened');
});

这篇关于猫鼬输出错误“错误:连接已关闭";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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