Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB setup with node.js: Error: failed to connect to [localhost:27017]

I am getting the error: "failed to connect to [localhost:27017]" while trying to follow this tutorial.

I've been attempting to do several other node.js tutorials with mongoDB over the past few days, and each one runs into similar problems. The other solutions I've found surrounding this all involve people not having the server running when trying to connect. I have mongod running in a terminal window, can view the info in the browser at localhost, and have added and viewed records to the database through the mongo shell. All of that appears to be working. The breakage seems to be in connecting any of the node.js projects to the database, but being pretty new to this, I am not even sure where to begin troubleshooting.

RESOLVED: very stupid mistake on my part. The file suggested by Neil below (view here was instrumental in helping me to troubleshoot and find the problem, though.

needed to change the line

mongoose.connect('mongodb://localhost/MyApp');

to

mongoose.connect('mongodb://127.0.0.1:27017/test');

in the server.js file.

Additional note: for reasons I don't quite understand, specifying the address as 127.0.0.1: works, but localhost: does not.

like image 267
Deimyts Avatar asked Feb 02 '14 06:02

Deimyts


1 Answers

Just for my curiousity, can you do

npm install mongoose

and also post the output from

ps -ef | grep mongod

All on the the machine you are running node from. That would go a long way towards the rest of an answer.

Also follow the simple sample code right after here, substitute the database name and a collection name and run that on node

node sample.js

Edit your post to include more information. It will all help.

like image 62
Neil Lunn Avatar answered Sep 28 '22 08:09

Neil Lunn