node.js MongoError:连接 ECONNREFUSED 127.0.0.1:27017

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46523321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:30:49  来源:igfitidea点击:

MongoError: connect ECONNREFUSED 127.0.0.1:27017

node.jsmongodb

提问by Deepika P.

I'm using NodeJS wih MongoDB using mongodb package. When I run mongod command it works fine and gives "waiting for connection on port 27017". So, mongod seems to be working. But MongoClient does not work and gives error when I run node index.js command-

我在使用 mongodb 包的 MongoDB 中使用 NodeJS。当我运行 mongod 命令时,它工作正常并给出“正在等待端口 27017 上的连接”。所以,mongod 似乎在工作。但是当我运行 node index.js 命令时 MongoClient 不起作用并给出错误 -

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

enter image description here

在此处输入图片说明

I have install mongo db 3.4 and my code is-

我已经安装了 mongo db 3.4,我的代码是-

var MongoClient = require('mongodb').MongoClient;
var dburl       =   "mongodb://localhost:27017/test";
MongoClient.connect(dburl, function(err, db) {
  if (err) {
    throw err;
  }
  console.log('db connected');
  db.close();
});

I have created data/db directories on root and given write permissions. mongod.conf file takes db path as-

我已经在 root 上创建了 data/db 目录并授予了写权限。mongod.conf 文件将数据库路径作为-

storage: dbPath: /var/lib/mongo

存储:dbPath:/var/lib/mongo

But it seems that it is actually taking db path as data/db and not var/lib/mongo

但似乎它实际上是将 db 路径作为 data/db 而不是 var/lib/mongo

It working earlier but suddenly stopped.

它早些时候工作但突然停止。

回答by SONU SOURAV

This happened probably because the MongoDBservice isn't started. Follow the below steps to start it:

发生这种情况可能是因为MongoDB服务未启动。请按照以下步骤启动它:

  1. Go to Control Panel and click on Administrative Tools.
  2. Double click on Services. A new window opens up.
  3. Search MongoDB.exe. Right click on it and select Start.
  1. 转到控制面板并单击管理工具
  2. 双击服务。一个新窗口打开。
  3. 搜索MongoDB.exe。右键单击它并选择Start

The server will start. Now execute npm startagain and the code might work this time.

服务器将启动。现在npm start再次执行,这次代码可能会工作。

回答by Nitin Bhanderi

Try to start mongoDB server by giving the --dbpath with mongod.

尝试通过使用 mongod 提供 --dbpath 来启动 mongoDB 服务器。

sudo mongod --dbpath /var/lib/mongo/data/db &

'&' in the last will start the mongodb server as service on your server.

最后的“&”将启动 mongodb 服务器作为您服务器上的服务。

Hope it Works.

希望它有效。

回答by Farid Ansari

For windows - just go to Mongodb folder (ex : C:\ProgramFiles\MongoDB\Server\3.4\bin) and open cmd in the folder and type "mongod.exe --dbpath c:\data\db"

对于 Windows - 只需转到 Mongodb 文件夹(例如:C:\ProgramFiles\MongoDB\Server\3.4\bin)并在文件夹中打开 cmd 并输入“mongod.exe --dbpath c:\data\db”

if c:\data\db folder doesn't exist then create it by yourself and run above command again.

如果 c:\data\db 文件夹不存在,则自己创建它并再次运行上面的命令。

All should work fine by now.))

现在一切都应该正常工作。))

回答by matrixb0ss

I've forgot to start MongoDB at all. Go to separate tab in terminal and type:

我完全忘了启动 MongoDB。转到终端中的单独选项卡并键入:

sudo service mongod start

回答by Wamala Emmanuel Kisakye

I had the same error because i had not installed mongoDB. Make sure that you have mongodb installed and if not, you can download it from here https://www.mongodb.com/download-center/community

我有同样的错误,因为我没有安装 mongoDB。确保你已经安装了 mongodb,如果没有,你可以从这里下载https://www.mongodb.com/download-center/community

回答by Manohar Reddy Poreddy

I had below error:

我有以下错误:

Error: connect ECONNREFUSED 127.0.0.1:27017

In my case, this issue occurred since MongoDB is not installed at all.

就我而言,发生此问题是因为根本没有安装 MongoDB。

The solution was to install MongoDB from below location:
https://www.mongodb.com/download-center/community

解决方案是从以下位置安装 MongoDB:https:
//www.mongodb.com/download-center/community

After installing the ECONNREFUSEDerror did not appear again.

安装后ECONNREFUSED错误没有再出现。

Hope that helps.

希望有帮助。

回答by Farrukh

most probably your mongo db service is not started. For ubuntu : sudo service mongod startFor windows : go to services and start the MongoDB service

很可能你的 mongo db 服务没有启动。对于 ubuntu: sudo service mongod start对于 Windows:转到服务并启动 MongoDB 服务

also install link for mondoDB service https://www.mongodb.com/download-center/community

还安装 mondoDB 服务的链接https://www.mongodb.com/download-center/community

I had this problem and i solved it thanks to this man's answer https://stackoverflow.com/a/47433551/7917608

我遇到了这个问题,由于这个人的回答,我解决了这个问题https://stackoverflow.com/a/47433551/7917608

回答by otoloye

You probably need to continue running your DB process (by running mongod) while running your node server.

您可能需要mongod在运行节点服务器的同时继续运行数据库进程(通过运行)。

回答by Novichok

I had the same issue on Windows.

我在 Windows 上遇到了同样的问题。

Ctrl+C to close mongod, and start mongod again.

Ctrl+C 关闭 mongod,然后再次启动 mongod。

Am not sure, but it seems that initially mongod was working opening and closing connections. Then, it was not able to close some earlier connections and reached limit for opened ones. After restarting mongod, it works again.

我不确定,但似乎最初 mongod 正在打开和关闭连接。然后,它无法关闭一些较早的连接并达到打开连接的限制。重新启动 mongod 后,它再次工作。

回答by UA_

For Ubuntu users run sudo systemctl restart mongod

对于 Ubuntu 用户运行 sudo systemctl restart mongod