[Solved] MongoError: connect ECONNREFUSED

I’m using NodeJS wih MongoDB using mongodb package. When I run mongod command it works fine and gives “waiting for connection on port 27017”. So, mongod seems to be working. But MongoClient does not work and gives error when I run node index.js command-

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED]

enter image description here

I have install mongo db 3.4 and my code is-

var MongoClient = require('mongodb').MongoClient;
var dburl       =   "mongodb://localhost:27017/test";
MongoClient.connect(dburl, function(err, db) {
  if (err) {
    throw err;
  console.log('db connected');

I have created data/db directories on root and given write permissions.
mongod.conf file takes db path as-

dbPath: /var/lib/mongo

But it seems that it is actually taking db path as data/db and not var/lib/mongo

It working earlier but suddenly stopped.

Solution #1:

This happened probably because the MongoDB service isn’t started. Follow the below steps to start it:

  1. Go to Control Panel and click on Administrative Tools.
  2. Double click on Services. A new window opens up.
  3. Search MongoDB.exe. Right click on it and select Start.

The server will start. Now execute npm start again and the code might work this time.

Respondent: Sonu Sourav

Solution #2:

For windows – just go to Mongodb folder
(ex : C:ProgramFilesMongoDBServer3.4in) and open cmd in the folder and type
“mongod.exe –dbpath c:datadb”

if c:datadb folder doesn’t exist then create it by yourself and run above command again.

All should work fine by now.))

Respondent: Farid Ansari

Solution #3:

I’ve forgot to start MongoDB at all.
Go to separate tab in terminal and type:

sudo service mongod start
Respondent: matrixb0ss

Solution #4:

Try to start mongoDB server by giving the –dbpath with mongod.

sudo mongod --dbpath /var/lib/mongo/data/db &

‘&’ in the last will start the mongodb server as service on your server.

Hope it Works.

Respondent: Nitin Bhanderi

Solution #5:

I had below error:

Error: connect ECONNREFUSED

In my case, this issue occurred since MongoDB is not installed at all.

The solution was to install MongoDB from below location:

After installing the ECONNREFUSED error did not appear again.

Hope that helps.

Solution #6:

For Windows users.

This happens because the MondgDB server is stopped.
to fix this you can,

  1. 1 search for Services ,for that search using the left bottom corner
    search option.

    2 then there is a gear icon named with ‘Services’.

    3 after clicking that you will get a window with lot of services

    4 scroll down and you will find ‘MongoDB server (MongoDB)’.

    5 so you will see that there is an option to start the service.

    6 start it.

  2. 1 then open your command prompt.

    2 type mongod

now you can connect to your server.

this steps also working fine if your sql server service down. (find sql server services)

Respondent: HeshanHH

Solution #7:

I had the same error because i had not installed mongoDB.
Make sure that you have mongodb installed and if not, you can download it from here

Solution #8:

Press CTRL+ALT+DELETE/task manager , then go to services , find MongoDB , right click on it. start. go back to terminal . npm start it will work.

Respondent: Doniyor Boltayev

Solution #9:

For Ubuntu users run
sudo systemctl restart mongod

Respondent: C.K

Solution #10:

For Windows users:
Mongo version 4.4
Use following commands:
NET STOP MONGODB – To stop MongoDB as a service,if this returns “mongoDb service is not running then use below command to start service”
NET START MONGODB – To start MongoDB as a service.

This worked for me.

Respondent: Nizar Kadri

Solution #11:

Search services.msc in your device and click start to run the mongoDB server

enter image description here

After started running MongoDB server you will able to run your program

Respondent: lahiru dilshan

Solution #12:

I was facing the same issue on windows 10. I just uninstall MongoDB and installed it again and it started working. It solved my problem.

Respondent: yogesh patel

Solution #13:

If you already installed “MongoDB”, if you accidentally exit from the MongoDB server, then “restart your system”.

This method solved me…

[On Windows only]

And also another method is there:


Windows + R



and click “ok”, it opens
“services” window, and then search for “MongoDB Server” in the list.
After you find “MongoDB Server”, right-click and choose “start” from the pop-up menu.

The MongoDb Server will start running.

Hope it works!!

Respondent: nitheesh

Solution #14:

most probably your mongo db service is not started.
For ubuntu : sudo service mongod start
For windows : go to services and start the MongoDB service

also install link for mondoDB service

I had this problem and i solved it thanks to this man’s answer

Respondent: Farrukh

Solution #15:

I was having the same problem today.. and has been searching for answers.. I am on Ubuntu… however, I could not find the correct one that works on this thread.. after much research the following worked for me finally!! 🙂

First, after running

mongod dbpath

if appeared that mongodb was looking for the data/db directory.. which was missing in my installed mongodb app.. so I ran the following commands:

$ sudo mkdir -p /data/db

then run,

$ sudo chown -R $USER /data/db

chown – changes ownership of files/dirs. Ie. owner of the file/dir changes to the specified one, but it doesn’t modify permissions. As detailed here:

Finally, run

`$ sudo systemctl enable mongod.service

It will give a message: Created symlink /etc/systemd/system/ ? /lib/systemd/system/mongod.service and started the service.

Respondent: Richard Rosario

Solution #16:

just start the mongo server from terminal

Ubuntu –

     sudo systemctl start mongod
Respondent: Sahil

Solution #17:

On a mac I had to run brew services start [email protected]

Check: Install MongoDB Community Edition on macOS

Respondent: Fotis Tsakiris

Solution #18:

I had the same issue on Windows.

Ctrl+C to close mongod, and start mongod again.

Am not sure, but it seems that initially mongod was working opening and closing connections. Then, it was not able to close some earlier connections and reached limit for opened ones. After restarting mongod, it works again.

Respondent: Novichok

Solution #19:

You probably need to continue running your DB process (by running mongod) while running your node server.

Respondent: otoloye

Solution #20:

Please ensure that your mongo DB is set Automatic and running at Control Panel/Administrative Tools/Services like below. That way you wont have to start mongod.exe manually each time.

enter image description here

Respondent: wise king

Solution #21:

The solution was to install MongoDB from below location:

Respondent: Anonymous Unknown

Solution #22:

because you didn’t start mongod process before you try starting mongo shell.

Start mongod server


Open another terminal window

Start mongo shell

Respondent: Mohit Rakhade

Solution #23:

In my particular instance, I was connected to a VPN and that was blocking the localhost connection to MongoDB somehow. It worked when I disconnected my VPN. It’s a special case, but good to know nonetheless. So anything impeding your network connectivity, Get that fixed.

Solution #24:

If you are a windows user, right click on Start and open Windows Powershell(Run as administartor). Then type


That’s it.
Your database will surely get connected!

Respondent: Arunesh Naha

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy