L1ghtk3ira L1ghtk3ira - 4 months ago 34x
Node.js Question

Mac: Node JS Mongo Database Error: Connection Refused

I am following a tutorial found here. However I am getting the following error:

Unable to connect to the mongoDB server. Error: { [MongoError: connect ECONNREFUSED]
name: 'MongoError',
message: 'connect ECONNREFUSED' }

I have verified using the command found here which returned 1:

ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' '

I have also ran

ps -ef | grep mongod

And received the following:

0 4165 418 0 Fri12pm ttys000 0:00.03 sudo mongod
501 6165 418 0 9:54am ttys000 0:00.00 grep mongod

My javascript file I am trying to run is as follows:

var fs = require('fs');
var http = require("https");
var express = require('express');
var app = express();
var path = require('path');
var http = require("http");
var url = require("url");
var req = require('request')
var pem = require('pem');
var cors = require("cors");
var mongodb = require('mongodb').MongoClient;
var url = 'mongodb://';

// Use connect method to connect to the Server
mongodb.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);

// do some work here with the database.

//Close connection

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

app.use(express.static(path.join(__dirname, '../')));
app.listen(process.env.PORT || 8080);
app.options('*', cors());

app.all('/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");

app.get('/', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHead(200, {'Content-Type': 'text/plain'});
contents = fs.readFileSync("sliderImages.json", "utf8");
console.log(path.join(__dirname, '/sliderImages.json'));


Things I have tried:

  1. Restarting the service

  2. Changing the port number from 27017 to others

  3. Taking out code so it is bare bones to verify

  4. This links information

  5. This links information

  6. This links information

I am fairly new to Node JS and Mongo DB so trying to debug this is a bit difficult. I have tried many things from other related posts both on and off StackOverflow but cannot seem to figure out why I am getting this error.

I do have the following installed as I read Mongo DB requires them:

  1. mongodb-core

  2. bson

  3. kerberos

  4. node-gyp

As well as :

  1. Mongoose

  2. Mongo

  3. Node

I am hoping posting my code perhaps a better developer who knows these well can hopefully explain to me as well as similar developers who are having this issue have a better understanding of what exactly is going on and why this problem is occurring. I have read from a source I cannot find the link to that this can occur because of a few key issues such as not closing the database connection, no database, mongo not running and a few others.

Any ideas on how to fix this is greatly appreciated. The more well explained answer to help myself as well as future developers with this issue such as why they are usually caused and why this one was caused is greatly appreciated.

Note: Please do not down vote as duplicate when the other possibilities from other posts were implemented and did not fix the issue.

Entering Mongod:

2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] MongoDB starting : pid=10385 port=27017 dbpath=/data/db 64-bit host=RBCs-MacBook- Pro-3.local
2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] db version v3.2.6
2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] git version: 05552b562c7a0b3143a729aaa0838e558dc49b25
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] allocator: system
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] modules: none
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] build environment:
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] distarch: x86_64
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] options: {}
2016-05-31T11:18:44.937-0400 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-05-31T11:18:44.937-0400 W - [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2016-05-31T11:18:44.937-0400 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] dbexit: rc: 100


You are a little confused about the Mongodb driver available on NPM and the Mongodb service which you can install on mongodb.com

The mongodb service you installed off the their website is used to create a mongodb instance which will actually keep and store the records of your data. If you have a background with RDBMS it's the same as having localdb/expressdb.

Install the mongodb service they offer via the brew package manager - similar to NPM but supports different libraries. Install brew first and then follow the instructions on the Mongodb website there is a link there located at http://brew.sh/.

Once you have a MongoDB instance installed and the path variable set you will be able to run mongodb command and mongo command after opening any terminal window.

This will then start your mongodb service

brew services start mongodb

This will then end your mongodb service

brew services end mongodb

The mongodb you installed from NPM is just a public api/driver that allows you to interact with the service you have installed. You must have a terminal open that is running mongodb while running your node project while connecting to it.

Note: (about the package you have installed.) You do not need mongoose if your using mongodb - mongoose is built on top of mongodb that enforces schema stuff and is much more bulky. I suggest just sticking with mongodb first before adding another layer to learn.