启用IPTable时,LAN上的MySQL无法正常工作

时间:2014-10-23 14:51:35

标签: php mysql apache iptables centos6.5

我有两个Centos VM。 VM_1上的IP地址为 10.99.0.10 ,VM_2为 10.99.0.12 。 Apache和PHP在VM_1中,MySQL在VM_2中。两者都有IPTables规则。 VM_2可以正常使用规则。现在我正在从VM_1进行测试。

首先,我禁用了VM_1 IPTables并连接到VM_2 MySQL(已成功连接)。

[root@foster ~]# service IPTables  stop
IPTables : Applying firewall rules:                         [  OK  ]
[root@foster ~]# mysql -h 10.99.0.12 -u root -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.6.21 MySQL Community Server (GPL)

接下来,我启用了VM_1 IPTables并连接到VM_2 MySQL(它也不会在数小时和数小时内响应)。

[root@foster ~]# service IPTables  start
IPTables : Applying firewall rules:                         [  OK  ]
[root@foster ~]# mysql -h 10.99.0.12 -u root -p
Enter password:

我的IPTables规则有什么问题?我的规则在Pastebin

1 个答案:

答案 0 :(得分:2)

问题在于您启用MySQL流量的方法:

# Allow MySQL private Networking
sudo iptables -A INPUT -i eth1 -p tcp -s 10.99.0.12 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -o eth1 -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT

这些规则有两个问题:

  1. 仅当首次从MySQLVM_1)发起连接时,他们才允许VM_2来自10.99.0.12的流量。
  2. 它们将端口3306指定为客户端(VM_1)端口,而不是服务器(VM_2)端口。
  3. 更合适的规则集如下:

    # Allow MySQL private Networking
    sudo iptables -A OUTPUT -o eth1 -p tcp --dport 3306 -m state --state NEW, ESTABLISHED -j ACCEPT
    sudo iptables -A INPUT -i eth1 -p tcp -s 10.99.0.12 --sport 3306 -m state --state ESTABLISHED -j ACCEPT