網(wǎng)站首頁 編程語言 正文
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ù)程序的方式啟用,即在后臺運行
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)配置并重啟后,此時查看用戶,會發(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 --->報錯沒有權(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ù)庫
原文鏈接:https://blog.csdn.net/fen_fen/article/details/106891911
相關(guān)推薦
- 2022-06-11 詳解如何在Flutter中獲取設(shè)備標(biāo)識符_Android
- 2022-12-06 Pycharm配置anaconda環(huán)境圖文教程_python
- 2022-07-02 Python?matplotlib繪圖時使用鼠標(biāo)滾輪放大/縮小圖像_python
- 2022-07-13 python版jpeg合成pdf兩種方法
- 2022-05-22 Nginx基礎(chǔ)location語法及功能配置實例_nginx
- 2022-04-11 python制作簡單計算器功能_python
- 2023-10-10 css像素的問題 物理像素 獨立像素 設(shè)備像素比
- 2022-07-20 react中事件處理與柯里化的實現(xiàn)_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支