16

I am having problem connecting to MongoDB from NodeJS using following sample code. I tried running "mongod" with or without sudo but nodejs code still fail to connect. I am able to successfully connect to database using "mongo".

Running on : MAC OS 10.6.8

   var mongoClient = require('mongodb').MongoClient;
   mongoClient.connect("mongodb://localhost:27017/test", function(error, db) {
        if(!error){
             console.log("We are connected");
        }
        else
           console.dir(error);
    });

get following error running above code : [Error: failed to connect to [localhost:27017]]

Also tried mongoose but similar error:

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
          console.log("DB connected");
    // yay!
    });

Output: connection error: [Error: failed to connect to [localhost:27017]]

Here is the log of mongod

mongod --help for help and startup options
2014-07-11T23:33:47.843-0700 kern.sched unavailable
2014-07-11T23:33:47.849-0700 [initandlisten] MongoDB starting : pid=29942 port=27017 dbpath=/data/db 64-bit host=usc8bcc8a0d0b1
2014-07-11T23:33:47.849-0700 [initandlisten]
2014-07-11T23:33:47.849-0700 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2014-07-11T23:33:47.849-0700 [initandlisten] db version v2.6.3
2014-07-11T23:33:47.849-0700 [initandlisten] git version: nogitversion
2014-07-11T23:33:47.849-0700 [initandlisten] build info: Darwin usc8bcc8a0d0b1 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
2014-07-11T23:33:47.849-0700 [initandlisten] allocator: system
2014-07-11T23:33:47.849-0700 [initandlisten] options: {}
2014-07-11T23:33:47.850-0700 [initandlisten] journal dir=/data/db/journal
2014-07-11T23:33:47.850-0700 [initandlisten] recover : no journal files present, no recovery needed
2014-07-11T23:33:47.901-0700 [initandlisten] waiting for connections on port 27017
2014-07-11T23:34:47.901-0700 [clientcursormon] mem (MB) res:48 virt:2810
2014-07-11T23:34:47.901-0700 [clientcursormon]  mapped (incl journal view):320
2014-07-11T23:34:47.901-0700 [clientcursormon]  connections:0
user2812866
  • 589
  • 1
  • 5
  • 15
  • Does this answer your question? [Can't connect to MongoDB 6.0 Server locally using Nodejs driver](https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver) – Wernfried Domscheit May 24 '23 at 05:48

5 Answers5

22

Never mind, I was able to resolve the issue by using 127.0.0.1 instead of localhost, not sure why I have to use ip address. My tomcat, apache, redis, and even node server all works using localhost but not the mongodb. Is there is config I need change to make it work using local host?

user2812866
  • 589
  • 1
  • 5
  • 15
  • YES! I used 127.0.0.1 in my apache config, which was previously localhost and it connected instantly. Thank you! – Andy Nov 27 '16 at 06:01
7

Try adding 127.0.0.1 localhost in /private/etc/hosts file.

ani627
  • 5,578
  • 8
  • 39
  • 45
2
Community
  • 1
  • 1
Chhavi Gangwal
  • 1,166
  • 9
  • 13
1

yes , I came across this error too, and my hosts is as follow: 127.0.0.1 xxxxxx(the ip of my computer) and when I ran npm start in express project, it got an error like that . After I tried to change the mapping of 127.0.0.1 to localhost, it had no error.

  • It should be the other way around. Format of `/etc/hosts` file is `IPAddress Hostname Alias`. "the ip of my computer" is the (real) IP address, it should not be the alias. Typically you have `127.0.0.1 localhost` and ` ` – Wernfried Domscheit May 24 '23 at 06:13
-1

Somebody on Github found a solution: Instead of writing:

Mongoose.connect(config.database.url);

Write:

Mongoose.connect(config.database.url, {
  keepAlive: true,
  reconnectTries: Number.MAX_VALUE,
  useMongoClient: true
});

(Source https://github.com/Automattic/mongoose/issues/5399)

Mr Washington
  • 1,295
  • 14
  • 15