阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL 8.0版本连接报错:Could not create connection to database server.

126次阅读
没有评论

共计 3031 个字符,预计需要花费 8 分钟才能阅读完成。

准备搭建一个 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″

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3031字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中