title: Mac: Node JS Mongo Database Error: Connection Refused
body: <p>I am following a tutorial found <a href="http://blog.modulus.io/mongodb-tutorial" rel="nofollow noreferrer">here</a>. However I am getting the following error:</p>
  
  <pre><code>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' }
  </code></pre>
  
  <p>I have verified using the command found <a href="https://stackoverflow.com/questions/31561098/how-to-check-if-mongo-db-is-running-on-mac">here</a> which returned 1:</p>
  
  <pre><code>ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' '
  </code></pre>
  
  <p>I have also ran </p>
  
  <pre><code>ps -ef | grep mongod
  </code></pre>
  
  <p>And received the following:</p>
  
  <pre><code>0  4165   418   0 Fri12pm ttys000    0:00.03 sudo mongod
  501  6165   418   0  9:54am ttys000    0:00.00 grep mongod
  </code></pre>
  
  <p>My javascript file I am trying to run is as follows:</p>
  
  <pre><code>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);
  
  });
  </code></pre>
  
  <p>Things I have tried: </p>
  
  <ol>
  <li>Restarting the service</li>
  <li>Changing the port number from 27017 to others</li>
  <li>Taking out code so it is bare bones to verify</li>
  <li><a href="https://stackoverflow.com/questions/31542990/nodejs-application-mongodb-connection-fails-with-error-econnrefused">This links information</a></li>
  <li><a href="https://stackoverflow.com/questions/20386464/econnrefused-error-when-connecting-to-mongodb-from-node-js">This links information</a></li>
  <li><a href="https://stackoverflow.com/questions/21507761/mongodb-setup-with-node-js-error-failed-to-connect-to-localhost27017">This links information</a></li>
  </ol>
  
  <p>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.</p>
  
  <p>I do have the following installed as I read Mongo DB requires them:</p>
  
  <ol>
  <li>mongodb-core</li>
  <li>bson</li>
  <li>kerberos</li>
  <li>node-gyp</li>
  </ol>
  
  <p>As well as :</p>
  
  <ol>
  <li>Mongoose</li>
  <li>Mongo</li>
  <li>Node</li>
  </ol>
  
  <p>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. </p>
  
  <p>Entering Mongod:</p>
  
  <pre><code>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
  </code></pre>