MongoDB數(shù)據(jù)庫用戶角色和權(quán)限管理詳解
查看數(shù)據(jù)庫
使用終端命令行輸入 mongo 登陸 mongodb 之后切換到 admin 庫,并認(rèn)證后可查看所有數(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 表示認(rèn)證成功,0 表示認(rèn)證失敗,認(rèn)證失敗后查看數(shù)據(jù)庫無任何返回。
創(chuàng)建數(shù)據(jù)庫及用戶
創(chuàng)建一個(gè) renwoledb 數(shù)據(jù)庫并授權(quán) renwole 用戶為該庫的 dbOwner 角色。另外、MongoDB數(shù)據(jù)庫實(shí)行注冊(cè)制,數(shù)據(jù)庫內(nèi)無內(nèi)容時(shí),無法查看到新建的數(shù)據(jù)庫,操作如下:
> use renwoledb > db.createUser( { user:"renwole", pwd:"renwolecom", roles:[{role:"dbOwner",db:"renwoledb"}] } )
此時(shí)已完成了一庫一賬號(hào)的創(chuàng)建。如果創(chuàng)建用戶提示無權(quán)限,請(qǐng)先使用超級(jí)管理員登錄之后切換到對(duì)應(yīng)的數(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 用戶,擁有整個(gè) MongoDB 最高權(quán)限,建議取消認(rèn)證模式后,先進(jìn)入到 admin 庫,再添加 root 用戶權(quán)限
> use admin > db.createUser({user: "root",pwd: "renwolecom",roles: [ { role: "root", db: "admin" } ]})
密碼修改
修改某個(gè)賬號(hào)的數(shù)據(jù)庫密碼需要進(jìn)入到該數(shù)據(jù)庫,認(rèn)證后再修改,否則報(bào)錯(cuò),操作如下:
> use renwoledb > db.changeUserPassword("renwole", "renwolecompwdnew") > db.auth("renwole","renwolecompwdnew") 1
刪除用戶及數(shù)據(jù)庫
刪除用戶(必須切換到admin使用最高權(quán)限刪除某個(gè)用戶角色)
> db.system.users.remove({user:"renwole"}); WriteResult({ "nRemoved" : 1 })
刪除所有用戶(必須具備超級(jí)管理權(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB的$sample、aggregate和$rand實(shí)現(xiàn)隨機(jī)選取數(shù)據(jù)
在MongoDB中,我們可以使用內(nèi)置的$sample聚合操作符來隨機(jī)生成數(shù)據(jù),$sample可以從集合文檔中隨機(jī)選擇指定數(shù)量的文檔,但由于其查詢整個(gè)集合的性能問題,應(yīng)該慎用,aggregate方法以及$rand函數(shù)的結(jié)合使用可以實(shí)現(xiàn)更加靈活的查詢操作,并且可以對(duì)查詢結(jié)果進(jìn)行精細(xì)篩選2024-01-01Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法
MongoDB是一個(gè)跨平臺(tái),面向文檔的數(shù)據(jù)庫,提供高性能,高可用性和易于擴(kuò)展。MongoDB是工作在集合和文檔上一種概念。下面通過本文給大家分享Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法,需要的朋友參考下吧2017-11-11Mongodb數(shù)據(jù)庫誤刪后的恢復(fù)方法(兩種)
本文給大家分享兩種方法來實(shí)現(xiàn)Mongodb數(shù)據(jù)庫誤刪后的恢復(fù),每種方法給大家介紹的都非常詳細(xì),需要的朋友參考下吧2018-08-08MongoDB服務(wù)端JavaScript腳本使用方法
這篇文章主要介紹了MongoDB服務(wù)端JavaScript腳本使用方法,需要的朋友可以參考下2015-10-10SpringBoot+MongoDB實(shí)現(xiàn)物流訂單系統(tǒng)的代碼
這篇文章主要介紹了SpringBoot+MongoDB實(shí)現(xiàn)物流訂單系統(tǒng)的代碼,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09