我的代码
var mongoose = require('mongoose');
for(let i = 0;i<600;i++)
{
let db1 = mongoose.createConnection('mongodb://127.0.0.1:27017/abc');
db1.on('error', function(error) {
console.log("error = "+(i)+" "+error +db1);
});
db1.on('close', function() {
console.log("close = "+(i)+" "+db1);
});
db1.once('open', function() {
"use strict";
// db1.close();
});
}
我想测试mongodb,结果是
error = 364 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 365 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 385 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object].......
另一个问题是连接是否需要关闭? 感谢。
答案 0 :(得分:5)
确保在运行Node应用程序的同一台计算机的端口27017上运行Mongo。要验证这一点,请在命令行中运行:
mongo localhost:27017
您不需要关闭连接并多次打开它。您应该在应用中打开一次连接,并仅在您要关闭应用时将其关闭。有关更多数据,请参阅这些答案: