温馨提示×

mongodb在centos上的网络配置

小樊
57
2025-07-10 14:15:09
栏目: 云计算

在CentOS上配置MongoDB的网络设置涉及多个步骤,包括修改网络接口配置文件、配置防火墙以及设置MongoDB服务以使用特定的网络接口。以下是详细的步骤指南:

1. 配置网络接口

  • 静态IP配置(推荐用于生产环境)

    1. 查看当前网卡名称:

      ip a # 或者 ifconfig
      
    2. 关闭防火墙和SELinux(测试环境推荐):

      systemctl stop firewalld
      systemctl disable firewalld
      sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
      setenforce 0
      
    3. 修改网卡配置文件:文件路径为 /etc/sysconfig/network-scripts/ifcfg-ens33(根据实际网卡名称调整)。

      BOOTPROTO=static
      ONBOOT=yes
      IPADDR=192.168.1.100 # 静态IP地址
      NETMASK=255.255.255.0 # 子网掩码
      GATEWAY=192.168.1.1 # 网关
      DNS1=8.8.8.8 # 主DNS
      DNS2=114.114.114.114 # 备用DNS
      
    4. 重启网络服务:

      systemctl restart network
      
  • 动态IP配置(适用于开发测试环境)

    1. 简化配置:

      BOOTPROTO=dhcp
      ONBOOT=yes
      
    2. 手动激活网卡(如果网卡未启动):

      ifup ens33
      

2. 配置MongoDB以使用特定网络接口

  • 编辑MongoDB配置文件:文件路径为 /etc/mongod.conf

    net:
      port: 27017
      bindIp: 192.168.1.100 # 替换为你的静态IP地址
    
  • 重启MongoDB服务:

    sudo systemctl restart mongod
    

3. 验证网络配置

  • 测试外网连通性:

    ping www.baidu.com
    
  • 查看路由表:

    ip route show
    
  • 检查DNS配置:

    cat /etc/resolv.conf
    

4. 防火墙配置

  • 开放MongoDB端口:

    firewall-cmd --zone=public --add-port=27017/tcp --permanent
    firewall-cmd --reload
    
  • 检查防火墙状态:

    firewall-cmd --list-ports
    

5. MongoDB服务状态检查

  • 启动服务:

    sudo systemctl start mongod
    
  • 检查服务状态:

    sudo systemctl status mongod
    
  • 停止服务:

    sudo systemctl stop mongod
    
  • 开机自启动:

    sudo systemctl enable mongod
    

0