在Debian上設置MongoDB的安全認證,可以按照以下步驟進行:
首先,確保你已經安裝了MongoDB。你可以使用以下命令來安裝:
sudo apt update
sudo apt install -y mongodb
安裝完成后,啟動MongoDB服務:
sudo systemctl start mongod
編輯MongoDB的配置文件 /etc/mongod.conf
,找到 security
部分并啟用認證:
security:
authorization: enabled
啟動MongoDB服務后,連接到MongoDB并創建一個管理員用戶:
mongo --eval 'db.createUser({user:"admin", pwd:"your_password", roles:["root"]})'
為了使配置生效,需要重啟MongoDB服務:
sudo systemctl restart mongod
使用新創建的管理員用戶連接到MongoDB:
mongo -u admin -p your_password --authenticationDatabase admin
你可以根據需要創建其他用戶和角色。例如,創建一個只讀用戶:
mongo -u admin -p your_password --authenticationDatabase admin --eval 'db.createUser({user:"readonly_user", pwd:"readonly_password", roles:["readOnly"]})'
如果你使用的是云服務器,確保配置了防火墻規則,允許MongoDB的默認端口(27017):
sudo ufw allow 27017
確保所有用戶都能正確連接并執行相應的操作。
通過以上步驟,你可以在Debian上成功設置MongoDB的安全認證。