Home > Net >  Access denied for user 'root '@'localhost' " when connecting to mysql
Access denied for user 'root '@'localhost' " when connecting to mysql

Time:03-23

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(SQLError.java:127)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)

The configuration in application.properties 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

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.11</version>
</dependency>

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.

CodePudding user response:

Remove the spaces on the sides of the = signs in your application.properties. 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

  • Related