简体   繁体   中英

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.

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.

just start mongo server or run on mongo.exe and type

mongod

then

mongo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM