在CentOS系統下,為MongoDB配置防火墻規則需要使用firewalld
服務。以下是配置防火墻規則的步驟:
firewalld
服務。如果沒有安裝,可以使用以下命令安裝:sudo yum install firewalld
firewalld
服務,并設置開機自啟:sudo systemctl start firewalld
sudo systemctl enable firewalld
firewalld
服務使用的是public
區域。為了允許MongoDB的連接,我們需要創建一個新的區域(例如:mongodb
),并將MongoDB的端口(默認為27017)添加到該區域。sudo firewall-cmd --permanent --new-zone=mongodb
mongodb
區域:sudo firewall-cmd --permanent --zone=mongodb --add-port=27017/tcp
firewalld
配置,使更改生效:sudo firewall-cmd --reload
mongodb
區域:sudo firewall-cmd --permanent --zone=mongodb --add-service=mongodb
firewalld
配置,使更改生效:sudo firewall-cmd --reload
mongodb
區域的配置已生效:sudo firewall-cmd --list-all --zone=mongodb
現在,MongoDB應該已經可以在新創建的mongodb
區域中接受連接了。請確??蛻舳耸褂谜_的IP地址和端口連接到MongoDB服務器。如果需要允許遠程連接,請確保MongoDB配置文件(通常位于/etc/mongod.conf
)中的bindIp
設置允許遠程IP地址。