为什么 iptables 不允许 MySQL 连接?

Leo*_*lis 1 mysql iptables centos

出于某种原因,不允许来自特定 IP 地址的 mysql 连接。这是我的规则(从 中抓取iptables-save):

-A INPUT -s 12.34.56.78/32 -p tcp -m state --state NEW -m multiport --dports 22,80,3306 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

有趣的是,SSH 连接和 HTTP 页面可以完美加载,没有问题。后来我为 MySQL 连接添加了 3306,但它们似乎被忽略了。为什么?

我在 CentOS 上。我已经重新启动了 iptables 服务并将 IPTABLES_SAVE_ON_STOP/RESTART 设置为是。

跑步netstat -tunelp告诉我这个:

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      27         8849593    10712/mysqld
Run Code Online (Sandbox Code Playgroud)

这是我的完整规则列表iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
ACCEPT     tcp  --  192.168.1.0/24       anywhere            state NEW multiport dports ssh,http,mysql
ACCEPT     tcp  --  some-resolved-hostname-1.com  anywhere            state NEW multiport dports ssh,http,mysql
ACCEPT     tcp  --  some-resolved-hostname-2.com  anywhere            state NEW multiport dports ssh,http,mysql
ACCEPT     tcp  --  some-resolved-hostname-3.com  anywhere            state NEW multiport dports ssh,http,mysql
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Run Code Online (Sandbox Code Playgroud)

注意:服务器端没有日志(包括错误日志)。尝试连接时出现的错误是:

D:\>mysql -u username -p -h 12.34.56.78
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '12.34.56.78' (10060)
Run Code Online (Sandbox Code Playgroud)

另外(因为这是来自试图连接的 Windows 机器)我试过这个:

D:\>telnet 12.34.56.78 3306
Connecting To 12.34.56.78...Could not open connection to the host, on port 3306: Connect failed
Run Code Online (Sandbox Code Playgroud)

还要注意的skip_networking是 to off

Ber*_*ert 5

首先,您是否更改my.cnf了配置 MySQL 以侦听服务器 IP 地址上的连接?您的文件应包含以下行:

bind-address = <public_ip_address_of_your_machine>

其次,您是否已授予从该 IP 地址连接的用户在 MySQL 中进行连接的权限?

GRANT ALL ON example.* TO someuser@'12.34.56.78' IDENTIFIED BY 'PASSWORD';

如果您收到类似于以下内容的错误消息,这将解决问题 "Access denied for user: 'someuser@12.34.56.78' (Using password: YES)"

最后,确保您的路由器具有端口转发和防火墙规则,允许传入连接到 MySQL 服务器。