我用connect-mongo持久化Session没问题,使用mongoose连接MongoDB的时候,报以下错误
events.js:72 throw er; // Unhandled ‘error’ event ^ Error: failed to connect to [localhost:27017] at null.<anonymous> (D:\Program Files\nodejs\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:74) at EventEmitter.emit (events.js:106:17) at null.<anonymous> (D:\Program Files\nodejs\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:15) at EventEmitter.emit (events.js:98:17) at Socket.<anonymous> (D:\Program Files\nodejs\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:10) at Socket.EventEmitter.emit (events.js:95:17) at net.js:441:14 at process._tickCallback (node.js:415:13) at Function.Module.runMain (module.js:499:11) at startup (node.js:119:16)
mongoose.createConnection("mongodb://localhost:27017/mytest", { "server": { "poolSize": 10, "socketOptions": { "keepAlive": 1 }, "auto_reconnect": true } });
@nonkey mongoose连接应该这样的吧 …
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mytest');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// yay!
});
@superRaytin mongoose.createConnection("mongodb://localhost:27017/mytest", { "server": { "poolSize": 10, "socketOptions": { "keepAlive": 1 }, "auto_reconnect": true } }); 我在Ubuntu下 这种连接池的方式可用