admin管理员组

文章数量:1289690

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 wele 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;

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 wele 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;
Share Improve this question edited Sep 22, 2017 at 17:57 CommunityBot 11 silver badge asked Jun 1, 2016 at 19:03 user6377738user6377738 4
  • 1 Mongo default port is 27017, you change port to 21017? try mongoose.connect('mongodb://localhost:27017/'); or mongoose.connect('mongodb://localhost:21017/'); – Jorge Hess Commented Jun 1, 2016 at 19:16
  • Mongoose connect method use default Mongo port (as You sad - 21017) when specific port is not passed. You can see it from error message (it's trying to connect to 127.0.0.1:21017). – Ivan Matveev Commented Jun 1, 2016 at 20:03
  • Yes Ive tried both ways but still getting the same error – user6377738 Commented Jun 1, 2016 at 22:47
  • Do I have to adjust something with the configurations? – user6377738 Commented Jun 1, 2016 at 23:50
Add a ment  | 

7 Answers 7

Reset to default 0

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 remend 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 stop
sudo service mongod start

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.

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');

change your mongoose connection code to:

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

This error can be solved by replacing localhost in the following code with the address of localhost which is 127.0.0.1. mongoose.connect('mongodb://localhost/');
Try this -> mongoose.connect('mongodb://127.0.0.1/');

本文标签: javascriptMongoDBMongoError connect ECONNREFUSEDStack Overflow