Kaumadie Kariyawasam
Kaumadie Kariyawasam

Reputation: 1456

java.sql.SQLException: Access denied for user 'root'@'localhost' - Can't create connection with mysql

I created a Spring Boot project and I want to connect with a database.

This is a .property file

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

In MySQL username=root which doesn't have a password.

mysql port=3306

I get the following error when running the project.

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]

How do I solve this error?

I'm working with the Ubuntu operating system.

Also, I use mysql -u root command in the terminal to connect mysql it gives same error. hence I used sudo mysql -u root I can sucessfully use mysql shell

Upvotes: 0

Views: 7021

Answers (2)

lee yiyang
lee yiyang

Reputation: 75

Maybe is the privileges' problem. Try below command at the MySQL server side.

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

Reference :

Upvotes: 0

Simon Martinelli
Simon Martinelli

Reputation: 36133

Change

spring.datasource.password=''

to

spring.datasource.password=

Upvotes: 2

Related Questions