could not create connection to database server.] with root cause

场景

SpringBoot项目运行后提示:

could not create connection to database server.] with root cause_第1张图片

详细提示如下:

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.] with root cause

java.lang.NullPointerException: null
 at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3286) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1987) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1913) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:834) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.27.jar:na]
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_181]
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_181]
 at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_181]
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416) ~[mysql-connector-java-5.1.27.jar:na]
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347) ~[mysql-connector-java-5.1.27.jar:na]
 at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:115) ~[HikariCP-3.2.0.jar:na]
 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar:na]

解决

自己项目的mysql驱动的版本与本机安装的mysql的版本不对应导致,项目所用驱动版本过低。

打开cmd,输入mysql,可以查看本机安装的mysql的版本为8.0。

所以找到项目中的pom.xml

将mysql驱动版本修改为:

dependency>
      mysql
      mysql-connector-java
      8.0.11
  

could not create connection to database server.] with root cause_第2张图片

 

注意:

8.0.11版本的mysql驱动包的driverClass有所变化

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

could not create connection to database server.] with root cause_第3张图片

你可能感兴趣的:(数据库)