安全与授权
授权
json
// 对test数据库授予,find,update 权限
{ resource:{db:"test", collection: ""}, actions: ["find", "update"] }
// 对集群授予shutdown
{resource: {cluster: true}, actions: ["shutdown"]}
角色
- read: 读取当前数据库中所有非系统集合
- readWrite: 读写当前数据库中所有非系统集合
- dbAdmin: 管理当前数据库
- userAdmin: 管理当前数据库中的用户和角色
- userAdminAnyDatabase: 对所有数据库执行操作(只在admin数据库中提供)
sh
# 对test数据库授予read权限
use test
db.createUser(
{
user: "testReader",
pwd: "passwd",
roles: [{role: "read", db:"test"}]
}
)
创建角色
sh
# create a role that just can read collection account of test database
use test
db.createRole(
{
role: "readAccounts",
privileges: [{ resource:{db:"test", collection: "accounts"}, actions: ["find"] }],
roles: [],
}
)
db.createUser(
{
user: "accountsReader",
pwd: "passwd",
roles: ["readAccounts"]
}
)
#退出,登录
mongo -u "accountsReader" -p "passwd" --authenticationDatabase "test"