MySQL 8.0版本連接報錯:Could not create connection to database server.


 准備搭建一個Spring Boot 組合mybatis的項目,數據庫采用的是MySQL 8.0.11按照以往的配置,使用插件mybatis-generator-maven-plugin生成代碼時,一直報錯Could not create connection to database server.如下:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building songci-serv 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ songci-serv ---
[INFO] Connecting to the Database
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.841 s
[INFO] Finished at: 2018-08-22T21:32:27+08:00
[INFO] Final Memory: 13M/46M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project songci-serv: Could not create connection to database server. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

 


      經排查是,MySQL8.0版本需要更換驅動為“com.mysql.cj.jdbc.Driver”,之前的“com.mysql.jdbc.Driver”已經不能在MySQL 8.0版本使用了,官方文檔鏈接:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-api-changes.html;

描述如下:The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated.

另外mysql-connector-java也推薦更新到8.0的版本(https://dev.mysql.com/downloads/connector/j/)。

綜上修改以下兩點:

1.更新mysql-connector-java版本:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>

2.更換驅動:

 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test?
useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false" userId="root" password="root"/>

 


配置好以上兩點,運行mybatis-generator-maven-plugin,接着報錯如下:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project songci-serv: 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. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

 


報時區錯誤,解決方法執行命令給MySQL服務器設置時區為東八區,這個是com.mysql.cj.jdbc.Driver需要指定的:

mysql> set global time_zone='+8:00';
Query OK, 0 rows affected
Navicat中命令列介面執行即可。或者在數據庫連接配置中加上serverTimezone=GMT%2B8(代表東八區),如下:

connectionURL="jdbc:mysql://127.0.0.1:3306/songci?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM