I've searched the Internet but couldn't find an answer for my problem. It seems specific to my application, but hopefully not :)
This error occurs both during heroku deployment as well as during dokku deployment.
But to the point.
I have the app.js file which part responsible for db connection looks like this:
/**
* DB connection
*/
var dbUrl;
if(app.get('env') === 'development'){
dbUrl = process.env.MONGOLAB_URI || "mongodb://localhost:27017/test";;
}
var db = require('./database');
db.connect(dbUrl, function(err){
if(err) {
console.log('Unable to connect to MongoDB');
} else {
console.log('MongoDB connected successfully!');
}
});
I'm working on development environment only, so I've removed the production bits.
As you can see, I'm getting dbUrl from heroku environment variables, then I'm requiring db index file which looks like this:
var MongoClient = require('mongodb').MongoClient;
var state = {
db: null
};
exports.connect = function(url, done) {
if(state.db) return done();
console.log("trying to connect to mongodb");
console.log(url);
MongoClient.connect(url, function(err, db){
console.log("mongodb callback");
if(err) return done(err);
state.db = db;
done();
});
};
And than I'm invoking the db.connect method in the app.js file (first code snippet) from the database index file (just above).
And I'm getting this error:
Starting process with command
NODE_ENV=development node server/bin/www
mongodb://user:password@ds011111.mongolab.com:33333/db_name
trying to connect to mongodb <<---- this is console.log from my db.connect method
/app/server/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; }) Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) State changed from starting to crashed
The error occurs only during heroku or dokku deployment.
My db connection is 100% correct (all the environment variables are ok as well so please don't ask about it), checked it in shell, as well as connecting to it from my localhost within the app.
Basically it works on my localhost, I've deployed it on vps with ubuntu, end it works as well. The problems occurs only when I'm trying to use automation tools.
Any help greatly appreciated
Thanks!