最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mac: Node JS Mongo Database Error: Connection Refused - Stack Overflow

programmeradmin7浏览0评论

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 mand 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

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 mand 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
Share Improve this question edited Dec 15, 2018 at 22:14 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked May 30, 2016 at 14:34 L1ghtk3iraL1ghtk3ira 3,1999 gold badges38 silver badges70 bronze badges 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? – Andrei 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 mands. I am trying to run mands such as 'mongo' that I see online however kees saying 'mand not found'. I don't know if this helps but everything was downloaded via mand line using npm – L1ghtk3ira Commented May 30, 2016 at 15:30
  • Please execute the below mands 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 Commented May 30, 2016 at 15:30
  • I do not know how to open mongo shell, I have been using terminal – L1ghtk3ira 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./manual/tutorial/install-mongodb-on-os-x – notionquest Commented May 30, 2016 at 15:36
 |  Show 9 more ments

3 Answers 3

Reset to default 3

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

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 mand and mongo mand 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.

Type this mand in terminal:

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

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 mand to start mongo

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

发布评论

评论列表(0)

  1. 暂无评论