2

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 127.0.0.1:27017]
name: 'MongoError',
message: 'connect ECONNREFUSED 127.0.0.1:27017' }

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://127.0.0.1:27017/Rewards';

// 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
  db.close();
  }
});

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");
  next();
});

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");
  next();
});

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'));
 res.end(contents);

});

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 posting my code; perhaps a better developer who knows these well can explain to me 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.

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
14
  • so while running a mongod instance in the cli and while running in another cli node index.js you get the connection err still? have you tried connected to the mongodb instance via mongo cli and querying on it? Commented May 30, 2016 at 15:26
  • I am sorry I am a bit confused. I believe mongoDb is running i have posted what was brought back from 2 commands. I am trying to run commands such as 'mongo' that I see online however kees saying 'command not found'. I don't know if this helps but everything was downloaded via command line using npm Commented May 30, 2016 at 15:30
  • Please execute the below commands in mongo shell and see whether you are getting expected results. db.runCommand( { whatsmyuri : 1 } ) db.runCommand( { ping : 1 } ) show dbs mongo 127.0.0.1:27017/Rewards Your JavaScript looks fine. I am able to connect to mongo using that. Commented May 30, 2016 at 15:30
  • I do not know how to open mongo shell, I have been using terminal Commented May 30, 2016 at 15:31
  • 1
    Have you installed your MongoDB as per the instructions given in the below link? The one you installed via npm should be mongo client lib. docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x Commented May 30, 2016 at 15:36

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I will follows these instructions shortly and see how it goes. Thanks.
hope it goes well :). Just make sure to follow all the steps in the instructions in the mongodb website, skipping a single step could mess you up. also another possibility is to use [mongolab](www.mlab.com) - sign up it's free and they give you a remote sandbox to use for the mongodb. Just create a database and create a user with a username/password and copy the link for the uri and remember to change the username@password to your user you created <- if you are just trying to get up quick and play around not locally
I am stuck on this: docs.mongodb.com/master/tutorial/install-mongodb-on-os-x/…. Step 4. I am giving it the path but it says does not exist
try following the instructions I added in the answer in the third paragraph called HERE its a link to another SO answer that might help
Just says command not found. I ran it from within the bin folder for Mongo
|
2

Type this command in terminal:

mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork

Comments

0

with YOSEMITE 10.10.5

I had the same problem and I solved in this way.

  • first of all uninstall any mongodb version... reading doc I was installed current mongodb version (4.2) and this is not supported in YOSEMITE;
  • install version 3.6

brew install [email protected]

  • once installation end it says: for start mongodb "brew services start mongodb/brew/[email protected]" so use this command to start mongo

and finally mongodb starts and connection from node.js it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.