在新电脑上安装了MySQL8.0,打开原来的一个项目去链接时,报以下错误java
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.mysql
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)spring
经排查,是由于JDBC驱动版本问题致使的,项目原来使用的MySQL版本是5.X,如今使用MySQL8.0,驱动须要相应的跟新,以下:sql
<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 表明东八区)
也可设置为:serverTimezone=UTC serverTimezone=Shanghai(会比中国时间早8个小时,若是在中国,能够选择Asia/Shanghai或者Asia/Hongkong)
(GMT%2B8
若是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=trueapp
SSL – Secure Sockets Layer(安全套接层)url