ubuntu

MongoDB在Ubuntu上的网络配置指南

小樊
47
2025-08-07 18:14:00
栏目: 云计算

以下是在Ubuntu上配置MongoDB网络的步骤及安全建议:

  1. 安装MongoDB

    sudo apt update  
    sudo apt install -y mongodb  
    
  2. 修改配置文件(关键步骤)
    编辑 /etc/mongod.conf,找到 net 部分:

    • 允许所有IP连接bindIp: 0.0.0.0(生产环境不推荐)
    • 仅允许特定IPbindIp: 192.168.1.100,127.0.0.1(替换为实际IP)
    • 修改端口(可选):port: 27017(默认端口,可自定义)
  3. 重启MongoDB服务

    sudo systemctl restart mongod  
    
  4. 配置防火墙(UFW)

    • 允许所有IP访问端口
      sudo ufw allow 27017/tcp  
      sudo ufw reload  
      
    • 仅允许特定IP访问
      sudo ufw allow from <IP_ADDRESS> to any port 27017  
      
  5. 启用安全认证(生产环境必做)

    • mongod.conf 中添加:
      security:  
        authorization: enabled  
      
    • 重启服务后,通过 mongo shell 创建管理员用户:
      use admin  
      db.createUser({  
        user: "admin",  
        pwd: "强密码",  
        roles: [{ role: "userAdminAnyDatabase", db: "admin" }]  
      })  
      
  6. 测试远程连接
    从其他机器执行:

    mongo -h <服务器IP> -u admin -p "密码" --authenticationDatabase admin  
    

安全风险提示

参考来源:

0
看了该问题的人还看了