繁体   English   中英

MongoDB 图集错误:uncaughtException: (Unauthorized) 未授权管理员执行命令

[英]MongoDB atlas error: uncaughtException: (Unauthorized) not authorized on admin to execute command

我是 mongodb 的新手,请原谅我的语言。 我已将应用程序的后端部署到 heroku 并尝试将其与 Mongodb 连接。 我遵循的步骤如下。

  1. 我在 mongodb.com 上创建了一个项目,添加了一个用户(角色为atlasAdmin@admin ),添加了 IP。

  2. 我将它连接到 MongoDB Compass 并添加了使用 Compass 所需的数据

  3. 我使用了connect your application并在编辑用户名、密码和 dbname 后将下面的字符串保存在 heroku 配置环境变量中。

mongodb+srv://<username>:<password>@vidly.5pn3u.azure.mongodb.net/<dbname>?retryWrites=true&w=majority

但它给出的错误是'MongoError: (Unauthorized) not authorized on admin to execute command

我尝试了以下选项来修复错误:

  1. 使用 mongo shell 我尝试创建用户。 我使用字符串mongo "mongodb+srv://vidly.5pn3u.azure.mongodb.net/<dbname>" --username <username>连接到 mongo 然后
use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'password',
    roles: [ { role: 'root', db: 'admin' } ]
  }
);
exit;

但它给出了错误: uncaught exception: Error: couldn't add user: (Unauthorized) not authorized on admin to execute command

  1. cloud.mongodb.com我将 mongodb 服务器上的角色更改为(dbAdminAnyDatabase@admin、backup@admin、readWriteAnyDatabase@admin、clusterMonitor@admin),但它给出了相同的错误。

请建议如何解决此错误?

编辑:

我可以使用 mongo connect with the mongo shell访问数据库,使用connect with the mongo shell show dbs connect with the mongo shell (但不能创建用户),并connect using mongodb compass并在 compass 中编辑数据库。 但是当我尝试connect your application选项时,它给出了这个错误。

添加的用户具有这些访问权限(dbAdminAnyDatabase@admin、backup@admin、readWriteAnyDatabase@admin、clusterMonitor@admin),即使该用户不工作。

您需要安装最新版本的猫鼬。 例如,您可以使用“yarn add mongoose”来安装 mongoose。

暂无
暂无

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

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