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

229,536

Solution 1

You have to install MongoDB database server first in your system and start it.

Use the below link to install MongoDB

https://docs.mongodb.com/manual/installation/

If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.

Solution 2

Many of them don't add this, especially in AWS EC2 Instance, I had the same issue and tried different solutions. Solution: one of my database URL inside the code was missing this parameter 'authSource', adding this worked for me.

mongodb://myUserName:[email protected]:27017/databaseName?authSource=admin

Solution 3

After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.

Removing this entry solved it for me, along with the correct installation of mongoDB from the link given in the above solution, and including the correct promise handling code:

mongoose.connect('mongodb://localhost/testdb').then(() => {
console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

Solution 4

I faced same issue but after a lot of RND. I found that whts the problem so run this command on your terminal.

sudo service mongod start

then run mongo on terminal

Solution 5

You're IP address probably changed.

If you've recently restarted your modem, this changes your IP which was probably whitelisted on Atlas.

Soooo, you'll need to jump back onto Atlas and add your new IP address to the whitelist under Security>Network Access.

Share:
229,536
Karam Haj
Author by

Karam Haj

Updated on July 05, 2022

Comments

  • Karam Haj
    Karam Haj 6 months

    I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,

    mongo.connect("mongodb://localhost:27017/mydb")
    

    when I run my code on cmd (node start-app), get the following error,

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

    Could someone explain me which step I missed ? my code :

    var express = require("express");
    var MongoClient = require('mongodb');
    var url = "mongodb://localhost:27017/mydb";
    var webService = require("./webService");
    var server = express();
    MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        console.log("Database created!");
        db.close();
    });
    server.use(express.urlencoded({ extended: true }));
    server.set('views', __dirname);
    server.get('/', function (request, response) {
        response.sendFile(__dirname + '/MainPage.html');
    });
    server.get('/Sign', function (request, response) {
        response.render(__dirname + '/Sign.ejs');
    });
    server.post("/signUp", webService.signUp);
    server.post("/createUser", webService.createUser);
    server.listen(5500);
    
  • Ren44 over 3 years
    THIS SAVED MY LIFE... been trying to figure this out for days now. that ?authSource=admin is what did it for me!!!!!
  • mepley over 3 years
    If you've whitelisted your IP address under Security > Network Access > IP Whitelist and then try to connect from a different IP address, Mongo will block the connection. It's a security feature to ensure only trusted IPs can connect to the database. You just need to go to the aforementioned section of the Mongo DB Atlas site and either add your current IP address (permanent or temporary) to the whitelist or temporarily allow access from anywhere (i.e. 0.0.0.0/0 for all IP addresses). It is not recommended that you permanently allow access from anywhere for obvious security reasons.
  • Mohammad Reza Shahrestani
    Mohammad Reza Shahrestani over 3 years
    Your code is very similar to stackoverflow.com/a/50955506/6174449 , if it's not a duplicate explain a little above your code.
  • hongsy
    hongsy almost 3 years
    This does not provide an answer to the question. Note that there is already an accepted answer to this question. Please edit your answer to ensure that it improves upon other answers already present in this question.
  • Logemann over 2 years
    why do you write every word uppercase on first letter? This is quite exhausting to read.
  • Labham Jain over 2 years
    Oh! :-( But I'm Comfortable With Uppercase
  • Logemann over 2 years
    stackoverflow.com/help/how-to-ask --> "Spelling, grammar and punctuation are important! Remember, this is the first part of your question others will see - you want to make a good impression. If you're not comfortable writing in English, ask a friend to proof-read it for you."
  • Admin
    Admin about 2 years
    This is the meaning of authSource