use admin > db.addUser( { user: "test", pwd: "pass-6ren">
gpt4 book ai didi

mongodb - 使用 mongodb v2.4.8 创建具有 "dbAdminAnyDatabse"角色的管理员

转载 作者:可可西里 更新时间:2023-11-01 09:52:20 26 4
gpt4 key购买 nike

我已经为我的 mongo 安装创建了一个管理员用户,如下所示:

> use admin   
> db.addUser( { user: "test",
pwd: "password",
roles: [ "dbAdminAnyDatabse",
otherDBRoles:
{
"otherTestDB": [ "readWrite" ]
}]
} )

当我尝试使用 user: "test"和 pwd: "password"与 robomongo 或 java 驱动程序连接到 "otherTestDB"时,出现错误的身份验证错误。

哪里错了?

最佳答案

您已经为管理数据库创建了一个用户标识,因此要使用该用户标识,您必须连接到管理数据库,而不是其他测试数据库。 otherDBRoles 文档在以该用户身份连接到管理数据库时控制对其他数据库的访问。因此,对于您指定的 addUser,以下命令失败,因为该用户是针对 admin 数据库的,而不是针对 otherTestDB 的:

$ mongo otherTestDB --username test --password password --eval 'printjson(db.c.findOne())'
connecting to: otherTestDB
Thu Feb 27 10:45:20.722 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228

而以下连接到管理数据库然后通过 getSiblingDB 使用 otherTestDB 的命令成功了:

$ mongo admin --username test --password password --eval 'printjson(db.getSiblingDB("otherTestDB").c.findOne())'
connecting to: admin
{ "_id" : ObjectId("530f5d904dbd43cfb46aab5b"), "hello" : "world" }

如果您希望用户在使用该用户 ID 和密码连接到 otherTestDB 时可以进行身份​​验证,则需要单独将这样的用户添加到 otherTestDB。

这有帮助吗?

关于mongodb - 使用 mongodb v2.4.8 创建具有 "dbAdminAnyDatabse"角色的管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21975834/

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