在centos 7中运行app.js 报错 mongodb导入数据库报错:command listCollections requires authentication(已解决)

在运行app.js的时候报出如下错误:

[root@VM_0_14_centos html]# node app.js
服务器启动成功
数据库连接成功
(node:12711) UnhandledPromiseRejectionWarning: MongoError: command find requires authentication
at Connection. (/var/www/html/node_modules/mongodb/lib/core/connection/pool.js:466:61)
at Connection.emit (events.js:305:20)
at processMessage (/var/www/html/node_modules/mongodb/lib/core/connection/connection.js:384:10)
at Socket. (/var/www/html/node_modules/mongodb/lib/core/connection/connection.js:553:15)
at Socket.emit (events.js:305:20)
at addChunk (_stream_readable.js:341:12)
at readableAddChunk (_stream_readable.js:316:11)
at Socket.Readable.push (_stream_readable.js:250:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:12711) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12711) [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.
(node:12711) UnhandledPromiseRejectionWarning: MongoError: command find requires authentication
at Connection. (/var/www/html/node_modules/mongodb/lib/core/connection/pool.js:466:61)
at Connection.emit (events.js:305:20)
at processMessage (/var/www/html/node_modules/mongodb/lib/core/connection/connection.js:384:10)
at Socket. (/var/www/html/node_modules/mongodb/lib/core/connection/connection.js:553:15)
at Socket.emit (events.js:305:20)
at addChunk (_stream_readable.js:341:12)
at readableAddChunk (_stream_readable.js:316:11)
at Socket.Readable.push (_stream_readable.js:250:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:12711) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

错误信息说是数据库需要验证,刚开始以为是权限不够,查了很久的资料但是在第一天还是没有解决,网站还是没法传输数据,知道第二天晚上继续这个问题的时候,突然看到一篇文章中的一段字

保存mongodb.conf文件,然后重启!

没错需要重启,因为我尝试过把 xshell 关闭然后重新打开过,发现还是不行,因为我的MongoDB是设置成开机自动启动的,于是就忽略了重启这个问题,但是请注意你把xshell关了,只是把这个软件关了,而服务器还处于开机状态,所以要输入MongoDB的重启命令才能重启MongoDB,也就是这个命令:

systemctl restart mongod.service

果然输入重启之后成功解决问题。

在这个解决问题的过程中也学到了很多其他东西,比如:

  • 如何远程连接数据库
  • 如何设置系统变量
  • MongoDB的安全问题,如何连接加密数据库
  • 对mongodb.conf 这个文件也有了更多的认识等等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gxhlh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值