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

来源:互联网 发布:io流 java面试题 编辑:程序博客网 时间:2024/05/29 07:11

首先,我是通过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 NEWACCEPT     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

原创粉丝点击