Skip to content

Access denied for user ‘root ‘@’localhost’ ” when connecting to mysql

I try to connect to a mysql database in a spring project and error occurred.

 create connection error, url: jdbc:mysql://localhost:3306/IwebManager?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user 'root  '@'localhost' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(

The configuration in is

spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/IwebManager?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true
spring.datasource.username = root  
spring.datasource.password = 1234

In pom.xml is


I’m sure the password is correct. And in cmd I tried following command and it work.

mysql -u -root -p
mysql>use IwebManager;

So what’s the possible reason? Thx.



Remove the spaces on the sides of the = signs in your And also make sure you have no spaces after the values (notice in your question title it’s trying to log in as root with a space after the t).

You do have spaces after root in the copy/pasted data in the question (couldn’t possibly know in your real file)

Look at a screenshot of your question with the text selected (notice after root):

Copy/paste spaces image

User contributions licensed under: CC BY-SA
1 People found this is helpful