在CentOS上配置MongoDB的网络设置涉及多个步骤,包括修改网络接口配置文件、配置防火墙以及设置MongoDB服务以使用特定的网络接口。以下是详细的步骤指南:
1. 配置网络接口静态IP配置(推荐用于生产环境):
查看当前网卡名称:
ip a # 或者 ifconfig
关闭防火墙和SELinux(测试环境推荐):
systemctl stop firewalldsystemctl disable firewalldsed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/configsetenforce 0
修改网卡配置文件:文件路径为 /etc/sysconfig/network-scripts/ifcfg-ens33
(根据实际网卡名称调整)。
BOOTPROTO=staticonBOOT=yesIPADDR=192.168.1.100 # 静态IP地址NETMASK=255.255.255.0 # 子网掩码GATEWAY=192.168.1.1 # 网关DNS1=8.8.8.8 # 主DNSDNS2=114.114.114.114 # 备用DNS
重启网络服务:
systemctl restart network
动态IP配置(适用于开发测试环境):
简化配置:
BOOTPROTO=dhcponBOOT=yes
手动激活网卡(如果网卡未启动):
ifup ens33
编辑MongoDB配置文件:文件路径为 /etc/mongod.conf
。
net:port: 27017bindIp: 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 --permanentfirewall-cmd --reload
检查防火墙状态:
firewall-cmd --list-ports
5. MongoDB服务状态检查启动服务:
sudo systemctl start mongod
检查服务状态:
sudo systemctl status mongod
停止服务:
sudo systemctl stop mongod
开机自启动:
sudo systemctl enable mongod