node.jsmongodbherokumongoosemlab

Connection error on Heroku + MongoLab addon with Node.js app - Error: connect ECONNREFUSED 127.0.0.1:27017


When trying to start Node.js app hosted on Heroku with MongoLab addons, I got the following error on Heroku logs when connecting to MongoDB. It looks like it is trying to connect to a local mongo even though I already set the heroku environment variable.

My entire code is here on GitHub: https://github.com/yhagio/meetup_planner

My gist for this issue

Heroku logs

2015-11-28T22:22:58.460531+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T22:22:59.353911+00:00 app[web.1]: Server started at port number:  38701
2015-11-28T22:22:59.303795+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:22:59.371719+00:00 app[web.1]:
2015-11-28T22:22:59.371723+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:22:59.371725+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T22:22:59.371725+00:00 app[web.1]:                                       ^
2015-11-28T22:22:59.371729+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:22:59.371730+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:22:59.371731+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:22:59.371732+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.052255+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:00.880672+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:23:00.939388+00:00 app[web.1]:
2015-11-28T22:23:00.939392+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:23:00.939393+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T22:23:00.939397+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:23:00.939394+00:00 app[web.1]:                                       ^
2015-11-28T22:23:00.939398+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:23:00.939399+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:23:00.939400+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.917276+00:00 app[web.1]: Server started at port number:  42573
2015-11-28T22:23:01.626965+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T22:23:01.614846+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:13.224552+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=warm-retreat-5113.herokuapp.com request_id=54404d8a-7744-49ae-b744-4417abd4b347 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
2015-11-28T22:23:13.581322+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=warm-retreat-5113.herokuapp.com request_id=063c0fef-8260-46b5-a0f6-b4d546e9a60b fwd="166.62.227.154" dyno= connect= service= status=503 bytes=

Here is the code where I making a connection to MongoDB (MongoLab addons from Heroku) server/db_connection.js

var mongoose  = require('mongoose');
var config    = require('../express-config');
var userModel = require('./models/user');
var eventModel = require('./models/event');

function makeDefaultConnection() {
  var uri = process.env.MONGOLAB_URI || '127.0.0.1/meetupplanner';
  console.log('Connecting to DB : ', uri);

  var conn = mongoose.createConnection(uri);

  conn.on('error', function(err){
    console.log('Connection Error ::: ', err);
  });

  conn.model('User', userModel.userSchema);
  conn.model('Event', eventModel.eventSchema);
  return conn;
}

module.exports.defaultConnection = makeDefaultConnection();

Profile

web: node server.js

In Server.js

...
var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server started at port number: ', port);
});

After I added MongoLab Add-ons on Heroku I verified that my MONGOLAB_URI is set on Heroku (Settings > Config Variables)

Reference: https://devcenter.heroku.com/articles/mongolab

Similar issues asked on StackOverflow:

Note that I could connect to mongo shell via:

$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD

I think credentials are fine.


Solution

  • I finally figured out. It was the middleware configuration of connect-mongo with session.

    In my config, it was:

    app.use(session({
      ...
    
      // ===== Following 3 lines are trying to connect to local db =====
      store: new MongoStore({    
        'db': 'meetupplanner'
      }),
    
      // ===== So, I replaced 3 lines above with following =====
      store: new MongoStore({
        url: process.env.MONGOLAB_URI
      }),
    
      ...