Windows MongoDB安裝和開啓用戶驗證

Windows MongoDB安裝和開啓用戶驗證
一.下載安裝包mongodb-win32-x86_64-2012plus-v4.2-latest-signed.msi
羣裏有安裝包和配置文檔,如有問題歡迎交流
Windows MongoDB安裝和開啓用戶驗證

二.安裝

Windows MongoDB安裝和開啓用戶驗證

Windows MongoDB安裝和開啓用戶驗證

注意:選擇自定義安裝
Windows MongoDB安裝和開啓用戶驗證
D:\Program Files\MongoDB\Server\4.2\

D:\Program Files\MongoDB\Server\4.2\data\
D:\Program Files\MongoDB\Server\4.2\log\
Windows MongoDB安裝和開啓用戶驗證
Compass是mongodb客戶端軟件,暫不安裝
Windows MongoDB安裝和開啓用戶驗證

允許程序在此計算機上安裝軟件

三.啓動 以管理員身份運行cmd
net start mongodb
Windows MongoDB安裝和開啓用戶驗證
計算機右鍵管理也可以管理服務(關閉,開啓)
Windows MongoDB安裝和開啓用戶驗證

安裝客戶端工具Navicat121_Mongodb_x64並連接(無認證)
Windows MongoDB安裝和開啓用戶驗證

四.添加用戶
連接上mongo 執行
use admin;
4.1 創建超級管理用戶
db.createUser({
user:'root',
pwd:'123456',
roles:[{role:'root',db:'admin'}]
})
查看用戶 show users;
Windows MongoDB安裝和開啓用戶驗證
4.2 爲testdb庫創建讀寫用戶,onlinedb庫創建只讀用戶 cooper
即cooper可以讀寫testdb,只能讀onlinedb的數據
use admin
db.createUser({
user:'cooper',
pwd:'123456',
roles:[{role:'readWrite',db:'testdb'},{role:'read',db:'onlinedb'}]
})

root是最高權限可以管理所有數據庫
use testdb;
db.user.insert({_id:1,name:"cooper",age:12})
db.user.find()
use onlinedb;
db.stu.insert({_id:1,name:"coco",age:22})
db.stu.find()

五.修改配置文件添加認證 "D:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg"
文件中添加,然後保存
security:
authorization: "enabled"
六.重啓服務1. net stop mongodb 2. net start mongodb
Windows MongoDB安裝和開啓用戶驗證

七.重啓mongodb
以管理員身份運行cmd
net start mongodb
net stop mongodb

八.連接
Windows MongoDB安裝和開啓用戶驗證
Windows MongoDB安裝和開啓用戶驗證
cooper 用戶登錄驗證權限
use testdb;
db.user.insert({_id:2,name:"sam",age:32})
db.user.find()
Windows MongoDB安裝和開啓用戶驗證
use onlinedb;
db.stu.insert({_id:2,name:"cici",age:23})
db.stu.find()
cooper 用戶對onlinedb庫只讀,不能修改
db.stu.insert({_id:2,name:"cici",age:23})

[Error] not authorized on onlinedb to execute command { insert: "stu", ordered: true, $db: "onlinedb", lsid: { id: UUID("e4bdfbd1-c3ca-4c2a-8fa4-476580aec004") } }

十.總結 先創建用戶再開啓認證

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章