简介:本文详细介绍如何在不同防火墙环境下(iptables、ufw、firewalld、云平台安全组)配置规则,允许外部访问MySQL服务的3306端口,同时保持系统安全性。内容涵盖规则配置原理、具体操作步骤、验证方法及安全建议。
在配置防火墙允许MySQL访问前,需理解TCP/IP网络通信的基本原理。MySQL默认使用3306端口进行客户端连接,防火墙规则的核心是允许该端口的入站(INBOUND)和出站(OUTBOUND)流量通过。
# 允许特定IP访问MySQLiptables -A INPUT -p tcp --dport 3306 -s 192.168.1.100 -j ACCEPT# 允许网段访问(示例:192.168.1.0/24)iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.0/24 -j ACCEPT# 保存规则(CentOS 6)service iptables save# CentOS 7使用:iptables-save > /etc/sysconfig/iptables
关键参数说明:
-A INPUT:追加到入站规则链-p tcp:指定TCP协议--dport 3306:目标端口3306-s:指定源IP或网段-j ACCEPT:允许匹配的流量
# 允许特定IPsudo ufw allow from 192.168.1.100 to any port 3306 proto tcp# 允许网段sudo ufw allow from 192.168.1.0/24 to any port 3306 proto tcp# 启用防火墙(如果未启用)sudo ufw enable
安全建议:
ufw status numbered查看规则序号,便于管理sudo ufw status verbose
# 添加富规则(推荐方式)sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4"source address="192.168.1.100"port protocol="tcp" port="3306" accept'# 允许网段sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4"source address="192.168.1.0/24"port protocol="tcp" port="3306" accept'# 重新加载配置sudo firewall-cmd --reload
优势说明:
--permanent参数使规则持久化最佳实践:
注意事项:
# 允许特定IP访问New-NetFirewallRule -DisplayName "Allow MySQL from 192.168.1.100" `-Direction Inbound -LocalPort 3306 -Protocol TCP `-Action Allow -RemoteAddress 192.168.1.100# 允许网段访问New-NetFirewallRule -DisplayName "Allow MySQL from 192.168.1.0/24" `-Direction Inbound -LocalPort 3306 -Protocol TCP `-Action Allow -RemoteAddress 192.168.1.0/24
进阶配置:
-EdgeTraversalPolicy Allow允许穿越NAT设备-Profile Domain,Private限制网络位置-Enabled True确保规则激活
# Linux系统测试telnet <服务器IP> 3306# 或使用nc工具nc -zv <服务器IP> 3306# MySQL客户端测试mysql -h <服务器IP> -u username -p
预期结果:
连接超时:
netstat -tulnp | grep 3306)权限拒绝:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password';FLUSH PRIVILEGES;
bind-address配置(/etc/my.cnf或/etc/mysql/my.cnf)规则不生效:
最小权限原则:
%通配符时需谨慎端口变更:
port=3307)IP白名单管理:
日志监控:
# iptables日志配置iptables -A INPUT -p tcp --dport 3306 -j LOG --log-prefix "MYSQL_ACCESS: "# 查看日志tail -f /var/log/kern.log | grep MYSQL_ACCESS
- name: Configure MySQL firewall ruleshosts: db_serversbecome: yestasks:- name: Allow MySQL from specific IP (iptables)iptables:chain: INPUTprotocol: tcpdestination_port: 3306source: 192.168.1.100jump: ACCEPTaction: insertrule_num: 1when: ansible_os_family == "RedHat" and ansible_distribution_major_version == "7"- name: Allow MySQL from subnet (ufw)ufw:rule: allowport: "3306"proto: tcpsrc: 192.168.1.0/24when: ansible_os_family == "Debian"
#!/bin/bash# MySQL防火墙配置脚本MYSQL_PORT=3306ALLOWED_IPS=("192.168.1.100" "192.168.1.101")# 检测防火墙类型if command -v ufw &> /dev/null; thenecho "检测到UFW防火墙"for ip in "${ALLOWED_IPS[@]}"; dosudo ufw allow from "$ip" to any port "$MYSQL_PORT" proto tcpdoneelif command -v firewall-cmd &> /dev/null; thenecho "检测到firewalld"for ip in "${ALLOWED_IPS[@]}"; dosudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4'source address='$ip'port protocol='tcp' port='$MYSQL_PORT' accept"donesudo firewall-cmd --reloadelseecho "未检测到支持的防火墙,尝试iptables"for ip in "${ALLOWED_IPS[@]}"; dosudo iptables -A INPUT -p tcp --dport "$MYSQL_PORT" -s "$ip" -j ACCEPTdone# 提示用户保存规则echo "请手动保存iptables规则(如:service iptables save)"fi
配置检查清单:
通过系统化的防火墙配置,既能确保MySQL服务的可用性,又能有效抵御未经授权的访问尝试,构建安全可靠的数据库环境。