简体   繁体   English

如何使用Spring Data JPA和MySQL在Spring Boot中修复此错误

[英]How to fix this error in Spring Boot using Spring Data JPA and MySQL

I'm try to connect to a MySQL database using Spring Data JPA (in a Spring Boot application) and always the same error: 我尝试使用Spring Data JPA(在Spring Boot应用程序中)连接到MySQL数据库,并且始终出现相同的错误:

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

My application.properties looks like: 我的application.properties看起来像:

spring.datasource.url=jdbc:mysql://localhost:3306/demographics
spring.datasource.data-username=root
spring.datasource.data-password=root
spring.jpa.hibernate.ddl-auto=update

I know the url , username and password are good. 我知道urlusernamepassword都不错。

The problem is with creating connection with the database itself due to authentication. 问题是由于身份验证而与数据库本身创建了连接。 Execute this with command line or some tool like mysql workbench. 使用命令行或某些工具(例如mysql workbench)执行此操作。

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%root%' WITH GRANT OPTION;

The Spring documentation for "Common Application Properties" states that spring.datasource.data-username is the user to execute DML scripts (if different) . Spring的“公共应用程序属性” 文档指出spring.datasource.data-username执行DML脚本的用户(如果不同) I think you are looking for: 我认为您正在寻找:

spring.datasource.username = root
spring.datasource.password = root

Notice you don't need the data- part of the key. 注意,您不需要密钥的data-部分。

Keep in mind that if you need different credentials for both, you must specify them accordingly, unless they are the same. 请记住,如果两者都需要不同的凭据,则必须相应地指定它们,除非它们相同。 Also, just make sure the user have the correct/required privileges to access the schema you are using. 另外,只需确保用户具有正确/所需的特权即可访问您使用的架构。

看来您在数据库中的帐户不需要密码,所以只需在您的媒体资源中输入空密码

spring.datasource.data-password=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM