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

29,185

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.

Share:
29,185
Deimyts
Author by

Deimyts

Updated on July 01, 2020

Comments

  • Deimyts
    Deimyts almost 4 years

    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.

  • Deimyts
    Deimyts over 10 years
    Did npm install mongoose, which ran without errors, but did not have an effect on the problem. Output from grep command: euterpe:benm me$ ps -ef | grep mongod 502 16818 13001 0 12:26AM ttys002 0:11.65 /usr/local/Cellar/mongodb/2.4.9/mongod --rest --config /usr/local/etc/mongod.conf 502 17373 6467 0 12:40AM ttys003 0:00.00 grep mongod
  • Neil Lunn
    Neil Lunn over 10 years
    try running something very simple in node connecting to mongo as suggested. Edit your question with the additional information you find. As it stands it's just a block of text. People will ignore you and vote the question down.
  • Deimyts
    Deimyts over 10 years
    Thanks - I have been experimenting with the sample file you linked to to try and get a better handle on the problem. Will update when I have more of a clue and can explain better. So far it is helping.
  • Deimyts
    Deimyts over 10 years
    solved. It was a pretty dumb mistake, but the file you linked to helped increase my understanding enough to find it. Thanks!