前言

今天学习使用Spring配置Druid数据源的时候,连接数据库配到如下问题:

pringboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

谷歌搜索了一下,发现导致这个问题出现的原因很多,按照每个都进行了检查,发现仍然没有解决,最后,才发现是我配置文件有问题,下面详细解析一下。

先看一下有问题的配置文件吧:

url=jdbc:mysql://localhost:3306/worlduseSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
initPoolSize=5
maxPoolSize=10

这里用户名,密码,URL都是对的,并且都没有空格,并且在Navicat 下来连接Mysql 也是可以的,所以问题不是这些。真正错误的原因如下:

spring.datasource.data-username= # Username of the database to execute DML scripts (if different).
看注释这个是用来单独执行DML脚本的如果设置了的话
正确的应该使用spring.datasource.username= # Login username of the database.

修改后的正确配置如下:

spring.datasource.url=jdbc:mysql://localhost:3306/world?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.initPoolSize=5
spring.datasource.maxPoolSize=10