gpt4 book ai didi

security - 我需要哪些 MongoDB 用户权限才能将用户添加到新的/另一个 mongo 数据库?

转载 作者:IT老高 更新时间:2023-10-28 13:08:10 27 4
gpt4 key购买 nike

添加一个具有以下权限的管理员用户后,我在 MongoDB 配置文件中启用了身份验证:userAdminuserAdminAnyDatabase

现在我将此用户连接到定义此管理员用户的数据库(否​​则我会得到 exception: login failed)。

成功连接后,我想将新用户添加到新数据库。为此,我正在尝试:

use another
db.addUser(...)

但我得到一个错误:

Wed Dec 11 17:45:18.277 couldn't add user: not authorized for insert on app.system.users at src/mongo/shell/db.js:128

如何创建新数据库并向其中添加第一个用户?


详细(本例中所有用户的密码都是1234)

$ mongo mono -u admin_all -p 1234
MongoDB shell version: 2.4.6
connecting to: mono
> db.system.users.find()
{ "_id" : ObjectId("52a9831de41eb640bb0f5f64"), "user" : "admin_all", "pwd" : "a6316ed4886c10663cce46bc216ea375", "roles" : [ "userAdmin", "userAdminAnyDatabase" ] }
{ "_id" : ObjectId("52a98404ef1f9bc934b62e11"), "user" : "admin_one", "pwd" : "884f516cf308a4c6a75bbc5a0a00807b", "roles" : [ "userAdmin" ] }
{ "_id" : ObjectId("52a98415ef1f9bc934b62e12"), "user" : "admin_any", "pwd" : "1616611df9b47c58b607054d384cab99", "roles" : [ "userAdminAnyDatabase" ] }
> use another
switched to db another
> db.addUser({ user: "user", pwd: "1234", roles: ["read"] })
{
"user" : "user",
"pwd" : "461d4f349d8d4ec3d22a4c945010c330",
"roles" : [
"read"
],
"_id" : ObjectId("52a985372fcdbfd033003a7e")
}
Thu Dec 12 10:43:19.091 couldn't add user: not authorized for insert on another.system.users at src/mongo/shell/db.js:128

最佳答案

犯的错误是我在 admin 数据库中使用的 userAdminAnyDatabase 用户是 NOT (请参阅 this note )。所以必须在您的服务器上有一个名为 admin 的数据库!正如文档所说的“AnyDatabase”权限:

If you add any of these roles to a user privilege document outside of the admin database, the privilege will have no effect.

所以你必须:

  • userAdminAnyDatabase 添加到 admin db

    $ mongo admin
    > db.createUser({ user: "myadmin", pwd: "1234", roles: ["userAdminAnyDatabase"] })
  • 开启身份验证

    auth = true
    setParameter = enableLocalhostAuthBypass=0
  • 使用新的 myadmin 用户连接到您想要的任何数据库并添加更多用户:

    $ mongo another -u myadmin -p 1234
    > db.createUser({ user: "user", pwd: "1234", roles: ["readWrite"] })

    > use another
    > db.createUser({ user: "user", pwd: "1234", roles: ["readWrite"] })

关于security - 我需要哪些 MongoDB 用户权限才能将用户添加到新的/另一个 mongo 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20525103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com