SpringBoot + mybatis + mysql8.0.11 构建项目

  前几天在阐释使用mysql8.0.11,刚解决第三方工具链接没数据库的问题,然后兴高采烈的去搭建项目,进行使用,然后又是一种一种的问题冒出,具体出现的问题,以及相关的解决方式如下,主要是在建立链接的时候一些配置参数的问题。

  本人使用的是Intellij idea进行框架的搭建,关于如何使用Intellij idea整合SpringBoot + mybatis可以自行百度,也可以参照  https://www.cnblogs.com/hackyo/p/7246541.html。文章中使用的是application.yml配置文件,进行配置,我使用的是application.properties配置文件进行配置,相差不大,初始配置文件为:

#数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/snow?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

  这只是简单的数据源配置,这边需要注意的是,MYSQL8.0.11的驱动为 :com.mysql.cj.jdbc.Driver,所以需要再pom文件中,添加jar包依赖:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
</dependency>

  配置完成,然后就浏览器访问,然后出现了异常:

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 
nestedexception is 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.] with root cause

  查找原因,好像是因为时区的问题,引起的,这里有两个解决方式,

  第一 :将 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/snow?characterEncoding=UTF-8  改为: spring.datasource.url=jdbc:mysql://127.0.0.1:3306/snow?characterEncoding=UTF-8&serverTimezone=GMT%2B8

  第二:在数据库中操作:

show variables like '%time_zone%'
set global time_zone='+8:00';

  先查询数据库的时区,然后在设置为东八区,这里使用的是中国的时区时间

  修改完之后,再次浏览器访问,哎呀,可以获得相关的记过了,但是,在程序的后台,出现了如下的警告:

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.

  虽然是警告,不过看起来还是不是很好看,这个时候就需要在

  spring.datasource.url 的值后边添加 &useSSL=false,这样就可以进行正常使用了。

  不过有的时候也会出现:

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed] with root cause

  这样需要在:spring.datasource.url 的值后边添加 &allowPublicKeyRetrieval=true。

  出现这个异常是我将参数 spring.datasource.username 写成了 spring.datasource.name ,然后出现的异常,改过来之后就不再出现这样的异常。如果有朋友出现了这样的异常,可以使用这种方式进行操作;

  同时,当我使用spring.datasource.name时,即使加上了 &allowPublicKeyRetrieval=true。依然后出现这样的异常:

java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)

  这是一个用户权限的问题,因为springboot在读取参数的时候,读取的是spring.datasource.username的值,由于我没有配置,所以得的是空字符串,而我们并没有添加空字符串用户,所以出现权限问题,如果其他朋友出现了这样的异常,就需要修改你用户的权限了。

  所以在使用的时候,最终的参数配置为:spring.datasource.url=jdbc:mysql://127.0.0.1:3306/snow?characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false,这样就可以正常使用

如果为了保险起见,也是可以写成:spring.datasource.url=jdbc:mysql://127.0.0.1:3306/snow?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true,

然后又有朋友会发现中间还多了一个useUnicode=true,网上有朋友说缺少了这个也是会出现异常的,不过我这边到没有出现,如果有朋友因为缺少这个参数出现了异常,可以回复给我,谢谢。

猜你喜欢

转载自www.cnblogs.com/icesnow521/p/9402780.html
今日推荐