用spring连接mysql数据库时出现报错:
详细报错:
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 specific time zone value if you want to utilize time zone support. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
报错原因:这个异常是由于MySQL服务器的时区设置问题导致的。错误提示“The server time zone value 'йʱ' is unrecognized or represents more than one time zone.
”表明服务器的时区值无法被识别或者代表多个时区。
以下是几种解决方案:
-
指定服务器时区:您可以在JDBC连接字符串中通过
serverTimezone
参数指定一个具体的时区。例如,如果您想要使用UTC时区,可以这样设置:
jdbc:mysql://localhost:3306/yourDatabase?serverTimezone=UTC
2.避免使用模糊时区:如果服务器时区设置为SYSTEM
,并且system_time_zone
变量的值是CST
,这可能会导致问题,因为CST
可能代表多个时区。在这种情况下,建议将MySQL服务器的time_zone
设置为非SYSTEM
值,比如+08:00
或者使用具体的时区名称,如Asia/Shanghai
:
jdbc:mysql://localhost:3306/yourDatabase?serverTimezone=Asia/Shanghai
3. 更新JDBC驱动:如果您使用的是较旧版本的MySQL JDBC驱动,可能需要更新到支持新密码规则的版本,例如8.0.11或更高版本。