node.js MongoNetworkError:第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50173080/
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
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
提问by Karam Haj
I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,
我是 nodeJS 的新手,通过跟随 youtube 上的预告片开始学习,一切顺利,直到我添加了连接功能,如果 mongodb,
mongo.connect("mongodb://localhost:27017/mydb")
when I run my code on cmd (node start-app), get the following error,
当我在 cmd (node start-app) 上运行我的代码时,出现以下错误,
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Could someone explain me which step I missed ? my code :
有人能解释一下我错过了哪一步吗?我的代码:
var express = require("express");
var MongoClient = require('mongodb');
var url = "mongodb://localhost:27017/mydb";
var webService = require("./webService");
var server = express();
MongoClient.connect(url, function (err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
server.use(express.urlencoded({ extended: true }));
server.set('views', __dirname);
server.get('/', function (request, response) {
response.sendFile(__dirname + '/MainPage.html');
});
server.get('/Sign', function (request, response) {
response.render(__dirname + '/Sign.ejs');
});
server.post("/signUp", webService.signUp);
server.post("/createUser", webService.createUser);
server.listen(5500);
采纳答案by Vishnu
You have to install MongoDBdatabase server first in your system and start it.
您必须MongoDB首先在系统中安装数据库服务器并启动它。
Use the below link to install MongoDB
使用以下链接进行安装 MongoDB
回答by NadZ
Many of them don't add this, especially in AWS EC2 Instance, I had the same issue and tried different solutions. Solution: one of my database URL inside the code was missing this parameter 'authSource', adding this worked for me.
他们中的许多人没有添加这个,尤其是在 AWS EC2 实例中,我遇到了同样的问题并尝试了不同的解决方案。解决方案:代码中我的数据库 URL 之一缺少此参数“authSource”,添加此参数对我有用。
mongodb://myUserName:MyPassword@ElasticIP:27017/databaseName?authSource=admin
回答by Cory M
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts filea long time ago to allow me to access my localhost from my virtualbox.
在尝试了谷歌提出的关于堆栈溢出的每个解决方案后,我发现了我的特殊问题。我很久以前编辑过我的主机文件,以允许我从我的虚拟机访问我的本地主机。
Removing this entry solved it for me, along with the correct installation of mongoDB from the link given in the above solution, and including the correct promise handling code:
删除此条目为我解决了这个问题,以及从上述解决方案中给出的链接正确安装 mongoDB,并包括正确的承诺处理代码:
mongoose.connect('mongodb://localhost/testdb').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
回答by nagender pratap chauhan
I faced same issue but after a lot of RND. I found that whts the problem so run this command on your terminal.
我遇到了同样的问题,但经过大量 RND。我发现问题出在哪里,所以在你的终端上运行这个命令。
sudo service mongod start
须藤服务 mongod 开始
then run mongoon terminal
然后在终端上运行mongo
回答by KeitelDOG
Following the logic behind @CoryM's answer above :
按照上面@CoryM 的回答背后的逻辑:
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me...
在尝试了谷歌提出的关于堆栈溢出的每个解决方案后,我发现了我的特殊问题。我很久以前编辑过我的主机文件,以允许我从我的虚拟机访问我的本地主机。
删除此条目为我解决了它...
I had edited my hosts file too for Python Machine Learning setup 2 months ago. So instead of removing it because I still need it, I use 127.0.0.1in place of localhostand it worked :
2 个月前,我也为 Python 机器学习设置编辑了我的主机文件。因此,我没有因为我仍然需要它而将其删除,而是使用127.0.0.1了 代替localhost并且它起作用了:
mongoose.connect('mongodb://127.0.0.1/testdb')
回答by Pritt Balagopal
This had occurred to me and I have found out that it was because of faulty internet connection. If I use the public wifi at my place, which blocks various websites for security reasons, Mongo refuses to connect. But if I were to use my own mobile data, I can connect to the database.
这发生在我身上,我发现这是因为互联网连接错误。如果我在我的地方使用公共 wifi,出于安全原因阻止了各种网站,Mongo 拒绝连接。但是如果我要使用自己的移动数据,我可以连接到数据库。
回答by Udit Jain
just run mongod in terminal on the base folder if everything has been set up like installing mongo db and the client for it like mongoose. After running the command run the project file that you are working on and then the error shouldn't appear.
如果一切都已经设置好,比如安装 mongo db 和像 mongoose 这样的客户端,只需在基本文件夹的终端中运行 mongod。运行该命令后,运行您正在处理的项目文件,然后不应出现错误。
回答by Mrutyunjaya Kar
first create folder by command line mkdir C:\data\db (This is for database) then run command mongod --port 27018 by one command prompt(administration mode)- you can give name port number as your wish
首先通过命令行 mkdir C:\data\db 创建文件夹(这是用于数据库)然后通过一个命令提示符(管理模式)运行命令 mongod --port 27018 - 您可以根据需要指定名称端口号
回答by Aleem
You can check detail of error by running this command
您可以通过运行此命令来检查错误的详细信息
sudo service mongod status
sudo service mongod status
if error is something like this
如果错误是这样的
- Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error
- Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 670
- 无法取消连接套接字文件 /tmp/mongodb-27017.sock 未知错误
- src/mongo/transport/transport_layer_asio.cpp 670 处的致命断言 40486
simply running this will resolve your issue
简单地运行这将解决您的问题
rm /tmp/mongodb-27017.sock
rm /tmp/mongodb-27017.sock
回答by Pedro Nunez
I had this issue while working at the local Starbucks and I remembered that when I initially set up my database through Mongo Atlas. I set my IP address to be able to access the database. After looking through several threads, I changed my IP address on Atlas and the issue went away. Hope this helps someone.
我在当地的星巴克工作时遇到了这个问题,我记得当我最初通过 Mongo Atlas 设置我的数据库时。我设置了我的 IP 地址以便能够访问数据库。在浏览了几个线程后,我在 Atlas 上更改了我的 IP 地址,问题就消失了。希望这可以帮助某人。

