您好,登錄后才能下訂單哦!
今晚苦逼加班,就稍微梳理下mongodb的權限境配置,在安裝配置好一套mongodb后,需要創建相應的數據庫和用戶密碼給開發人員,那么權限改如何設置呢?
1.首先要創建數據庫
use chunqiu
2.創建用戶授予權限,在創建用戶之前首先了解下mongodb中的role功能,那么admin數據庫中有哪些role 呢?
查看role:
use admin
PRIMARY> show roles
{
"role" : "__system",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "backup",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "clusterAdmin",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "clusterManager",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "clusterMonitor",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "dbAdmin",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "dbAdminAnyDatabase",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "dbOwner",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "enableSharding",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "hostManager",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "read",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "readAnyDatabase",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "readWrite",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "readWriteAnyDatabase",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "restore",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "root",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "userAdmin",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "userAdminAnyDatabase",
"db" : "admin",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
那么chunqiu的庫有哪些role呢?
use chunqiu
PRIMARY> show roles
{
"role" : "dbAdmin",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "dbOwner",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "enableSharding",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "read",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "readWrite",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "userAdmin",
"db" : "chunqiu",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
3.看完了mongodb的role后是不是感覺豁然開朗?那么現在就創建用戶吧:
use chunqiu
db.createUser(
{
user: "app_user",
pwd: "app#password",
roles:
[
{
role: "dbOwner", ##數據庫權限的role選擇dbOwner
db: "chunqiu" ##注意此處數據驗證使用chunqiu庫
}
]
}
)
如果要建只讀賬號:
use chunqiu
db.createUser(
{
user: "app_user_read",
pwd: "read#password",
roles:
[
{
role: "read", ##數據庫權限的role選擇read
db: "chunqiu"
}
]
}
)
查看已經創建的用戶:
show users
修改密碼:
db.changeUserPassword('app_user','password');
授予角色:
db.grantRolesToUser( "admin" , [ { role: "dbAdminAnyDatabase", db: "admin" } ])
取消角色:
db.revokeRolesFromUser( "admin" , [ { role: "dbAdminAnyDatabase", db: "admin" } ])
4.最后你可以把創建的賬號和密碼發給開發人員使用了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。