網站首頁 編程語言 正文
MongoDB默認不啟用授權認證,只要能連接到該服務器,就可連接到mongod。若要啟用安全認證,需要更改配置文件mongdb.conf中的參數auth。
MongoDB的用戶是跟數據庫相關聯的,具體的數據庫,需要有對應的用戶,超級管理員也不能操作其他數據庫的。
MongoDB存儲所有的用戶信息在admin 數據庫的集合system.users中,保存用戶名、密碼和數據庫信息。
MongoDB開啟權限認證:配置用戶名和密碼認證登錄,操作步驟:
1、查看是否開啟認證登錄
$cd /usr/local/mongodb/bin $cat mongodb.conf
#數據文件存放目錄
dbpath = /usr/local/mongodb/data
#日志文件存放目錄
logpath = /usr/local/mongodb/logs/mongodb.log
logappend=true
#端口
port = 27017
#以守護程序的方式啟用,即在后臺運行
fork = true
#認證模式(true代表開啟認證登錄,false代表未開啟認證登錄)
auth=false ??
#遠程連接
bind_ip=0.0.0.0
2、開啟用戶名和密碼認證(創建用戶均需進入admin數據庫)
2.1、為admin數據庫創建管理員賬號
1、數據庫admin創建管理員賬號
[root@hadoop-master bin]# mongo > use admin > db.createUser({user:"root",pwd:"lianshi",roles:["root"]})
2、查看目前用戶
> show users
2.2、為數據庫mytest創建普通用戶
1、給數據庫mytest創建cg用戶
>use mytest > db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})
2、查看目前用戶
> show users >db.system.users.find()命令可以查看新創建的用戶
2.3、配置文件開啟用戶名密碼認證
#認證模式(true代表開啟認證登錄,false代表未開啟認證登錄) auth=true
3、重啟mongo服務
[root@hadoop-master bin]# ps -ef |grep mongo [root@hadoop-master bin]# kill -9 15231 $./mongod -f mongodb.conf
4、mongo授權訪問
4.1、admin數據庫授權登錄
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 -->授權配置并重啟后,此時查看用戶,會發現沒有權限
2、用用戶和密碼登錄
> db.auth("root","lianshi")
--->使用db.auth(“root”,”lianshi”)啟用auth認證,看到返回的值為1,這就表示啟動成功了,然后我們再使用命令查看用戶和數據庫。
4.1、mytest數據庫授權登錄
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 --->報錯沒有權限
2、用戶和密碼登錄用戶
> db.auth("cg","lianshi");
使用db.auth(“cg”,”lianshi”)啟用auth認證,看到返回的值為1,這就表示啟動成功了,然后我們再使用命令查看用戶和數據庫。
> show dbs mytest 0.000GB > db.student.insert({"id":"2","name":"yxy"}) WriteResult({ "nInserted" : 1 })
其他用戶命令:
1、創建普通用戶(創建用戶cg,對mytest數據庫讀寫權限)
> db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})
2、刪除用戶>db.dropUser("yonghu")
3、修改用戶密碼
db.updateUser("cg",{pwd:"123456"})
4、進入數據mytest,用戶名密碼認證
> db.auth("cg","lianshi");
5、客戶端工具授權登錄連接mongo數據庫
用戶名和密碼連接數據庫
原文鏈接:https://blog.csdn.net/fen_fen/article/details/106891911
相關推薦
- 2022-07-22 EasyExcel導出Excel 通過 RGB 設置 表頭顏色
- 2022-05-17 qt 解決Error while building/deploying project Hmi (k
- 2024-02-25 Navicat提示Access violation at address **** in modul
- 2022-12-23 C++成員函數如何當作回調函數同時傳遞this指針_C 語言
- 2022-06-02 基于python的MD5腳本開發思路_python
- 2022-12-12 flutter中如何使用和擴展ThemeData實現詳解_Dart
- 2022-12-14 C++?Boost?shared_ptr共享指針詳細講解_C 語言
- 2022-08-17 python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支