MongoDB許可權配置
阿新 • 來源:網路 • 發佈:2021-04-01
- 前提:安裝好了mongodb
- 建立賬號
Read:允許使用者讀取指定資料庫
readWrite:允許使用者讀寫指定資料庫
dbAdmin:允許使用者在指定資料庫中執行管理函式,如索引建立、刪除,檢視統計或訪問system.profile
userAdmin:允許使用者向system.users集合寫入,可以找指定資料庫裡建立、刪除和管理使用者
clusterAdmin:只在admin資料庫中可用,賦予使用者所有分片和複製集相關函式的管理許可權。
readAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀許可權
readWriteAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀寫許可權
userAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的userAdmin許可權
dbAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的dbAdmin許可權。
root:只在admin資料庫中可用。超級賬號,超級許可權。
mongo
use admin
#建立一個管理賬號(role有個表參照下面)
db.createUser({user:'root',pwd:'root',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})
- 修改配置
啟用安全規則
security:
authorization: enabled
繫結外網IP並允許其他IP訪問,這樣其他機器就可以通過192.168.0.159來訪問了
net:
port: 27017
bindIp: 192.168.0.159
bindIpAll: true
vi /etc/mongod.conf
內容如下
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 192.168.0.159
bindIpAll: true
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
- 重啟mongod服務
sudo systemctl restart mongod
由於MongoDB的使用者是分資料庫的,分角色的,我們實際業務操作資料庫讀寫的時候,到業務資料庫另外建立一個賬號並賦予讀寫角色即可,下面示範,假定我們的資料庫是mall
mongo
use admin
db.auth('root','root')
use mall
db.createUser({user:'root',pwd:'yourpassword',roles:[{role:'readWrite',db:'mall'}]})

