Can't connect to MongoDB via NodeJS, what's going on?

1,055 views
Skip to first unread message

Thijs Koerselman

unread,
Sep 26, 2012, 12:36:13 PM9/26/12
to nod...@googlegroups.com
Hi, 

I have mongodb installed locally on my osx laptop and on a remote ubuntu server. Both have mongodb running and I can verify this using the http diagnostics on port 28017. I'm running the same code on both computers. On osx everything works fine, but on Ubuntu I can't make a connection to the database through NodeJS. I keep getting this error:

Error: failed to connect to [localhost:27017]]
message:  listen EADDRNOTAVAIL
stack:  Error: listen EADDRNOTAVAIL
    at errnoException (net.js:769:11)
    at Server._listen2 (net.js:892:19)
    at listen (net.js:936:10)
    at Server.listen (net.js:993:9)
    at asyncCallback (dns.js:67:16)
    at Object.onanswer [as oncomplete] (dns.js:120:9)

What I don't understand is that I can connect on Ubuntu locally via the mongo commandline interface. I can also connect to the database on Ubuntu via the mongo command on my OSX computer. So nothing seems to be wrong with the installation of MongoDB itself. 

Can anyone think of a reason why I can't connect via NodeJS? I have tried using the mongodb and mongoose packages. Both give me the same error.

Here are the 2 ways I tried:

var mongo = require("mongodb");
var host = "localhost";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db('node-mongo-examples', new mongo.Server(host, port, {}), {});
db.open(function(err, db){
if(err){
log.error('MongoDB connection error:', err);
}else{
log.info("OPEN MONGO CONNECTION");
}
});


And the with mongoose:

var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'node-mongo-examples');

db.on('error', function(err){
  log.error('MongoDB connection error:', err);
});

db.once('open', function () {
  log.debug("OPEN MONGO CONNECTION");
});



In the logs I see nothing special, and nothing happens either

***** SERVER RESTARTED *****

Wed Sep 26 18:00:18 [initandlisten] MongoDB starting : pid=13377 port=27017 dbpath=/var/lib/mongodb 64-bit host=octo-dev
Wed Sep 26 18:00:18 [initandlisten] db version v2.2.0, pdfile version 4.5
Wed Sep 26 18:00:18 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Wed Sep 26 18:00:18 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Wed Sep 26 18:00:18 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Wed Sep 26 18:00:18 [initandlisten] journal dir=/var/lib/mongodb/journal
Wed Sep 26 18:00:18 [initandlisten] recover : no journal files present, no recovery needed
Wed Sep 26 18:00:18 [websvr] admin web console waiting for connections on port 28017
Wed Sep 26 18:00:18 [initandlisten] waiting for connections on port 27017


..... except when I connect through the mongo commandline interface:

Wed Sep 26 18:30:40 [initandlisten] connection accepted from 127.0.0.1:38229 #3 (1 connection now open)

I ran out of things to try. Anyone?

Cheers,
Thijs


Thijs Koerselman

unread,
Sep 27, 2012, 2:29:17 AM9/27/12
to nod...@googlegroups.com
Second installation of Ubuntu went without problems. I still have not idea what might be causing this on that particular machine, but I'll leave it for now.

Thijs Koerselman

unread,
Sep 27, 2012, 5:35:38 AM9/27/12
to nod...@googlegroups.com
On Thu, Sep 27, 2012 at 8:29 AM, Thijs Koerselman <thijsko...@gmail.com> wrote:
Second installation of Ubuntu went without problems. I still have not idea what might be causing this on that particular machine, but I'll leave it for now.



It turned out to be a messed up configuration of /etc/hosts. Something about the localhost settings that was unusual. I corrected the file and everything works now. 
Reply all
Reply to author
Forward
0 new messages