简体   繁体   English

Mac:Node JS Mongo 数据库错误:连接被拒绝

[英]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 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:我已使用此处找到的命令进行了验证,该命令返回 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:我试图运行的 javascript 文件如下:

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将端口号从 27017 更改为其他
  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.我是 Node JS 和 Mongo DB 的新手,所以尝试调试这有点困难。 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.我在 StackOverflow 内外都尝试了其他相关帖子中的许多内容,但似乎无法弄清楚为什么我会收到此错误。

I do have the following installed as I read Mongo DB requires them:当我阅读 Mongo DB 需要它们时,我确实安装了以下内容:

  1. mongodb-core mongodb-核心
  2. bson布森
  3. kerberos kerberos
  4. node-gyp节点-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.我从一个来源中读到,我找不到可能发生这种情况的链接,这是因为一些关键问题,例如未关闭数据库连接、没有数据库、Mongo 未运行以及其他一些问题。

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您对 NPM 上可用的 Mongodb 驱动程序和可以在 mongodb.com 上安装的 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.您在他们的网站上安装的 mongodb 服务用于创建一个 mongodb 实例,该实例将实际保留和存储您的数据记录。 If you have a background with RDBMS it's the same as having localdb/expressdb.如果您有 RDBMS 的背景,它与拥有 localdb/expressdb 是一样的。

Install the mongodb service they offer via the brew package manager - similar to NPM but supports different libraries.通过 brew 包管理器安装他们提供的 mongodb 服务 - 类似于 NPM,但支持不同的库。 Install brew first and then follow the instructions on the Mongodb website there is a link there located at http://brew.sh/ .首先安装 brew,然后按照 Mongodb 网站上的说明进行操作,那里有一个位于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.一旦安装了 MongoDB 实例并设置了路径变量,您就可以在打开任何终端窗口后运行 mongodb 命令和 mongo 命令。

This will then start your mongodb service这将启动您的 mongodb 服务

brew services start mongodb

This will then end your mongodb service这将结束您的 mongodb 服务

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.您从 NPM 安装的 mongodb 只是一个公共 api/驱动程序,它允许您与您安装的服务进行交互。 You must have a terminal open that is running mongodb while running your node project while connecting to it.在连接到节点项目时,您必须打开一个运行 mongodb 的终端。

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.注意:(关于您已安装的软件包。)如果您使用 mongodb,则不需要 mongoose - mongoose 构建在 mongodb 之上,它强制执行模式内容并且体积更大。 I suggest just sticking with mongodb first before adding another layer to learn.我建议先坚持使用 mongodb,然后再添加另一层来学习。

with YOSEMITE 10.10.5使用优胜美地 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;首先卸载任何 mongodb 版本......阅读文档我安装了当前的 mongodb 版本(4.2),这在 YOSEMITE 中不受支持;
  • install version 3.6安装 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一旦安装结束它说:启动mongodb“brew services start mongodb/brew/mongodb-community@3.6”所以使用这个命令来启动mongo

and finally mongodb starts and connection from node.js it works!最后 mongodb 启动并从 node.js 连接它工作了!

在终端中键入此命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM