【问题标题】:java.sql.SQLException: Access denied for user 'root'@'localhost' - Can't create connection with mysqljava.sql.SQLException:用户'root'@'localhost'的访问被拒绝-无法创建与mysql的连接
【发布时间】:2021-04-19 09:39:42
【问题描述】:

我创建了一个 Spring Boot 项目,我想连接一个数据库。

这是一个 .property 文件

spring.jpa.open-in-view=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/tracking
spring.datasource.username=root
spring.datasource.password=
server.port=8084
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

在没有密码的 MySQL username=root 中。

mysql 端口=3306

运行项目时出现以下错误。

java.sql.SQLException: Access denied for user 'root'@'localhost'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.22.jar:8.0.22]
SQLError.java:129
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.22.jar:8.0.22]
SQLError.java:97
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.22.jar:8.0.22]
SQLExceptionsMapping.java:122
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.22.jar:8.0.22]

我该如何解决这个错误?

我正在使用 Ubuntu 操作系统。

另外,我在终端中使用mysql -u root 命令连接mysql,它给出了同样的错误。 因此我使用了sudo mysql -u root 我可以成功使用mysql shell

【问题讨论】:

标签: java mysql spring spring-boot ubuntu


【解决方案1】:

改变

spring.datasource.password=''

spring.datasource.password=

【讨论】:

  • 我已经更改并再次尝试。但它给出了同样的错误
  • 您如何授予用户访问跟踪数据库的权限?
【解决方案2】:

也许是权限的问题。在 MySQL 服务器端尝试以下命令。

->GRANT ALL PRIVILEGES ON tracking.* TO 'root'@'localhost';
->FLUSH PRIVILEGES;

参考:

【讨论】:

  • 谢谢你的回答。我很早就做了。我可以暂时只为“跟踪”数据库解决这个问题
猜你喜欢
  • 2020-01-07
  • 2020-09-12
  • 1970-01-01
  • 2022-11-19
  • 2013-08-22
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 2014-09-10
相关资源
最近更新 更多