天天看點

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

制作了一個Spring boot 整合 mybatis連接配接mysql的小demo,從資料庫讀取資料再傳回到前端,實作最基本的查詢功能

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

但是遇到啟動的時候遇到報錯

Could not create connection to database server. Attempted reconnect 3 times.Giving up

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

**1.**檢查url沒有發現問題

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

**2.**在cmd也能正常連接配接資料庫

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

在我百思不得其解地時候我注意到下方報錯原因有一行奇怪的亂碼:

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.

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

果斷百度這一行錯誤發現,SpringBoot2.1在你沒有指定MySQL驅動版本的情況下它自動依賴的驅動是8.0.12很高的版本,這是由于資料庫和系統時區差異所造成的,在jdbc連接配接的url後面加上serverTimezone=GMT即可解決問題,如果需要使用gmt+8時區,需要寫成GMT%2B8,否則會被解析為空。再一個解決辦法就是使用低版本的MySQL jdbc驅動,5.1.28不會存在時區的問題。

加上後:

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

成功通路

Spring boot+MyBatis連接配接Mysql時遇到Could not create connection to database server的問題

百度原文在此描述解決MySQL報錯The server time zone value…