码迷,mamicode.com
首页 > 数据库 > 详细

MySQL8.0数据库连接驱动问题

时间:2019-03-05 21:38:09      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:runtime   ESS   auth   att   com   hand   配置   6.2   mysqli   

在新电脑上安装了MySQL8.0,打开原来的一个项目去连接时,报如下错误

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)

 

经排查,是因为JDBC驱动版本问题导致的,项目原来使用的MySQL版本是5.X,现在使用MySQL8.0,驱动需要相应的跟新,如下:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--<scope>runtime</scope>-->
            <version>8.0.11</version>
</dependency>

另外,application.properties文件中的数据库配置项也需要相应变化:

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

url 需要添加时区设置:

&serverTimezone=GMT%2B8   (GMT%2B8代表东八区)
也可设置为:serverTimezone=UTC serverTimezone=Shanghai(会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong)

如果mysql-connector-java用的6.0以上的

如果MySQL-connector-java用的6.0以上的,则driver要使用:

com.mysql.cj.jdbc.Driver

 

如果未明确设置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接。
为了符合当前不使用SSL连接的应用程序,verifyServerCertificate属性设置为’false’。
如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。
如果你需要用SSL连接,就要为服务器证书验证提供信任库,并设置useSSL=true

SSL – Secure Sockets Layer(安全套接层)

 

 

 

 

 

MySQL8.0数据库连接驱动问题

标签:runtime   ESS   auth   att   com   hand   配置   6.2   mysqli   

原文地址:https://www.cnblogs.com/xiaoyu666/p/10479636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!