spring boot+mysql連線資料庫錯誤

在使用spring boot +Mysql組合的時候。遇到了連線資料庫錯誤。資訊:The server time zone value xxx

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 specifc time zone value if you want to utilize time zone support。

at sun。reflect。NativeConstructorAccessorImpl。newInstance0(Native Method)

at sun。reflect。NativeConstructorAccessorImpl。newInstance(NativeConstructorAccessorImpl。java:62)

at sun。reflect。DelegatingConstructorAccessorImpl。newInstance(DelegatingConstructorAccessorImpl。java:45)

at java。lang。reflect。Constructor。newInstance(Constructor。java:423)

檢視錯誤的資料庫連結:

jdbc:mysql://127。0。0。1:3306/springbootautocode?user=root&password=123456

錯誤原因分析:

這是因為mysql 伺服器時區問題導致的。

解決方案一:

直接在url後面新增:&serverTimezone=UTC

如果想或者新增:serverTimezone=GMT%2B8

因為中國的時區是在東八區。所以這裡設定時區

解決方案二:

修改mysql 伺服器的時區配置。

檢視當前mysql伺服器時區語句:

show variables like ‘%time_zone%’;

spring boot+mysql連線資料庫錯誤

修改time_zone的值為+8:00語句:

set global time_zone=‘+8:00’;

修改後:

spring boot+mysql連線資料庫錯誤

這裡不推薦第二種修改方式。

因為如果修改了伺服器時區的話,原來老資料的時間就不比時間少8個小時。這個時候會導致很多未知的問題出現。

所以強烈推薦第一種修改方式。方便又簡單。

下面看看凱哥的:

spring。datasource。url=jdbc:mysql://127。0。0。1:3306/springbootautocode?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8

修改完成後,重啟服務,就可以正常訪問了。

頂部