在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 这个文件也有了更多的认识等等

你可能感兴趣的:(MongoDB)