【问题标题】:mongoose output the error "Error: connection closed"猫鼬输出错误“错误:连接关闭”
【发布时间】:2012-08-09 07:39:38
【问题描述】:

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

    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)

mongoose的sn-p代码是:

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,然后再试一次问题消失了,一切正常,所以我想知道为什么?

【问题讨论】:

  • 很难确定,但我的猜测是在 MongoDB 服务器绑定到它之前,另一个应用程序已经在使用 Mongo 的默认 TCP 端口 27017。
  • 不,我用命令行“mongo”,没关系,“show dbs”也可以
  • netstat -bano 的输出是什么?您可以将其发布在 pastebin 或类似的地方
  • 你能告诉我们你的猫鼬的版本吗?
  • 查看答案,如果您仍然面临同样的问题,请告诉我。请向我们展示 MongoDB 的日志文件,看看是否有任何其他问题会引发导致您的连接中断的问题。 ..

标签: node.js mongodb mongoose


【解决方案1】:

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

猫鼬documentation 建议将keepAlive 添加到您传递给connect 函数的选项对象中。

这是一个例子(如果你不使用这个,你可以删除replset),

// 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');
});

【讨论】:

    【解决方案2】:

    mongoose.connect() 不接受您在代码中使用的任何回调函数,即您的代码 sn-p of mongoose:

    var mongoose = require('mongoose');
    
    mongoose.connect("mongodb://localhost/word-sentence",function(err) {
        if(err)
            console.trace('error occurred, when attempted to connect db. Error: ' + err);
    });
    

    所以,我建议你从这个开始:

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/word-sentence');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
      // yay connected!
    });
    

    同样,您不能期望来自db.open('open', function cb(){}) 的回调中有任何参数

    我建议仔细阅读这些quick startmongoose docs - 如果在文档和mongodb/mongoose close connection 中发现一些冲突,请了解如何跳转到源代码

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      相关资源
      最近更新 更多