啟動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的數據庫完全一致。