无法连接到端口 3306

无法连接到端口 3306

我在 VPS 上运行 CentOS,并尝试远程访问我的 MySQL 数据库。因此,我使用以下命令在 iptables 中添加了一行:

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

但是我无法访问我的 mysql 数据库。当我使用 nmap 进行端口扫描时,结果如下:

>nmap -p 3306 x.x.x.x
>3306/tcp filtered mysql

因此 stat 不是OPEN。这些是 VPS 上的所有输入规则。有一行(已经在那里)包含REJECT。这是否阻止了到端口 3306 的流量?

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:ftp
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:smtp
ACCEPT     udp  --  anywhere             anywhere            state NEW udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:pop3
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:imap
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:submission
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:imaps
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:pop3s
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:EtherNet/IP-1
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpts:35000:35999
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:mysql
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:mysql

提前致谢!

我的.cnf文件:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
bind-address=x.x.x.x

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

答案1

编辑:我更彻底地查看了您的 iptables 输出并注意到您在那里遇到了一些问题。

REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:mysql
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:mysql

在明确允许 MySQL 流量的规则有机会匹配之前,您拒绝了所有流量。您需要重新排序这些规则。


旧答案:

您的 MySQL 服务器可能没有主动监听远程连接。

命令“ss -ln | grep 3306”可能不会返回任何结果。

检查 MySQL 配置文件中的关键字 skip-networking 和 bind-address。如果配置中存在 skip-networking,请将其注释掉或删除。确保 bind-address 设置为 0.0.0.0(“通配符”)或系统 IP 地址。重新启动 MySQL 以应用配置更改。

您需要确保您没有任何帐户可以使用空密码或简单密码供其他人访问。除了 MySQL 用户和数据库 ACL 之外,强烈建议限制对 MySQL 端口的远程访问。

答案2

这应该是一条评论而不是答案,我没有足够的代表。

3306/TCP已过滤mysql

你的防火墙可能出了问题。如果 mysql 由于未绑定到网络而不接受到 3306 的连接,则应该是关闭

tcpdump是您调试问题的朋友,尝试检查数据包是否真正到达

需要指出的是,过滤状态通常与防火墙问题有关,并且关闭时实际服务不具有约束力

答案3

按顺序检查 netfilter 防火墙中的规则,直到找到第一个匹配项。

iptables 的 -A 开关在结尾现有配置。

您现有的配置打开了多个端口,并以匹配并拒绝所有之前未打开的端口的规则结束。然后您添加了 MySQL 端口。

该规则永远不会被达到,并且 MySQL 端口仍然关闭。

您应该使用 iptables 的 -I 开关在防火墙配置的开头插入新规则。

相关内容