MongoDB數(shù)據(jù)庫用戶角色和權(quán)限管理詳解
查看數(shù)據(jù)庫
使用終端命令行輸入 mongo 登陸 mongodb 之后切換到 admin 庫,并認證后可查看所有數(shù)據(jù)庫,操作如下所示:
[root@renwole.com ~]# mongo MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("1ea1-4343-9523-167a101973a9") } MongoDB server version: 4.4.0 > use admin > db.auth("admin","InaM6Aip#2JBlWwY") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB
說明:1 表示認證成功,0 表示認證失敗,認證失敗后查看數(shù)據(jù)庫無任何返回。
創(chuàng)建數(shù)據(jù)庫及用戶
創(chuàng)建一個 renwoledb 數(shù)據(jù)庫并授權(quán) renwole 用戶為該庫的 dbOwner 角色。另外、MongoDB數(shù)據(jù)庫實行注冊制,數(shù)據(jù)庫內(nèi)無內(nèi)容時,無法查看到新建的數(shù)據(jù)庫,操作如下:
> use renwoledb > db.createUser( { user:"renwole", pwd:"renwolecom", roles:[{role:"dbOwner",db:"renwoledb"}] } )
此時已完成了一庫一賬號的創(chuàng)建。如果創(chuàng)建用戶提示無權(quán)限,請先使用超級管理員登錄之后切換到對應的數(shù)據(jù)庫再創(chuàng)建即可,如下所示:
MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("7be9-4c30-ad2e-2a5b58127ab7") } MongoDB server version: 4.4.0 > use renwoledb switched to db renwoledb > db.createUser( { user:"renwole", pwd:"renwolecom", roles:[{role:"dbOwner",db:"renwoledb"}] } ) uncaught exception: Error: couldn't add user: command createUser requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.createUser@src/mongo/shell/db.js:1343:11 @(shell):1:1 > use admin switched to db admin > db.auth("root","renwolecompassword") 1 > use renwoledb switched to db renwoledb > db.createUser( { user:"renwole", pwd:"renwolecom", roles:[{role:"dbOwner",db:"renwoledb"}] } ) Successfully added user: { "user" : "renwole", "roles" : [ { "role" : "dbOwner", "db" : "renwoledb" } ] }
添加 root 用戶,擁有整個 MongoDB 最高權(quán)限,建議取消認證模式后,先進入到 admin 庫,再添加 root 用戶權(quán)限
> use admin > db.createUser({user: "root",pwd: "renwolecom",roles: [ { role: "root", db: "admin" } ]})
密碼修改
修改某個賬號的數(shù)據(jù)庫密碼需要進入到該數(shù)據(jù)庫,認證后再修改,否則報錯,操作如下:
> use renwoledb > db.changeUserPassword("renwole", "renwolecompwdnew") > db.auth("renwole","renwolecompwdnew") 1
刪除用戶及數(shù)據(jù)庫
刪除用戶(必須切換到admin使用最高權(quán)限刪除某個用戶角色)
> db.system.users.remove({user:"renwole"}); WriteResult({ "nRemoved" : 1 })
刪除所有用戶(必須具備超級管理權(quán)限才能刪除)
> db.system.users.remove({})
刪除數(shù)據(jù)庫(必須切換到指定的數(shù)據(jù)庫,然后再刪除)
> use renwoledb switched to db renwoledb > db.dropDatabase() { "ok" : 1 } >
總結(jié)
到此這篇關(guān)于MongoDB數(shù)據(jù)庫用戶角色和權(quán)限管理的文章就介紹到這了,更多相關(guān)MongoDB用戶角色和權(quán)限管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB的$sample、aggregate和$rand實現(xiàn)隨機選取數(shù)據(jù)
在MongoDB中,我們可以使用內(nèi)置的$sample聚合操作符來隨機生成數(shù)據(jù),$sample可以從集合文檔中隨機選擇指定數(shù)量的文檔,但由于其查詢整個集合的性能問題,應該慎用,aggregate方法以及$rand函數(shù)的結(jié)合使用可以實現(xiàn)更加靈活的查詢操作,并且可以對查詢結(jié)果進行精細篩選2024-01-01Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法
MongoDB是一個跨平臺,面向文檔的數(shù)據(jù)庫,提供高性能,高可用性和易于擴展。MongoDB是工作在集合和文檔上一種概念。下面通過本文給大家分享Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法,需要的朋友參考下吧2017-11-11Mongodb數(shù)據(jù)庫誤刪后的恢復方法(兩種)
本文給大家分享兩種方法來實現(xiàn)Mongodb數(shù)據(jù)庫誤刪后的恢復,每種方法給大家介紹的都非常詳細,需要的朋友參考下吧2018-08-08MongoDB服務(wù)端JavaScript腳本使用方法
這篇文章主要介紹了MongoDB服務(wù)端JavaScript腳本使用方法,需要的朋友可以參考下2015-10-10SpringBoot+MongoDB實現(xiàn)物流訂單系統(tǒng)的代碼
這篇文章主要介紹了SpringBoot+MongoDB實現(xiàn)物流訂單系統(tǒng)的代碼,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09