MongoDB4.28開啟權(quán)限認(rèn)證配置用戶密碼登錄功能
MongoDB默認(rèn)不啟用授權(quán)認(rèn)證,只要能連接到該服務(wù)器,就可連接到mongod。若要啟用安全認(rèn)證,需要更改配置文件mongdb.conf中的參數(shù)auth。
MongoDB的用戶是跟數(shù)據(jù)庫相關(guān)聯(lián)的,具體的數(shù)據(jù)庫,需要有對應(yīng)的用戶,超級管理員也不能操作其他數(shù)據(jù)庫的。
MongoDB存儲所有的用戶信息在admin 數(shù)據(jù)庫的集合system.users中,保存用戶名、密碼和數(shù)據(jù)庫信息。
MongoDB開啟權(quán)限認(rèn)證:配置用戶名和密碼認(rèn)證登錄,操作步驟:
1、查看是否開啟認(rèn)證登錄
$cd /usr/local/mongodb/bin $cat mongodb.conf
#數(shù)據(jù)文件存放目錄
dbpath = /usr/local/mongodb/data
#日志文件存放目錄
logpath = /usr/local/mongodb/logs/mongodb.log
logappend=true
#端口
port = 27017
#以守護(hù)程序的方式啟用,即在后臺運(yùn)行
fork = true
#認(rèn)證模式(true代表開啟認(rèn)證登錄,false代表未開啟認(rèn)證登錄)
auth=false
#遠(yuǎn)程連接
bind_ip=0.0.0.0
2、開啟用戶名和密碼認(rèn)證(創(chuàng)建用戶均需進(jìn)入admin數(shù)據(jù)庫)
2.1、為admin數(shù)據(jù)庫創(chuàng)建管理員賬號
1、數(shù)據(jù)庫admin創(chuàng)建管理員賬號
[root@hadoop-master bin]# mongo > use admin > db.createUser({user:"root",pwd:"lianshi",roles:["root"]})
2、查看目前用戶
> show users
2.2、為數(shù)據(jù)庫mytest創(chuàng)建普通用戶
1、給數(shù)據(jù)庫mytest創(chuàng)建cg用戶
>use mytest > db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})
2、查看目前用戶
> show users >db.system.users.find()命令可以查看新創(chuàng)建的用戶
2.3、配置文件開啟用戶名密碼認(rèn)證
#認(rèn)證模式(true代表開啟認(rèn)證登錄,false代表未開啟認(rèn)證登錄) auth=true
3、重啟mongo服務(wù)
[root@hadoop-master bin]# ps -ef |grep mongo [root@hadoop-master bin]# kill -9 15231 $./mongod -f mongodb.conf
4、mongo授權(quán)訪問
4.1、admin數(shù)據(jù)庫授權(quán)登錄
1、mongo訪問
[root@hadoop-master bin]# mongo > use admin switched to db admin > show users 2020-06-21T20:14:59.735+0800 E QUERY [js] uncaught exception: Error: command usersInfo requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1638:15 shellHelper.show@src/mongo/shell/utils.js:883:9 shellHelper@src/mongo/shell/utils.js:790:15 @(shellhelp2):1:1 -->授權(quán)配置并重啟后,此時(shí)查看用戶,會發(fā)現(xiàn)沒有權(quán)限
2、用用戶和密碼登錄
> db.auth("root","lianshi")
--->使用db.auth(“root”,”lianshi”)啟用auth認(rèn)證,看到返回的值為1,這就表示啟動成功了,然后我們再使用命令查看用戶和數(shù)據(jù)庫。
4.1、mytest數(shù)據(jù)庫授權(quán)登錄
1、mongo訪問
> use mytest; switched to db mytest > show users 2020-06-21T21:25:41.293+0800 E QUERY [js] uncaught exception: Error: command usersInfo requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1638:15 shellHelper.show@src/mongo/shell/utils.js:883:9 shellHelper@src/mongo/shell/utils.js:790:15 @(shellhelp2):1:1 --->報(bào)錯沒有權(quán)限
2、用戶和密碼登錄用戶
> db.auth("cg","lianshi");
使用db.auth(“cg”,”lianshi”)啟用auth認(rèn)證,看到返回的值為1,這就表示啟動成功了,然后我們再使用命令查看用戶和數(shù)據(jù)庫。
> show dbs mytest 0.000GB > db.student.insert({"id":"2","name":"yxy"}) WriteResult({ "nInserted" : 1 })
其他用戶命令:
1、創(chuàng)建普通用戶(創(chuàng)建用戶cg,對mytest數(shù)據(jù)庫讀寫權(quán)限)
> db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})
2、刪除用戶>db.dropUser("yonghu")
3、修改用戶密碼
db.updateUser("cg",{pwd:"123456"})
4、進(jìn)入數(shù)據(jù)mytest,用戶名密碼認(rèn)證
> db.auth("cg","lianshi");
5、客戶端工具授權(quán)登錄連接mongo數(shù)據(jù)庫
用戶名和密碼連接數(shù)據(jù)庫
到此這篇關(guān)于MongoDB4.28開啟權(quán)限認(rèn)證配置用戶密碼登錄功能的文章就介紹到這了,更多相關(guān)MongoDB權(quán)限認(rèn)證登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Node操作MongoDB數(shù)據(jù)庫的方法
這篇文章主要介紹了使用Node操作MongoDB數(shù)據(jù)庫的方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01MongoDB學(xué)習(xí)以及集群搭建的實(shí)踐全紀(jì)錄
這篇文章主要給大家介紹了關(guān)于MongoDB學(xué)習(xí)以及集群搭建的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09