启动springboot后遇到错误:
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:172) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar:8.0.11] ....(手动省略) Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
然后查找相关资料,发现各大网站千篇一律的都是数据库连接失败,新增两个配置,配置如下:
#下面这两个配置,可以在每次连接的时候判断一些连接是否有效 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.test-while-idle=true
以上两个配置
test-on-borrow=true 表示当应用向连接池申请连接时,连接池会判断这条连接是否是可用的
test-while-idle=true 表示申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
于是我将其加入application.properties重启项目,错误依旧存在。
考虑到我使用的数据库是MySQL8,于是仔细检查了我的驱动
application中的驱动
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
pom.xml中的配置
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
咋一看好像没什么问题,驱动类加了 cj,版本也是用的MySQL8.0的版本。
我又检查了一下MySQL的版本 8.0.19,和8.0.11差了一点点,于是我抱着尝试的心态把version改成了8.0.19。果然报错的内容起了变化:
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_172] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_172] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_172] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_172] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2118) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2142) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar:8.0.19] ... 76 common frames omitted
出现了乱码的情况,这个比较好处理,经查找是因为时差的原因,于是在配置文件的url后面添加&serverTimezone=Asia/Shanghai
完整url如下
spring.datasource.url = jdbc:mysql://localhost:3306/mydevotion?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
至此,springboot正常启动,问题解决。
在pom.xml中配置的mysql的版本号,一定要跟所连接的MySQL的数据库完全一致。