java工程远程Linux服务器上的Mysql

xiaoxiao2021-02-27  198

首先,我是通过Navicat远程连接数据库,使用了SSH配置。当然连接也是成功的! 但是,当我通过java工程,使用Mysql的驱动来连接时,就连接不上了,报错Communications link failure。 后面发现是防火墙端口没有开。

Linux发行版:CentOS7 要打开防火墙,就先要去/etc/sysconfig下找到iptables文件,修改它添加上对应端口即可。 这里可以参照:

http://www.cnblogs.com/Struts-pring/p/5407509.html

但是我在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命令,参照以下链接:

sodino http://blog.csdn.net/sodino/article/details/52356472

转载请注明原文地址: https://www.6miu.com/read-11871.html

最新回复(0)