数据库配置,启动项目报错----The Server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone
数据库配置,启动项目报错----The Server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone
这个错误的意思是服务器的区时不对
于是先百度了怎么修改mysql的区时:
首先需要查看MySQL的当前时区,用time_zone参数
通过命令查看当前时区:
show variables like ‘%time_zone%’;
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
这里显示time_zone 是SYSTEM,需要设置时区为东八区(北京区时)(为什么要这么设置:mysql使用的SYSTEM时区即EST时区,EST时区要比北京时间(东八区)慢13个小时)
直接通过命令修改
set global time_zone = timezone;
比如北京时间(GMT+0800)
set global time_zone = ‘+8:00’;
注:修改全局time_zone的值 ——set global time_zone=’+8:00’;
或 修改当前会话的time_zone 的值——set time_zone=’+8:00’;
#刷新权限使设置立即生效
flush privileges;
mysql> set global time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)
再次启动项目不会报错,完美解决
以上是理想状态,但是如果不成功,还有第二种方法
这里我使用的是mysql-8.0.20-winx64,版本过高
之前我的数据库配置是这样的:
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
这里的spring.datasource.url的----数据库名----后面需要做修改,修改后是这样的:
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.username =root
或者使用这个:
?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8