2012-08-13 74 views
2

我偶然發現大約貓鼬好奇的問題連接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) 

代碼段是:

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,並再次嘗試問題消失,一切正常,所以我想知道爲什麼?

+0

很難肯定地說,但我的猜測是另一個應用程序已經在使用的27017蒙戈的默認TCP端口在MongoDB服務器可以綁定到它之前。 – JohnnyHK 2012-08-13 04:49:07

+0

不,我用「mongo」命令行,沒關係,「show dbs」也沒問題 – clevertension 2012-08-13 05:43:45

+0

'netstat -bano'的輸出是什麼?您可以將其發佈到pastebin或類似文件中 – 2012-08-13 09:25:47

回答

1

mongoose.connect()不接受任何回調函數,你必須使用你的代碼貓鼬的ieyour代碼片段:

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

2

這是一個常見的問題時,在長時間運行的應用程序池連接返回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'); 
});