mysql连接失败:The driver has not received any packets from the server

mysql连接失败:The driver has not received any packets from the server

java spring项目连接mysql报错,可以确认mysql自身没问题,数据库权限及网络连接均正常

Mysql数据库版本:mysql-5.7.30
Mysql客户端:mysql-connector-j-8.0.31.jar
HikariCP-4.0.3.jar

应用日志报错如下:
2024-03-26 14:31:27.706 ERROR [1E11EB80F4FF41DAB61299E0871A7F69] HikariPool throwPoolInitializationException (594): HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
        ...
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:120)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
        ... 103 common frames omitted
Caused by: java.net.SocketTimeoutException: connect timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        ...
        at java.net.Socket.connect(Socket.java:589)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
        ... 106 common frames omitted
配置文件:
spring:
  datasource:
    url: mydbm01.sudops.com:3306/sudops?useSSL=false&serverTimezone=UTC
    username: dbuesr
    password: dbpwd

解决方案:

修改mysql连接串,增加 autoReconnect 和 failOverReadOnly
jdbc:mysql://mydbm01.sudops.com:3306/sudops?useSSL=false&serverTimezone=UTC&autoReconnect=true&failOverReadOnly=false 释义: autoReconnect: 当数据库连接异常中断时,是否自动重新连接? failOverReadOnly: 自动重连成功后,连接是否设置为只读?