首先,我是通过Navicat远程连接数据库,使用了SSH配置。当然连接也是成功的!
但是,当我通过java工程,使用Mysql的驱动来连接时,就连接不上了,报错Communications link failure。
后面发现是防火墙端口没有开。
Linux发行版:CentOS7
要打开防火墙,就先要去/etc/sysconfig下找到iptables文件,修改它添加上对应端口即可。
这里可以参照:
但是我在sysconfig里没有找到iptables,很惊讶。搜索后发现CentOS7用了firewall替代了iptables,所以应该修改firewall。
(当然你依然可以使用iptables,只是需要yum安装罢了。)
修改firewall,添加mysql3306端口
firewall-cmd --add-port=3306/tcp --permanent
//重启防火墙
firewall-cmd --reload
防火墙状态前后对比
前:
Chain IN_public_allow (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
后:
Chain IN_public_allow (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 ctstate NEW
更多的firewall命令,参照以下链接: