Java/Spring连接MySQL出现Communications link failure解决

2021-12-01 56点热度 0人点赞 0条评论

今天启动一个交接的项目,发现启动报错,如下错误提示:

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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)

解决办法

通过研究,发现如下解决办法,把mysql配置连接的url增加如下参数:

autoReconnect=true&allowPublicKeyRetrieval=true

完整的url例子:

url: jdbc:mysql://localhost:3306/dbname?useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowMultiQueries=true&autoReconnect=true&allowPublicKeyRetrieval=true

 

admin

这个人很懒,什么都没留下

文章评论

*

code