在MongoDB中,访问控制列表(Access Control List,ACL)允许您为数据库用户定义特定角色和权限
admin
数据库)。use admin
myUser
的用户,该用户具有在mydb
数据库上读取、写入和执行查询的权限:db.createUser(
{
user: "myUser",
pwd: "myUserPassword",
roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" }
]
}
)
myUser
创建一个ACL,允许其在特定集合(例如myCollection
)上执行特定操作:db.createACL(
{
user: "myUser",
db: "mydb",
collection: "myCollection",
roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" }
]
}
)
myUser
分配了readWrite
和dbAdmin
角色:roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" }
]
roles
数组中使用多个对象,每个对象表示一个数据库及其相关角色。例如:roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" },
{ role: "readOnly", db: "otherDB" }
]
roles
数组中使用多个对象,每个对象表示一个集合及其相关角色。例如:roles: [
{ role: "readWrite", db: "mydb", collection: "myCollection" },
{ role: "dbAdmin", db: "mydb" },
{ role: "readOnly", db: "otherDB", collection: "anotherCollection" }
]
通过以上步骤,您可以为MongoDB用户设置访问控制列表,以便他们能够执行特定操作。