【问题标题】:Error Connecting to MongoDB: "not authorized on admin to execute command"连接到 MongoDB 时出错:“管理员未授权执行命令”
【发布时间】:2020-12-14 01:27:55
【问题描述】:

我目前正在尝试将我的应用程序连接到 MongoDB 数据库(以前在 mlab 上工作)。我正在使用为连接提供的字符串:

mongodb+srv://admin:password@cluster0.stbko.mongodb.net/name?retryWrites=true&w=majority

当我运行应用程序时,我收到以下错误消息:

(node:35944) UnhandledPromiseRejectionWarning: MongoError: (Unauthorized) not authorized on admin to execute command { insert: "system.indexes", documents: [[{ns admin.sessions} {key [{expires 1}]} {name expires_1} {expireAfterSeconds 0} {unique false}]], ordered: true, writeConcern: { w: "majority" } }

我已经尝试过 mlab 迁移和上传数据库的备份副本,但没有成功。我也尝试更改用户的权限...我也无法追踪代码中出现此错误的位置。

我查看了多个我不确定它们是否适用于我的帖子:#1#2

我已经使用 mongo shell 连接到数据库,但是当我尝试运行 db.getUsers() 时出现以下错误

2020-12-09T17:17:43.592-0500 E QUERY    [thread1] Error: (Unauthorized) not authorized on admin to execute command { usersInfo: 1.0, $clusterTime: { clusterTime: {1607552228 10}, signature: { hash: {0 [ random numbers here ]}, keyId: more numbers } }, $db: "myDatabase" } :

有什么想法吗?

【问题讨论】:

  • 您尝试使用的用户的权限是什么?您可以在没有身份验证的情况下重新启动 mongodb 以验证用户权限,然后以适当的角色重新启动
  • 我已经用 atlasAdmin 和 readWriteAnyDatabase 试过了。我将尝试重新启动 mongodb。当您说重新启动时,您的意思是擦除数据库并从头开始?
  • 您能否在 mongodb 上为我澄清一些事情:确切的数据库名称是什么? MongoDB好像是按照Organization --> Projects --> Clusters来组织的……数据库名到底属于哪里?
  • 不用清空数据库,在mongo config上,关闭认证重启

标签: node.js mongodb


【解决方案1】:

在没有 writeMajority 的情况下连接如何?使用连接字符串

mongodb+srv://admin:password@cluster0.stbko.mongodb.net/name

【讨论】:

  • 我收到了同样的错误信息。这似乎是某种用户授权问题。
【解决方案2】:

问题在于 mongo-connect 和 connect-mongodb-session 的版本已过时。升级后我没有问题。

还必须为特定用户运行db.getUsers()。当我试图找到我的管理员用户时,它没有问题。

【讨论】:

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