一、背景信息
MySQL驱动版本:5.1.49
Springboot项目application.yml中关于数据源的配置如下:
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_boot?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 12345
driver-class-name: com.mysql.jdbc.Driver
二、几类问题
2.1 errorCode 0, state 08S01
这个错发生在【创建数据库连接】这个阶段,一般是配置文件中写错了,比如由下面这个报错信息可以看出,url 属性的值中“localhost” 被写成了 “locahost”。那自然是连接不上数据库了...
ERROR 20324:... create connection SQLException,
url: jdbc:mysql://locahost:3306/spring_boot?useSSL=true, errorCode 0, state 08S01
2.2 Loading class `com.mysql.jdbc.Driver'. This is deprecated.
完整提示信息如下:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
看到这个提示说明项目中使用的MySQL驱动版本较高,原来的配置方式已不再适用,需要将原来配置文件中 driver-class-name 的值改成 “com.mysql.cj.jdbc.Driver”,如下所示:
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_boot?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 12345
driver-class-name: com.mysql.cj.jdbc.Driver
或者可以选择版本更低的MySQL驱动版本