为什么iptables不允许MySQL连接?

由于某些原因,不允许来自特定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 

这里有趣的是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 

这里是我从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 

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

 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) 

另外(因为这是从一台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 

另外请注意skip_networkingoff

首先,你是否改变了my.cnf来configurationMySQL来监听服务器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: '[email protected]' (Using password: YES)"的错误消息,则可以解决问题

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

我看到–dport而不是–dport,也许22和80的作品,因为他们被允许在另一个规则,它不能适用这条线?