MongoError: connect ECONNREFUSED 127.0.0.1:27017

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.


For windows - just go to Mongodb folder (ex : C:\ProgramFiles\MongoDB\Server\3.4\bin) and open cmd in the folder and type "mongod.exe --dbpath c:\data\db"

if c:\data\db folder doesn't exist then create it by yourself and run above command again.

All should work fine by now.))


My apps stopped working after upgrading from Nodejs 14 to 17.

The error I got was

MongoServerSelectionError: connect ECONNREFUSED ::1:27017

The solution was simply replacing localhost by 0.0.0.0. I.e. in my source code I had to change

const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);

to

const uri = "mongodb://0.0.0.0:27017/";
const client = new MongoClient(uri);

I am posting this as an answer because I couldn't find the solution anywhere on the Internet and I wasted a lot of time on this one...