ubuntu16.04下eclipse使用 jdbc连接本地mysql出现:Communications link failure错误

来源:互联网 发布:会计电算化软件 编辑:程序博客网 时间:2024/06/06 20:04
具体错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


被这个问题困扰类不少时间,网上帖子看了一遍有一遍,归纳了3种解决方式:

1.首先考虑 配置文件/etc/mysql/my.cnf
    网上说是出现这个问题是因为超时,数据库连接(java.sql.Connection)连接关闭,因此可以设置连接最长时间,wait_timeout的默认最长时间是28800秒,也就是8小时时间

     以root打开该文件:sudo gedit /etc/mysql/my.cnf

     再末尾添加:

     [mysqld]
     wait_timeout=31536000
     interactive_timeout=31536000

    保存退出

2.考虑是否是驱动版本问题,比如:将mysql-connector-java-5.1.14-bin.jar换成mysql-connector-java-5.1.19-bin.jar试试;

3.将 String url = "jdbc:mysql://localhost:3306/test";//?autoReconnect=true&failOverReadOnly=false

    中的localhost换成127.0.0.1


  

0 0