[Error: failed to connect to [localhost:27017]] from NodeJS to mongodb
mongodb, node.js
Solution
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?
Problem
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 ```