您如何更改MongoDB用户权限? [英] How do you change MongoDB user permissions?
本文介绍了您如何更改MongoDB用户权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果我有这个用户:
For instance, if I have this user:
> db.system.users.find()
{ "user" : "testAdmin", "pwd" : "[some hash]", "roles" : [ "clusterAdmin" ], "otherDBRoles" : { "TestDB" : [ "readWrite" ] } }
我想授予该用户对TestDB
数据库的dbAdmin
权限,我可以删除用户记录,然后使用新的权限将其添加回去:
And I want to give that user the dbAdmin
permissions on the TestDB
database, I can remove the user record then add it back with the new permissions:
> db.system.users.remove({"user":"testAdmin"})
> db.addUser( { user: "testAdmin",
pwd: "[whatever]",
roles: [ "clusterAdmin" ],
otherDBRoles: { TestDB: [ "readWrite", "dbAdmin" ] } } )
但这似乎很容易出错而且容易出错.
But that seems hacky and error-prone.
我可以更新表记录本身:
And I can update the table record itself:
> db.system.users.update({"user":"testAdmin"}, {$set:{ otherDBRoles: { TestDB: [ "readWrite", "dbAdmin" ] }}})
但是我不确定这是否真的创建了正确的权限-看起来不错,但可能是错误的.
But I'm not sure if that really creates the correct permissions - it looks fine but it may be subtly wrong.
有更好的方法吗?
推荐答案
如果只想更新用户角色.您可以通过以下方式进行
If you want to just update Role of User. You can do in the following way
db.updateUser( "userName",
{
roles : [
{ role : "dbAdmin", db : "dbName" },
{ role : "readWrite", db : "dbName" }
]
}
)
注意:-这只会覆盖该用户的角色.
Note:- This will override only roles for that user.
这篇关于您如何更改MongoDB用户权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文