MongoDB - MongoError: connect ECONNREFUSED MongoDB - MongoError: connect ECONNREFUSED express express

MongoDB - MongoError: connect ECONNREFUSED


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


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


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 stopsudo service mongod start