1. 程式人生 > >Mysql連線異常解決方法

Mysql連線異常解決方法

1、解決:Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone valu

報錯資訊為:

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)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
    at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
    ... 17 more



此問題為時區問題,在 JDBC 的連線 url 部分加上 serverTimezone=UTC 即可。 

spring:
  application:
    name: test
  liquibase:
    enabled: false
  datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
      username: root
      password: passwd


如圖: 

2、連線mysql 出現:java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'. 

網上查資料說的是mysql5.x 版本和 8.x版本的區別;

5.7版本是:default_authentication_plugin=mysql_native_password

8.x版本就是:default_authentication_plugin=caching_sha2_password
--------------------- 

mysql驅動已經更新適配了caching_sha2_password 的密碼規則,升級到最新版本就可以了。

改為 :

<version>8.0.11</version>

重新啟動java 伺服器,連線成功。