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
halfer
  • 19,824
  • 17
  • 99
  • 186
L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70
  • 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? – Andrei May 30 '16 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 – L1ghtk3ira May 30 '16 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. – notionquest May 30 '16 at 15:30
  • I do not know how to open mongo shell, I have been using terminal – L1ghtk3ira May 30 '16 at 15:31
  • Go to bin directory of your MongoDB installation. You should be able to see mongo.exe file ... – notionquest May 30 '16 at 15:33
  • I typed Mongo shell in search but does not exist. I did download via npm I do not know if that makes a difference. Appreciate the help – L1ghtk3ira May 30 '16 at 15:33
  • ah you never set up the local path of the variable by the sounds of it which is why in terminal if you run mongo it will say command not found – Andrei May 30 '16 at 15:36
  • I looked in node_modules -> Mongodb and there is no bin directory – L1ghtk3ira May 30 '16 at 15:36
  • 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. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ – notionquest May 30 '16 at 15:36
  • Tried, terminal says it does not know the command 'Brew' – L1ghtk3ira May 30 '16 at 15:37
  • you need to install brew first if you haven't yet and also add it to your global path. IF you want to install via brew there is another way to install manually if you read lower in the link provided by @L1ghtk3ira – Andrei May 30 '16 at 15:40
  • @NotionQuest I ran your script, it said syntax errors. I believe homebrew is now installed – L1ghtk3ira May 30 '16 at 15:43
  • Thank you for both of your help. I will make sure to leave a detailed answer on these steps as many other people might not have done these as well. – L1ghtk3ira May 30 '16 at 15:43
  • I tried installing homebrew here "https://coolestguidesontheplanet.com/installing-homebrew-os-x-yosemite-10-10-package-manager-unix-apps/" and yet the commands do not work. I get a different output then they provide ==> The following directories will be made group writable: /usr/local/. lists directories – L1ghtk3ira May 30 '16 at 15:45

3 Answers3

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.

L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70
Andrei
  • 1,196
  • 1
  • 11
  • 25
  • I will follows these instructions shortly and see how it goes. Thanks. – L1ghtk3ira May 30 '16 at 15:56
  • 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 – Andrei May 30 '16 at 16:03
  • I am stuck on this: https://docs.mongodb.com/master/tutorial/install-mongodb-on-os-x/?_ga=1.88454107.1570375696.1464621977. Step 4. I am giving it the path but it says does not exist – L1ghtk3ira May 30 '16 at 18:19
  • 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 – Andrei May 30 '16 at 18:23
  • Just says command not found. I ran it from within the bin folder for Mongo – L1ghtk3ira May 30 '16 at 19:04
  • So I believe I am almost there I think I am stuck on the path. I ran mongod as stated near the end and recieved an error message I am adding towards the bottom of my post. Thanks, – L1ghtk3ira May 31 '16 at 15:19
  • did you already have a mongodb instance running? that's what the error most likely seems to be. – Andrei May 31 '16 at 18:59
  • I do not think so. I was trying to run without specifying the path and that is the error message. I am trying to set the path like you described above and in the other post however it is not working. – L1ghtk3ira May 31 '16 at 20:04
2

Type this command in terminal:

mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
Ashwin J
  • 662
  • 7
  • 12
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 mongodb-community@3.6

  • once installation end it says: for start mongodb "brew services start mongodb/brew/mongodb-community@3.6" so use this command to start mongo

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

Marco Allori
  • 3,198
  • 33
  • 25