title: [Error: failed to connect to [localhost:27017]] from NodeJS to mongodb
body: <p>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".           </p>
  
  <p>Running on  : MAC OS 10.6.8</p>
  
  <pre><code>   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);
      });
  </code></pre>
  
  <p>get following error running above code : [Error: failed to connect to [localhost:27017]]</p>
  
  <p>Also tried mongoose but similar error:</p>
  
  <pre><code>    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!
      });
  </code></pre>
  
  <p>Output:   connection error: [Error: failed to connect to [localhost:27017]]</p>
  
  <p>Here is the log of mongod</p>
  
  <pre><code>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
  </code></pre>