在MongoDB中,訪問控制列表(Access Control List,ACL)允許您為數(shù)據(jù)庫用戶定義特定角色和權(quán)限
admin
數(shù)據(jù)庫)。use admin
myUser
的用戶,該用戶具有在mydb
數(shù)據(jù)庫上讀取、寫入和執(zhí)行查詢的權(quán)限:db.createUser(
{
user: "myUser",
pwd: "myUserPassword",
roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" }
]
}
)
myUser
創(chuàng)建一個ACL,允許其在特定集合(例如myCollection
)上執(zhí)行特定操作: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
數(shù)組中使用多個對象,每個對象表示一個數(shù)據(jù)庫及其相關(guān)角色。例如:roles: [
{ role: "readWrite", db: "mydb" },
{ role: "dbAdmin", db: "mydb" },
{ role: "readOnly", db: "otherDB" }
]
roles
數(shù)組中使用多個對象,每個對象表示一個集合及其相關(guān)角色。例如:roles: [
{ role: "readWrite", db: "mydb", collection: "myCollection" },
{ role: "dbAdmin", db: "mydb" },
{ role: "readOnly", db: "otherDB", collection: "anotherCollection" }
]
通過以上步驟,您可以為MongoDB用戶設(shè)置訪問控制列表,以便他們能夠執(zhí)行特定操作。