Fork me on GitHub

记录一下Linux下远程访问Mysql连接不上,报错The driver has not received any packets from the server.

问题所在

远程服务器已经提前安装好了MySQL,版本也是对应的。在ssh上可以正常进入MySQL数据库。但是其他地方无法连接上,例如navicat和IDE内部都发生报错。

排查

1. 首先排查了一下远程服务器上mysql服务状态是否处于正常运行状态

sudo service mysqld status

image

没有发生问题。

2. 检查了一下服务器安全组是否放行了对应端口。如3306、80等常用端口

检查安全组进入对应所选用的服务器控制台即可。(腾讯云、阿里云、华为云、AWS)都是类似的。

也没有发生问题。

3. 检查了linux中防火墙是否启用,是否放行了对应端口

Centos7默认使用的是firewalld作为防火墙,不是使用iptables,因此需要先关闭firewalld服务,或者直接使用默认的firewalld防火墙。

  • 使用iptables需要先安装服务 yum install iptables-services

  • 我们可以将其设置为开机启动 systemctl enable iptables.service

  • 关闭firewalld防火墙 systemctl stop firewalld systemctl mask firewalld


接下来就是关键点,我们需要放行对应的端口

  • 放行3306端口: iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
  • 放行443端口: iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  • 放行80端口: iptables -A INPUT -p tcp --dport 80 -j ACCEPT

... 其他的端口也是类似
image

问题解决!!!


firewalld常用命令

  • 查看firewalld防火墙状态 systemctl status firewalld.service

  • 开启firewalld防火墙 systemctl start firewalld.service

  • 关闭firewalld防火墙 systemctl stop firewalld.service

  • 重启firewalld防火墙 systemctl restart firewalld.service

  • 禁止开机启动firewalld防火墙 systemctl disable firewalld.service

  • 设置开机启动firewalld防火墙 systemctl enable firewalld.service

  • 开放端口 firewall-cmd --zone=public --add-port=80/tcp --permanent

  • 重新载入 firewall-cmd --reload

  • 查看对应端口 firewall-cmd --zone=public --query-port=80/tcp

  • 删除对应端口 firewall-cmd --zone=public --remove-port=80/tcp --permanent

posted @ 2023-08-03 18:26  Hui_Li  阅读(1107)  评论(0编辑  收藏  举报