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