连接MySql报错(时区错误)——The server time zone value "" is unrecognized or represents more t...

在使用SpringBoot创建并启动项目时,出现的一个SqlSQLException。经查验,该问题是MySql本身的时区设置的问题导致的。完整的异常信息如下:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents morethan 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.

/**
*谷歌翻译(英文是真的垃圾,唉):
java.sql.SQLException:服务器时区值'Öйú±ê׼ʱ¼ä'无法识别或代表多个时区。 
如果要使用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值。
**/

MySql安装默认设置为美国时区,而北京时间比美国迟8小时。解决方法也很简单:

如果安装MySql时没有更改过默认安装路径,那么在默认的安装路径下C:\ProgramData\MySQL\MySQL Server 8.0找到my.ini文件。

服务安装地址.png


[mysqld]下方添加: default-time-zone='+08:00'(注意引号)

修改位置.png

因为是在springboot项目启动时报的,与mysql连接有关的错,找到了另外一种解决错误的方式

# 数据库基本配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jpaLearn?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=sunyu
spring.datasource.password=sunyu

spring.datasource.url 这个配置中多加了 serverTimezone=UTC

发布了170 篇原创文章 · 获赞 64 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/hy_coming/article/details/104128024
今日推荐