温馨提示×

Debian系统下MongoDB如何安全设置

小樊
42
2025-07-30 12:06:13
栏目: 云计算

在Debian系统下对MongoDB进行安全设置,可以遵循以下步骤:

安装和更新MongoDB

  1. 确保系统最新:
sudo apt update && sudo apt upgrade -y 
  1. 安装MongoDB:
sudo apt install -y mongodb-org 

配置MongoDB

  1. 编辑MongoDB配置文件 /etc/mongod.conf

    • 启用鉴权:
      security: authorization: enabled 
    • 绑定IP地址,限制为本地访问:
      net: bindIp: 127.0.0.1 
    • 设置监听端口(默认27017):
      net: port: 27017 
    • 启用SSL:
      net: ssl: mode: requireSSL PEMKeyFile: /path/to/mongodb.pem CAFile: /path/to/ca.pem 
  2. 重启MongoDB服务

    sudo systemctl restart mongod 

创建管理员账户

  1. 连接到MongoDB shell:
    mongo -u admin -p --authenticationDatabase admin 
  2. 创建管理员账户:
    use admin db.createUser({ user: "myUserAdmin", pwd: "myStrongPassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }) 

启用安全审计

  1. mongod.conf中配置审计日志:
    security: auditLog: destination: file format: JSON path: /var/log/mongodb/audit.json 
  2. 重启MongoDB服务:
    sudo systemctl restart mongod 

配置防火墙

  1. 使用iptables限制访问:
    sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT sudo service iptables save 

定期更新和维护

  1. 保持MongoDB和Debian系统更新:
    sudo apt update && sudo apt upgrade -y 

0