'MongoDB - MongoError: connect ECONNREFUSED

I keep getting an error when trying to connect mongoDB. I know there are many questions similar to this one and I have checked all of them and haven't found a solution for my issue.

Here is the exact error:

connection error: { MongoError: connect ECONNREFUSED 127.0.0.1:21017 name: 'MongoError' message: connect ECONNREFUSED 127.0.0.1:21017

I looked at some other solutions and they say to adjust the mongo.conf file but I can't seem to find the file on my system and I've downloaded MongoDB.

Here is my full code:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
$ = require('cheerio');

/* GET home page. */


mongoose.connect('mongodb://localhost/');


var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log('Connected to database');
  // we're connected!
});


var pageInfo = {
    title: 'JotIT',
    owner: 'Emmanuel Obogbaimhe',
    message: 'Hi welcome to JotIT. A quick, simple and easy to use note taker.',
    date: Date.now(),
    age: 22
};

router.get('/', function(req, res, next) {
  res.render('index', { 
    title: pageInfo.title,
    author: pageInfo.owner,
    message: pageInfo.message,
    date: pageInfo.date,
    age: pageInfo.age
     });
});



module.exports = router;


Solution 1:[1]

The reason for getting that kind of error: {MongoError: connect ECONNREFUSED 127.0.0.1:21017}, is that Mongo process is not running on that PORT, or not running at all. For first check out if mongo process is running:

service mongod status //for Linux machine

For second - check the port of mongo process:

nmap -p- localhost //for Linux machine

Solution 2:[2]

For windows, open another terminal and cd into the apps root directory. Then, run $ mongod.exe. I would recommend placing the following into a test.js file:

var mongoose = require('mongoose');    
mongoose.connect('mongodb://localhost:27017/test.js');


var db = mongoose.connection;
db.on("error", function(error){
console.error("Connection error : " + error)
});
db.once('open', function() {
  console.log('Connected to database');
  db.close(function(){
        console.log("db connection closed");
  });
});

Go back to the original terminal and run $ node test.js

Solution 3:[3]

I had the same error, It looks like and bad closing in preview mongodb session I just did and It worked out fine

sudo service mongod stop
sudo service mongod start

Solution 4:[4]

Had the same problem, caused by running out of hard drive memory space. This caused mongod to keep crashing after restarting it.

Simply increasing memory space of the server, and restarting mongod (either manually or via reboot when service restarts automatically) solved the issue.

Solution 5:[5]

Try this

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
$ = require('cheerio');

/* GET home page. */


mongoose.connect('mongodb://localhost:27017/db-dev');

or try with

mongoose.connect('mongodb://0.0.0.0:27017/db-dev');

Solution 6:[6]

change your mongoose connection code to:

mongoose.connect('mongodb://localhost:27017/yourdb');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivan Matveev
Solution 2 Mosby42
Solution 3 gsalgadotoledo
Solution 4 Flep
Solution 5 Vignesh
Solution 6 Vimukthi