在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的安全认证。