連接MySQL 報錯"init datasource error, url: jdbc:mysql:/" 解決

原因

原來數據庫5.5升級到8.0後,數據庫連接配置沒有改造成的

解決辦法

1、驅動包要升級爲 mysql-connector-java-8.0.11.jar 或更新的版本
https://dev.mysql.com/downloads/file/?id=477058

Maven 依賴:

<dependencies>
	<dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>8.0.11</version>
	</dependency>
</dependencies>	

2、JDBC driver 由com.mysql.jdbc.Driver改爲com.mysql.cj.jdbc.Driver
3、url中加上“useSSL=false”。否則會出現以下錯誤:

“Establishing SSL connection withoutserver's identity verification is not recommended. According to MySQL 5.5.45+,5.6.26+ and 5.7.6+ requirements SSL connection must be established by defaultif explicit option isn't set. For compliance with existing applications notusing SSL the verifyServerCertificate property is set to 'false'. You needeither to explicitly disable SSL by setting useSSL=false, or set useSSL=trueand provide truststore for server certificate verification.”

4、url中加上“serverTimezone=GMT%2B8”(GMT%2B8代表東八區)否則會報關於時區的錯

一個完整配置的例子:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:/localhost:3306/mysql?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=yourpassword

參考

連接數據庫報錯init datasource error, url: jdbc:mysql:/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章