Caused by: com.mysql.cj.exceptions.CJException: Access denied for user ‘root‘@‘localhost‘

2024-07-14 1946阅读
Python
Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'root'@'localhost' (using password: YES)

报错:springboot项目启动时一直报连接数据库有误

报下面三个错误:

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user ‘root‘@‘localhost‘

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user ‘root‘@‘localhost‘

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'root'@'localhost' (using password: YES) 

被这困扰了很久,所以来记录下。网上有很多贴子,有相关的解决问题,但是对我的没有效果。大家可以先尝试按照别的帖子解决下,下面贴一下别的博主链接连接数据库报错Access denied for user 'root'@'localhost' (using password:YES)

Python
https://www.cnblogs.com/xiaohaodeboke/p/11771125.html

分析

我的数据库Test connection 是可以连上的

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user ‘root‘@‘localhost‘

是什么原因 

踩了很多次坑还是配置问题,每次重新导入别人的后端项目,时好时坏。

Springboot的配置信息在application-dev.yml文件中

看到网上说是配置问题到底怎么解决,对比了正常的和报错的application-dev.yml文件

Python
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: root
password: root

在自己看来是数据库配置的url中?问号后面的信息填写不一致。

上面是正常的配置

下面是一启动就报错的配置

Python
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=Asia/Shanghai&autoReconnect=true&rewriteBatchedStatements=true
username: root
password: root

大概是我之前自己配置是按照别的项目来的。

解决

修改application-dev.yml文件中

Python
--- # 数据源配置
spring:
  datasource:

下面的url: jdbc:mysql://localhost:3306/demo?后面的信息

以上是我个人的拙见,可能不适合别人,希望对大家有帮助。

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]