MongoDB 用戶與權限

1、創建查詢role:custom_role,對db idap_zl 下的collection:tab1、tab2 只有查詢權限
1)使用trs用戶登錄數據庫
2)切換到 db:idap_zl 創建 role ,替換示例中的 collection ,如果有多個 collection ,請按照示例依次編寫
use idap_zl
db.createRole(
{
role:"custom_role",
privileges:
[
{
resource: { db:"idap_zl", collection:"tab1" },
actions: ["find"]
},
{
resource: { db:"idap_zl", collection:"tab2" },
actions: ["find"]
}
],
roles:
[]
},
{ w:"majority" }
);

2、創建新用戶,將1中創建的role賦值給用戶
1)切換到admin db
use admin
2)創建新用戶
db.createUser({user:"test",pwd:"test123",roles:[{role:"custom_role",db:"idap_zl"}]})

3、驗證
1)推出數據庫,並使用新創建的用戶 test 登錄數據庫
use admin
db.auth('test','test123')
use idap_zl
show tables
可以看到只有角色中對應的 tab1、tab2 可以查看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章