I am trying to set up MongoDB with Node.js and I keep getting the following error:
Uncaught MongooseServerSelectionError MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at MongooseError (c:Userslilpimongonode_modulesmongooseliberrormongooseError.js:7:1)
at MongooseServerSelectionError (c:Userslilpimongonode_modulesmongooseliberrorserverSelection.js:26:1)
at Connection.openUri (c:Userslilpimongonode_modulesmongooselibconnection.js:824:32)
at <anonymous> (c:Userslilpimongonode_modulesmongooselibindex.js:381:10)
at <anonymous> (c:Userslilpimongonode_modulesmongooselibhelperspromiseOrCallback.js:41:5)
at promiseOrCallback (c:Userslilpimongonode_modulesmongooselibhelperspromiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (c:Userslilpimongonode_modulesmongooselibindex.js:1234:10)
at Mongoose.connect (c:Userslilpimongonode_modulesmongooselibindex.js:380:20)
at <anonymous> (c:Userslilpimongoapp.js:3:10)
at Module._compile (internal/modules/cjs/loader:1126:14)
at Module._extensions..js (internal/modules/cjs/loader:1180:10)
at Module.load (internal/modules/cjs/loader:1004:32)
at Module._load (internal/modules/cjs/loader:839:12)
at executeUserEntryPoint (internal/modules/run_main:81:12)
at <anonymous> (internal/main/run_main_module:17:47)
Here is my code:
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/mongotest')
Here is my database connection:
This is my first time attempting to use MongoDB and I am very new to node.js and programming as a whole, so the solution is probably very obvious.
2
Answers
Your code is trying to connect to a MongoDB server running on
localhost
(which is your local machine), but your screenshot is showing an entirely different hostname.So make sure you’re actually using the correct hostname:
You may also need to pass login credentials. How to do that is explained in the Mongoose documentation.
Worked for me on MAC: