在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用戶設置訪問控制列表,以便他們能夠執行特定操作。