本篇文章為大家展示了如何創(chuàng)建MongoDB用戶與角色使用,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司主營聞喜網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),聞喜h5重慶小程序開發(fā)搭建,聞喜網(wǎng)站營銷推廣歡迎聞喜等地區(qū)企業(yè)咨詢
一.mongodb內(nèi)部角色
1.數(shù)據(jù)庫用戶角色
read --讀取數(shù)據(jù)庫對像的權(quán)限
readWrite --讀取和修改數(shù)據(jù)庫對像權(quán)限
2.數(shù)據(jù)庫管理角色
dbAdmin --執(zhí)行管理任務(wù)角色
dbOwner --數(shù)據(jù)庫所有者,可以對數(shù)據(jù)庫所有操作
userAdmin --當(dāng)前數(shù)據(jù)庫上創(chuàng)建,修改角色和用戶功能
3.集群管理角色
clusterAdmin --集群管理員
clusterManager --管理集群和監(jiān)控
clusterMonitor --監(jiān)控集群和只讀訪問
hostManager --監(jiān)控和管理服務(wù)器功能
4.備份恢復(fù)角色
backup --備份數(shù)據(jù)最小權(quán)限
restore --恢復(fù)權(quán)限
5.所有數(shù)據(jù)庫角色
readAnyDatabase --只讀所有數(shù)據(jù)庫角色
readWriteAnyDatabase --讀寫所有數(shù)據(jù)庫
userAdminAnyDatabase --除local之外的所有數(shù)據(jù)庫相同的用戶管理操作訪問權(quán)限
dbAdminAnyDatabase --除local之外的所有數(shù)據(jù)庫相同的權(quán)限
6.超級用戶角色
root --提供所有資源readWriteAnyDatabase,dbAdminAnyDatabase,userAdminAnyDatabase,clusterAdmin,restore,backup
7.內(nèi)部角色
__system --提供對數(shù)據(jù)庫中的任何對象執(zhí)行任何操作的權(quán)限
二.自定義角色
1.自定義角色格式
{ role: "", privileges: [ { resource: { }, actions: [ " ", ... ] }, ... ], roles: [ { role: " ", db: " " } | " ", ... ], authenticationRestrictions: [ { clientSource: [" " | " ", ...], serverAddress: [" " | " ", ...] }, ... ] }
2.自定義角色(對config庫所有表可以增刪改查,對users庫usersCollection表更新,插入,刪除,對所有數(shù)據(jù)庫有查找權(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") --顯示單個角色信息(wuhan123是角色名) { "role" : "wuhan123", "db" : "admin", "isBuiltin" : false, "roles" : [ { "role" : "read", "db" : "admin" } ], "inheritedRoles" : [ { "role" : "read", "db" : "admin" } ] } > db.getRoles() --顯示當(dāng)前庫所有角色 [ { "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 --進入數(shù)據(jù)庫 switched to db tong > db.createUser( ... { ... user: "u_tong", --指定用戶名 ... pwd: "system123", --指定密碼 ... roles: [ "readWrite", "dbAdmin" ] --使用數(shù)據(jù)庫中的角色 ... } ... ) Successfully added user: { "user" : "u_tong", "roles" : [ "readWrite", "dbAdmin" ] } >
2.創(chuàng)建用戶指定來源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ù)庫所有用戶 [ { "_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(); --刪除當(dāng)前庫所有用戶 NumberLong(1) >
4.將角色授權(quán)給用戶
> db.grantRolesToUser( "u_tong",[ "readWrite" , { role: "read", db: "tong" } ], > )
上述內(nèi)容就是如何創(chuàng)建mongodb用戶與角色使用,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。