简体   繁体   English

UnhandledPromiseRejectionWarning: MongoNetworkError: 在第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError]

[英]UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError

I tried to run the following command:我尝试运行以下命令:

node index.js

However, I get the following from my terminal:但是,我从我的终端得到以下信息:

    success connection to port 3000
(node:16767) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError
: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:503:11
)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:16767) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16767) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I am not sure why I am getting this error.我不确定为什么会收到此错误。 Could anyone advice what to do?任何人都可以建议该怎么做?

If you are using mongoDB.Atlas go to network access and set Whitelist Entry: 0.0.0.0/0.如果您使用的是 mongoDB.Atlas,请转到网络访问并设置白名单条目:0.0.0.0/0。 在此处输入图片说明

在此处输入图片说明

Re-run your command.重新运行您的命令。

While the above solution might work, it is better to understand on how to debug and what the error says.虽然上述解决方案可能有效,但最好了解如何调试以及错误内容。

You got UnhandledPromiseRejectionWarning , because in your code, you must not be handling the promise rejection case where you are connection to MongoDB.您得到了UnhandledPromiseRejectionWarning ,因为在您的代码中,您不能处理连接到 MongoDB 的承诺拒绝情况。

Next, the node server failed to connect to the mongodb.接下来,节点服务器连接到mongodb失败。 This could be because of several reasons.这可能是由于几个原因。 One simple case as mentioned by @Sayegh, the server itself is not running. @Sayegh 提到的一种简单情况,服务器本身没有运行。 Could be other reason like wrong port etc, server not accepting incoming connections etc.可能是其他原因,例如错误的端口等,服务器不接受传入连接等。

How to debug.如何调试。 First step is to figure out if the process is running or not.第一步是确定进程是否正在运行。 One simple way would be to run this command ps aux | grep mongo一种简单的方法是运行此命令ps aux | grep mongo ps aux | grep mongo which list all processes from all users with mongo as a text. ps aux | grep mongo以文本形式列出所有用户的所有进程。 If this was true and still unable to connect, look for port.如果这是真的但仍然无法连接,请查找端口。 If still unable to connect, check if the server accepts incoming connection and take it from there.如果仍然无法连接,请检查服务器是否接受传入连接并从那里获取。

Not sure what system you're on, but try running this if you're on Linux:不确定您使用的是什么系统,但如果您使用的是 Linux,请尝试运行它:

mongo

or for macOS:或对于 macOS:

mongod

and leave it running in a new terminal and then try running the server again并让它在新终端中运行,然后再次尝试运行服务器

Before you launch your NodeJS command, make sure to create the right directories with the right permissions:在启动 NodeJS 命令之前,请确保使用正确的权限创建正确的目录:

> sudo mkdir -p /data/db
> sudo chmod -R go+w /data/db

我遇到了同样的问题,并通过将数据库用户的用户名和密码更改为更简单的东西(如dev:dev)来解决,并且我在将其与user-dev:user-dev一起使用时出错了……只需将其更改为简单的内容即可

First I tried Skitty's solution, and it didn't work for me, but I still keep the solution in my code, and then I changed app.listen(process.env.PORT || 3000);首先我尝试了Skitty的解决方案,它对我不起作用,但我仍然将解决方案保留在我的代码中,然后我更改了app.listen(process.env.PORT || 3000); and it worked for me.它对我有用。

I faced same issue but after a lot of RND.我遇到了同样的问题,但经过大量 RND。 I found that whts the problem so run this command on your terminal.我发现问题出在哪里,所以在你的终端上运行这个命令。

sudo service mongod start须藤服务 mongod 开始

then run mongo on terminal and look.然后在终端上运行mongo并查看。

Just repair the mongodb installed in your system.只需修复系统中安装的 mongodb。 By clicking on the package you installed you will have an option to repair it.通过单击您安装的软件包,您将可以选择修复它。

If you are using Mongo db atlas go on Network access and set to your current IP address and then re-run your command.如果您使用的是 Mongo db atlas,请继续网络访问并设置为您当前的 IP 地址,然后重新运行您的命令。

I hope it will work for you.我希望它对你有用。

在此处输入图片说明

MongoDB connection expects you to put URL encoded password. MongoDB 连接要求您输入 URL 编码的密码。 But when I had a password with '.'但是当我有一个带有 '.' 的密码时(xxx.xxx), the url encoding of that remains as dot. (xxx.xxx), url 编码保留为点。 However, it still did not work.但是,它仍然不起作用。 i changed the password to simple all lower case string and it worked fine.我将密码更改为简单的所有小写字符串,并且工作正常。

Not sure why it is not accepting.不知道为什么它不接受。 But this solved the issue for time-being.但这暂时解决了问题。

If you are a Mac user, this might be a relevant comment.如果您是 Mac 用户,这可能是相关评论。 For me it was because server was not running.对我来说这是因为服务器没有运行。 On running mongodb , I saw in console a message saying there is no /data/db and / is not writable for me.在运行mongodb ,我在控制台中看到一条消息,说没有/data/db并且 / 对我来说是不可写的。 So I used synthetic.conf solution.所以我使用了synthetic.conf 解决方案。

  1. sudo mkdir -p /System/Volumes/Data/data/db
  2. sudo chown -R whoami /System/Volumes/Data/data/db` sudo chown -R whoami /System/Volumes/Data/data/db`

  3. echo "data\\t/System/Volumes/Data/data" | sudo tee -a /etc/synthetic.conf
  4. reboot your system重启你的系统
  5. run mongodb运行mongodb

and it works.它有效。

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

相关问题 MongoNetworkError:首次连接时无法连接到服务器[localhost:27017] - MongoNetworkError: failed to connect to server [localhost:27017] on first connect MongoNetworkError:无法连接到服务器“MongoNetworkError” - MongoNetworkError: failed to connect to server 'MongoNetworkError' MongoError:首次连接时无法连接到服务器[localhost:27017] [MongoError:连接ECONNREFU SED 127.0.0.1:27017] - MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFU SED 127.0.0.1:27017] MongoError:无法连接到服务器[localhost:27017] - MongoError: failed to connect to server [localhost:27017] 错误:无法连接到[localhost:27017] - Error: failed to connect to [localhost:27017] 如何正确连接到 mongodb.atlas? Mongo网络错误 - how to properly connect to mongodb.atlas? MongoNetworkError 故障 - MongoError:无法连接到服务器 [未定义:27017] - Glitch - MongoError: failed to connect to server [undefined:27017] node和mongodb的问题 - UnhandledPromiseRejectionWarning:MongoNetworkError:connection 4 - Problems with node and mongodb - UnhandledPromiseRejectionWarning: MongoNetworkError: connection 4 使用node.js进行MongoDB设置:错误:无法连接到[localhost:27017] - MongoDB setup with node.js: Error: failed to connect to [localhost:27017] MongoNetworkError:连接 5 - MongoNetworkError: connection 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM