[Fixed] MongoError: connect ECONNREFUSED 127.0.0.1:27017 – Node.js

Photo of author
Written By M Ibrahim
alpine-linux mongodb node.js

Quick Fix: Replace 'localhost' with '0.0.0.0' in your MongoDB connection URI to fix the ECONNREFUSED error. This is necessary because Node.js 17 uses IPv6 by default, which may not be configured on your system.

The Problem:

A developer is using Node.js with MongoDB and encounters a "MongoError: connect ECONNREFUSED 127.0.0.1:27017" error while attempting to connect to a MongoDB database. The mongod command works correctly, but MongoClient fails to connect, indicating a connection refusal on port 27017. The developer has set up the necessary directories and permissions, and the issue seems to have arisen suddenly.

The Solutions:

Solution 1: Use 0.0.0.0. instead of localhost

To resolve this issue, change the “localhost” value in the MongoDB connection string to “0.0.0.0”. This allows the database to accept connections from any IP address, not just the local machine.

For example, instead of:

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

Use:

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

Solution 2: Starting MongoDB service

The issue arises due to MongoDB service being inactive. To resolve this problem, follow the following steps:

  1. Press Windows + R to open the Run window. Type services.msc and hit Enter to open the Services window.
  2. Locate the MongoDB.exe entry in the services list. Right-click on it and select Start.

This will start the MongoDB service, allowing your code to connect to it successfully.

Solution 3: Check if MongoDB is Running

In this case, the issue was that MongoDB was not running. To start MongoDB, follow these steps:

  1. Open a new terminal tab.
  2. Type the following command:
sudo service mongod start
  1. This will start the MongoDB service.

  2. Now, try running your Node.js script again. It should connect to the MongoDB server without any errors.

Solution 4: Run MongoDB server

To resolve this issue, you need to ensure that the MongoDB server is running before attempting to connect to it from your Node.js application.

Here are the steps to start the MongoDB server:

  1. Open the Run dialog box by pressing Windows Key + R.
  2. Type services.msc and press Enter.
  3. In the Services window, locate the MongoDB service.
  4. Right-click on the service and select Start.

Once the MongoDB server is running, you can try connecting to it from your Node.js application again.

Solution 5: Using Mongod Command

To resolve the error “MongoError: connect ECONNREFUSED 127.0.0.1:27017” in a Windows environment, follow these steps:

  1. Navigate to the MongoDB bin directory. Typically, this is located at
    C:\Program Files\MongoDB\Server\3.4\bin.
  2. Open a command prompt from within the bin directory.
  3. Execute the following command:

    mongod.exe –dbpath c:\data\db
  4. If the “c:\data\db” folder does not exist, create it manually.
  5. Run the command again after creating the folder.

This should resolve the connection issue, and MongoDB should start running properly. You can then access it from your NodeJS application using the MongoClient.