springboot 链接mysql8避坑

MySQL · 2020-05-08 · 199 人浏览

5.x版本是:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc\:mysql\://localhost\:3306/test1?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull

spring.datasource.username=xxx

spring.datasource.password=xxxx

8.x版本就是

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc\:mysql\://localhost\:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior\=CONVERT_TO_NULL&serverTimezone=UTC&useSSL=false

spring.datasource.username=xxx

spring.datasource.password=xxxx

变动的地方用红色标明

错误一:

java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.避坑指南

最近遇到了 java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.这个报错。

再此分享,希望能够对其他人有帮助,

主要原因8.x版本的验证模块和之前版本不同:

5.x版本是:default_authentication_plugin=mysql_native_password

8.x版本就是:default_authentication_plugin=caching_sha2_password

网上的主要解决方案是:

1 修改mysql的配置文件为旧的模块

2 运行命令将某个用户的验证模块改为旧版的模块

但是我更建议用另外一种方案,更新mysql驱动的jar版本,可以修改为8.0.11版本


因为MySQL的版本升级就是为了更加安全,不应该为了迁就旧的驱动而修改验证插件。

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

<dependency>

   <groupId>mysql</groupId>

   <artifactId>mysql-connector-java</artifactId>

   <version>8.0.11</version>

</dependency>

错误二:

问题一:WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 

问题二: Communications link failure

问题三: Unknown system variable ‘query_cache_size’

问题中的SSL问题需要通过在url配置中设置useSSL来解决。useSSL主要是用于mysql版本不匹配问题设置的,在mysql8.0以上版本一般由于兼容性问题都会用到这个配置。问题二和三均是mysql版本与驱动不匹配的问题mysql-connector-java版本号要8.0.x以上

错误三:

java.sql.SQLException: 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.

新版mysql驱动的url必须设置时区,即serverTimezone=UTC,否则会报如上错误

误四:

The connection property 'zeroDateTimeBehavior' acceptable values are: 'CONVERT_TO_NULL', 'EXCEPTION' or 'ROUND'. The value 'convertToNull' is not acceptable.

还是mysql-connector-java 依赖版本的问题使用低版本5.x版本问题不存在

注意:使用8.0.12版本时,配置文件的  spring.datasource.driverClassName 需要修改为 com.mysql.cj.jdbc.Driver