我用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连接那部分代码贴粗来
mongoose.createConnection(“mongodb://localhost:27017/mytest”, { “server”: { “poolSize”: 10, “socketOptions”: { “keepAlive”: 1 }, “auto_reconnect”: true } });
自己顶一下,是不是必须在linux下,才可以。我在Windows下开发的
首先你的先检查一下mongodb服务是否开启了,开启的端口号是否是27017
@xuyufei, connect-mongo和mongoDB 查看工具都可以连上
mongoose.connect 可以连上,但是mongoose.createConnection就不行
@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下 这种连接池的方式可用