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

本文解决MySQL8.0版本中使用mybatis-generator-maven-plugin生成代码时出现的连接错误问题,包括更换驱动和解决时区错误的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.linuxidc.com/Linux/2018-11/155344.htm

 

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

[日期:2018-11-15]来源:Linux社区  作者:Linux[字体:大 中 小]

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

### 数据库连接问题分析 当遇到无法创建到数据库服务器的连接时,可能涉及多种原因。以下是针对不同情况的具体解决方案: #### 1. **SQL Server 连接问题** 如果当前尝试建立与 SQL Server连接失败,则可能是由于网络配置、驱动程序兼容性或身份验证设置不正确引起的[^1]。建议执行以下操作: - 验证目标 SQL Server 是否正在运行并接受远程连接。 - 检查防火墙规则是否允许客户端访问指定端口(默认为 1433)。 - 使用正确的用户名和密码进行登录测试。 ```sql -- 测试SQL Server连接状态 SELECT @@VERSION; GO ``` #### 2. **Oracle 客户端软件错误** 对于 `oraoledb.oracle` 和 `Error 7311` 错误消息表明未能成功获取来自 OLE DB 提供商模式行集的情况[^2]。可以采取如下措施来解决问题: - 确认已安装最新版 Oracle 客户端组件,并匹配操作系统架构(即 x86 或 x64)。 - 设置环境变量 TNS_ADMIN 到 tnsnames.ora 文件所在目录路径下。 ```cmd :: 设置TNS_ADMIN环境变量 set TNS_ADMIN=C:\oracle\product\instantclient_19_8\network\admin tnsping ORCL ``` #### 3. **MySQL InnoDB 锁定文件冲突** 关于 MySQL 中出现 “InnoDB: Unable to lock ./ibdata1, error: 11”,通常意味着另一个进程已经占用了该共享表空间文件[^3]。处理方式包括但不限于以下几个方面: - 停止所有其他实例的服务后再启动新的服务; - 修改 my.cnf/my.ini 配置文件中的 datadir 参数指向不同的存储位置; ```ini # 调整my.cnf配置示例 [mysqld] datadir=/var/lib/mysql_new/ innodb_data_home_dir= innodb_log_group_home_dir= ``` #### 4. **MySQL 版本过旧引发的功能缺失** 提到远端机器上的 MySQL 版本较低 (5.7.x),而某些特性仅支持更高版本(如8.0+)的情形[^4]。此时应考虑升级至较新稳定发行版以获得更好的性能表现以及新增功能的支持度。具体步骤参见官方文档指南完成迁移过程即可。 ```bash # Linux平台更新yum源命令样例 sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm sudo yum update mysql-server service mysqld restart mysql_upgrade -u root -p ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值