简体   繁体   English

通过 Node.Js 进行 mongoDB 连接的 MongoNetworkError

[英]MongoNetworkError on mongoDB connection through Node.Js

I'm new to Node.Js and learning through tutorials, I have already installed the mongoDB server and mongoDB compass.我是 Node.Js 的新手并通过教程学习,我已经安装了 mongoDB 服务器和 mongoDB 指南针。 Trying to run it with Visual Studio code and windows powershell - but I couldn't connect app to mongoDB.尝试使用 Visual Studio 代码和 windows powershell 运行它 - 但我无法将应用程序连接到 mongoDB。

code:代码:

const mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/playground').then(()=>
console.log('connected to mongodb..'))
.catch(err => console.error('could not connect to MongoDB..', err));

Error:错误:

could not connect to MongoDB.. MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
  name: 'MongoNetworkError',
  message: 'connect ECONNREFUSED 127.0.0.1:27017',
  stack: 'Error: connect ECONNREFUSED 127.0.0.1:27017\n' +
    '    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14)'
}]

Any help is much appreciated.任何帮助深表感谢。

Looks like you're missing the port number.看起来您缺少端口号。 An example of connection string would be mongodb://localhost:27017/MyDb连接字符串的一个例子是mongodb://localhost:27017/MyDb

I don't know Node.js or MongoDB.我不知道 Node.js 或 MongoDB。

However, the error message looks common to me.但是,错误消息对我来说很常见。 It seems that localhost refuses the connection on port 27017. So the first thing I would check is if the DB server really is listening on that port.似乎localhost拒绝了端口 27017 上的连接。所以我要检查的第一件事是数据库服务器是否真的在该端口上侦听。

I even don't know whether your stack runs under Windows or under Linux.我什至不知道您的堆栈是在 Windows 下还是在 Linux 下运行。 But both of them have a tool called netstat which you can use to check who is listening on port 27017. If nobody listens there, you know that your DB server did not start as intended.但是他们都有一个叫做netstat的工具,你可以用它来检查谁在监听端口 27017。如果没有人在那里监听,你就知道你的数据库服务器没有按预期启动。

Another reason for the malfunction could be a firewall which blocks inbound connections to port 27017. This is unlikely in your case, though, because the connection to be established is from localhost to localhost ;故障的另一个原因可能是防火墙阻止了到端口 27017 的入站连接。不过,这在您的情况下不太可能,因为要建立的连接是从localhostlocalhost such connections usually are not blocked by firewalls.此类连接通常不会被防火墙阻止。

If you are using Windows and install mongoDB to C drive then you should create two如果您使用的是 Windows 并将 mongoDB 安装到 C 盘,那么您应该创建两个
directory in C drive one is "data" and inside data create "db" like C://data/db C驱动器中的目录是“data”,内部数据创建“db”,如C://data/db

open your cmd panel and type mongod -dbpath c://data/db and your mongodb service is running and now you able to connect mongoDB打开您的 cmd 面板并输入mongod -dbpath c://data/db并且您的 mongodb 服务正在运行,现在您可以连接 mongoDB

If you're new in the field too, you gonna have setup problems with some PORTS ... 如果您也是该领域的新手,那么某些PORTS可能会出现设置问题...

The easy way: just use MongoDB ATLAS (2 minutes setup) 简单的方法:只需使用MongoDB ATLAS (设置2分钟)

The first tutorial I found on google: https://developerhandbook.com/mongodb/connect-mongo-atlas-mongoose/ 我在Google上找到的第一个教程: https : //developerhandbook.com/mongodb/connect-mongo-atlas-mongoose/

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

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