以下是在CentOS上設置MongoDB權限的指南:
使用命令sudo yum install -y mongodb-org
安裝MongoDB。
執行sudo systemctl start mongod
啟動服務,sudo systemctl enable mongod
設置開機自啟。
連接到MongoDB shell,使用use admin
切換到admin數據庫,然后執行db.createUser({ user: "admin", pwd: "your_password", roles: [ { role: "root", db: "admin" } ]})
創建管理員用戶。
切換到目標數據庫,如use mydatabase
,再執行db.createUser({ user: "myuser", pwd: "myuser_password", roles: [ { role: "readWrite", db: "mydatabase" } ]})
為用戶分配權限。
編輯/etc/mongod.conf
文件,設置security.authorization: enabled
,然后重啟MongoDB服務sudo systemctl restart mongod
。
使用db.getUser("username")
查看用戶角色和權限信息,也可嘗試執行相關操作來驗證。