mongodb创建local库用户_mongodb用户与角色使用
此文檔以mongodb 4.0版本進行對用戶權限和角色講解,更詳細內容可參考mongodb官方文檔.
官方文檔:https://docs.mongodb.com/manual/core/security-users/
一.mongodb內部角色
1.數據庫用戶角色
read? ? ? --讀取數據庫對像的權限
readWrite? ?--讀取和修改數據庫對像權限
2.數據庫管理角色
dbAdmin? ?--執行管理任務角色
dbOwner? ?--數據庫所有者,可以對數據庫所有操作
userAdmin? ?--當前數據庫上創建,修改角色和用戶功能
3.集群管理角色
clusterAdmin?? ? ? ? ?--集群管理員
clusterManager? ? ? --管理集群和監控
clusterMonitor? ? ? ?--監控集群和只讀訪問
hostManager? ? ? ? ?--監控和管理服務器功能
4.備份恢復角色
backup? ? --備份數據最小權限
restore? ? --恢復權限
5.所有數據庫角色
readAnyDatabase? ?--只讀所有數據庫角色
readWriteAnyDatabase? ? ?--讀寫所有數據庫
userAdminAnyDatabase? ?--除local之外的所有數據庫相同的用戶管理操作訪問權限
dbAdminAnyDatabase? ? ? --除local之外的所有數據庫相同的權限
6.超級用戶角色
root? ? ? ? ?--提供所有資源readWriteAnyDatabase,dbAdminAnyDatabase,userAdminAnyDatabase,clusterAdmin,restore,backup
7.內部角色
__system? ? ? ?--提供對數據庫中的任何對象執行任何操作的權限
二.自定義角色
1.自定義角色格式
{
role:?"",
privileges:?[
{?resource:?{??},?actions:?[?"",?...?]?},
...
],
roles:?[
{?role:?"",?db:?""?}?|?"",
...
],
authenticationRestrictions:?[
{
clientSource:?[""?|?"",?...],
serverAddress:?[""?|?"",?...]
},
...
]
}
2.自定義角色(對config庫所有表可以增刪改查,對users庫usersCollection表更新,插入,刪除,對所有數據庫有查找權限)
>?use?admin
switched?to?db?admin
>?db.createRole(
{
role:?"wuhan123",???????--角色名
privileges:?[
{?resource:?{?db:?"config",?collection:?""?},?actions:?[?"find",?"update",?"insert",?"remove"?]?},
{?resource:?{?db:?"users",?collection:?"usersCollection"?},?actions:?[?"update",?"insert",?"remove"?]?},
{?resource:?{?db:?"",?collection:?""?},?actions:?[?"find"?]?}
],
roles:?[
{?role:?"read",?db:?"admin"?}
]
}
)
>
3.列出角色和刪除角色
>?db.getRole("wuhan123")???--顯示單個角色信息(wuhan123是角色名)
{
"role"?:?"wuhan123",
"db"?:?"admin",
"isBuiltin"?:?false,
"roles"?:?[
{
"role"?:?"read",
"db"?:?"admin"
}
],
"inheritedRoles"?:?[
{
"role"?:?"read",
"db"?:?"admin"
}
]
}
>?db.getRoles()?????--顯示當前庫所有角色
[
{
"role"?:?"wuhan123",
"db"?:?"admin",
"isBuiltin"?:?false,
"roles"?:?[
{
"role"?:?"read",
"db"?:?"admin"
}
],
"inheritedRoles"?:?[
{
"role"?:?"read",
"db"?:?"admin"
}
]
}
]
>?db.dropRole("wuhan123");?????--刪除角色
true
>?db.dropAllRoles();???????--刪除所有角色
NumberLong(1)
>
三.創建用戶并使用角色
1.創建用戶格式
{
user:?"",
pwd:?"",
customData:?{??},
roles:?[
{?role:?"",?db:?""?}?|?"",
...
],
authenticationRestrictions:?[
{
clientSource:?[""?|?"",?...]
serverAddress:?[""?|?"",?...]
},
...
],
mechanisms:?[?"",?...?],
passwordDigestor:?""
}
2.創建用戶使用角色
>?use?tong?????--進入數據庫
switched?to?db?tong
>?db.createUser(
...????{
...??????user:?"u_tong",???????--指定用戶名
...??????pwd:?"system123",?????--指定密碼
...??????roles:?[?"readWrite",?"dbAdmin"?]?????--使用數據庫中的角色
...????}
...?)
Successfully?added?user:?{?"user"?:?"u_tong",?"roles"?:?[?"readWrite",?"dbAdmin"?]?}
>
2.創建用戶指定來源IP和目標IP
>?use?tong
switched?to?db?tong
>?db.createUser(
{
user:?"u1_tong",????--用戶名
pwd:?"system123",???--密碼
roles:?[?{?role:?"readWrite",?db:?"tong"?}?],???--角色
authenticationRestrictions:?[?{
clientSource:?["192.168.1.10"],????--客戶端IP
serverAddress:?["192.168.1.20"]????--服務端IP
}?]
}
)>
3.查看用戶和刪除用戶
>?db.getUsers();??????--查看當前數據庫所有用戶
[
{
"_id"?:?"tong.u1_tong",
"user"?:?"u1_tong",
"db"?:?"tong",
"roles"?:?[
{
"role"?:?"readWrite",
"db"?:?"tong"
}
],
"mechanisms"?:?[
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
},
{
"_id"?:?"tong.u_tong",
"user"?:?"u_tong",
"db"?:?"tong",
"roles"?:?[
{
"role"?:?"readWrite",
"db"?:?"tong"
},
{
"role"?:?"dbAdmin",
"db"?:?"tong"
}
],
"mechanisms"?:?[
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
>?db.getUser("u_tong");?????--查看指定用戶
{
"_id"?:?"tong.u_tong",
"user"?:?"u_tong",
"db"?:?"tong",
"roles"?:?[
{
"role"?:?"readWrite",
"db"?:?"tong"
},
{
"role"?:?"dbAdmin",
"db"?:?"tong"
}
],
"mechanisms"?:?[
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>?db.dropUser("u_tong");????--刪除單個用戶
true
>?db.dropAllUsers();????????--刪除當前庫所有用戶
NumberLong(1)
>
4.將角色授權給用戶
>?db.grantRolesToUser(
"u_tong",[?"readWrite"?,?{?role:?"read",?db:?"tong"?}?],
>?)
總結
以上是生活随笔為你收集整理的mongodb创建local库用户_mongodb用户与角色使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中convert函数用法_P
- 下一篇: 冯乐乐 unity_Unity常用矩阵运