I'm trying to set up the MongoDB auto reconnection feature via Mongoose. Every way that I have tried to pass the option has had no effect, or at least the reconnected
event isn't being emitted.
What I've tried:
mongoose.createConnection("mongodb://localhost:27017/test", { auto_reconnect: true }); mongoose.createConnection("mongodb://localhost:27017/test", { autoReconnect: true }); mongoose.createConnection("mongodb://localhost:27017/test", { server: { auto_reconnect: true } }); mongoose.createConnection("mongodb://localhost:27017/test", { server: { autoReconnect: true } });
If one of these is correct, the reconnected
event should be triggered and a message should be logged in the console, however this never happens.
If there is a delay before the reconnection, does anyone know how to configure it?
Thanks in advance
For anyone looking into this, take a look at this and this issue in mongoose repository.
You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.
According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.
If initial connection fails, Mongoose will emit an 'error' event and the promise mongoose. connect() returns will reject. However, Mongoose will not automatically try to reconnect.
serverSelectionTimeoutMS - With useUnifiedTopology , the MongoDB driver will try to find a server to send any given operation to, and keep retrying for serverSelectionTimeoutMS milliseconds. If not set, the MongoDB driver defaults to using 30000 (30 seconds).
I had the same question as you, and robertklep's solution didn't work for me either. I found when MongoDB service is stopped, an error event is triggered, but the connection.readyState is still 1 (connected). That may be why it didn't auto reconnect.
This is what I have now:
var db = mongoose.connection; db.on('connecting', function() { console.log('connecting to MongoDB...'); }); db.on('error', function(error) { console.error('Error in MongoDb connection: ' + error); mongoose.disconnect(); }); db.on('connected', function() { console.log('MongoDB connected!'); }); db.once('open', function() { console.log('MongoDB connection opened!'); }); db.on('reconnected', function () { console.log('MongoDB reconnected!'); }); db.on('disconnected', function() { console.log('MongoDB disconnected!'); mongoose.connect(dbURI, {server:{auto_reconnect:true}}); }); mongoose.connect(dbURI, {server:{auto_reconnect:true}});
Plucked from http://bites.goodeggs.com/posts/reconnecting-to-mongodb-when-mongoose-connect-fails-at-startup/
This worked for me:
var mongoose = require('mongoose') var mongoUrl = "mongodb://localhost:27017/test" var connectWithRetry = function() { return mongoose.connect(mongoUrl, function(err) { if (err) { console.error('Failed to connect to mongo on startup - retrying in 5 sec', err); setTimeout(connectWithRetry, 5000); } }); }; connectWithRetry();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With