美文网首页
MySQL8的jdbc配置错误问题(MySQL8数据库变动)

MySQL8的jdbc配置错误问题(MySQL8数据库变动)

作者: 单名一个冲 | 来源:发表于2019-04-09 22:04 被阅读0次

    小编在一次使用mybatis-generator生成映射时,由于从mysql5切换到了mysql8.0,报了如下错误:

    [INFO] Scanning for projects...
    [INFO] 
    [INFO] ---------------------< com.demo:mybatis-generator >---------------------
    [INFO] Building mybatis-generator 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ mybatis-generator ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.497 s
    [INFO] Finished at: 2019-04-09T21:47:48+08:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project mybatis-generator: 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
    
    

    错误分析:

    • 报错为:Could not create connection to database server.
      这应该是我的连接有问题,但是我当时并找不出错误;
      经过一番尝试发现我的驱动问题(Mysql8.0+驱动名为com.mysql.cj.jdbc.Driver),但是当我修改后,发现mysql-connector-java的jar包也存在问题,version由5.1.35改为8.0.15;修改再次尝试发现开始报明显的timezone问题,这时猜测到是jdbc连接参数缺少serverTimezone,最后完美解决。

    1. 修改后的maven引入mysql连接包的版本:

    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.7</version>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
            </dependency>
        </dependencies>
        <!-- <configuration>
            配置文件的路径
            <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
            <overwrite>true</overwrite>
        </configuration> -->
    </plugin>
    

    2. MySQL8.0+的数据库连接驱动从com.mysql.jdbc.Driver更换到了com.mysql.cj.jdbc.Driver

    3. MySQL8.0+的数据库连接需要指定serverTimezone=UTC;
    如果直接在jdbc连接URL中添加serverTimezone=UTC会报错误
    ,如下:

    [ERROR] XML Parser Error on line 27: 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
    

    只需要修改为"&amp;serverTimezone=UTC"即可,如下例子:**

    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
        connectionURL="jdbc:mysql://127.0.0.1:3306/springdata?characterEncoding=utf-8&amp;serverTimezone=UTC"
        userId="root" password="password">
    </jdbcConnection>
    

    相关文章

      网友评论

          本文标题:MySQL8的jdbc配置错误问题(MySQL8数据库变动)

          本文链接:https://www.haomeiwen.com/subject/tbdwiqtx.html