user18806528
user18806528

Reputation: 1

ERRCONNREFUSED when connecting Mongodb in node.js

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:\Users\lilpi\mongo\node_modules\mongoose\lib\error\mongooseError.js:7:1)
    at MongooseServerSelectionError (c:\Users\lilpi\mongo\node_modules\mongoose\lib\error\serverSelection.js:26:1)
    at Connection.openUri (c:\Users\lilpi\mongo\node_modules\mongoose\lib\connection.js:824:32)
    at <anonymous> (c:\Users\lilpi\mongo\node_modules\mongoose\lib\index.js:381:10)
    at <anonymous> (c:\Users\lilpi\mongo\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5)
    at promiseOrCallback (c:\Users\lilpi\mongo\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (c:\Users\lilpi\mongo\node_modules\mongoose\lib\index.js:1234:10)
    at Mongoose.connect (c:\Users\lilpi\mongo\node_modules\mongoose\lib\index.js:380:20)
    at <anonymous> (c:\Users\lilpi\mongo\app.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: mongodb 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.

Upvotes: 0

Views: 365

Answers (2)

Alisha Irshad
Alisha Irshad

Reputation: 64

Worked for me on MAC:

brew services restart mongodb/brew/mongodb-community@"yourmongoversion"

Upvotes: 0

robertklep
robertklep

Reputation: 203514

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:

mongoose.connect('mongodb://mydb.kngo05j.mongodb.net/mongotest')

You may also need to pass login credentials. How to do that is explained in the Mongoose documentation.

Upvotes: 0

Related Questions