在CentOS上為MongoDB設置用戶權限,你需要遵循以下步驟:
首先,確保已經安裝了MongoDB。如果還沒有安裝,請參考MongoDB官方文檔進行安裝:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
啟動MongoDB服務:
sudo systemctl start mongod
mongo
admin
數據庫:use admin
myUser
的用戶,密碼為myPassword
,并為其分配readWrite
權限,作用于myDatabase
數據庫:db.createUser({
user: "myUser",
pwd: "myPassword",
roles: [
{ role: "readWrite", db: "myDatabase" }
]
})
exit
sudo systemctl restart mongod
現在,你已經成功為MongoDB創建了一個新用戶,并為其分配了相應的權限。你可以使用以下命令連接到MongoDB,使用新創建的用戶進行身份驗證:
mongo -u myUser -p myPassword --authenticationDatabase admin
這將允許你以myUser
的身份訪問myDatabase
數據庫,并具有讀寫權限。