ubuntu

如何配置Ubuntu MongoDB安全

小樊
37
2025-09-11 00:12:23
栏目: 云计算

以下是配置Ubuntu MongoDB安全的关键步骤:

  1. 安装MongoDB
    使用官方仓库安装最新版:

    sudo apt update  
    sudo apt install -y mongodb-org  
    
  2. 启用认证
    编辑配置文件 /etc/mongod.conf,添加:

    security:  
      authorization: enabled  
    

    重启服务生效:

    sudo systemctl restart mongod  
    
  3. 创建管理员用户
    进入MongoDB Shell,切换到admin数据库并创建用户:

    use admin  
    db.createUser({  
      user: "admin",  
      pwd: "强密码",  
      roles: [{ role: "root", db: "admin" }]  
    })  
    

    之后需使用该用户认证登录:

    mongo -u admin -p "强密码" --authenticationDatabase admin  
    
  4. 限制IP访问
    在配置文件中设置bindIp为特定IP(如127.0.0.1或服务器IP),避免暴露在公网:

    net:  
      bindIp: 127.0.0.1  
    

    重启服务:

    sudo systemctl restart mongod  
    
  5. 配置防火墙
    使用ufw限制MongoDB端口(默认27017)的访问:

    sudo ufw allow from <信任IP> to any port 27017  
    sudo ufw enable  
    
  6. 启用加密(可选)

    • 传输加密:在配置文件中添加SSL/TLS配置(需提前准备证书):
      net:  
        ssl:  
          mode: requireSSL  
          sslPEMKeyFile: /path/to/mongodb.pem  
          sslCAFile: /path/to/ca.pem  
      
    • 存储加密:启用WiredTiger存储引擎的加密功能(需配置密钥文件)。
  7. 定期维护

    • 备份数据:使用mongodump定期备份数据库。
    • 监控审计:启用审计日志记录操作(需在配置文件中配置systemLog路径及格式)。
  8. 更新与补丁管理
    定期检查MongoDB官方更新,及时安装安全补丁。

注意:生产环境中需根据实际需求调整权限和加密策略,避免使用默认配置。参考来源:[1,2,3,4,5,6,7,8,9,10,11]

0
看了该问题的人还看了