【问题标题】:MongoDB atlas error: uncaughtException: (Unauthorized) not authorized on admin to execute commandMongoDB atlas 错误:uncaughtException:(未授权)管理员未授权执行命令
【发布时间】:2020-11-01 09:18:33
【问题描述】:

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

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

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

  3. 我使用connect your application 并在编辑用户名、密码和数据库名后将下面的字符串保存在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 在 shell 上使用 show dbs 访问数据库(但无法创建用户)和 connect using mongodb compass 并在指南针中编辑数据库。 但是当我尝试connect your application 选项时,它给出了这个错误。

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

【问题讨论】:

  • @kanagavelu-sugumar 你能回答这个问题吗?

标签: mongodb heroku


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多