SQLException: Access denied for user ‘root’@’localhost’

Have you ever faced problem of Access denied for user or any login related issues after re-installing MySQL server. I faced this issue and when I googled it, I saw many others are also suffering from this problem frequently. In this post, I will suggest you a good try which will solve this problem in 95% of cases.

Reason of Access denied Error

Access denied error for user ‘root’ problem occurs mostly when you un-install and install a new version or even previous version also. The problem lies in in-complete cleanup done by mysql uninstaller wizard. So, solution is also very simple, cleanup remaining junk manually.

You exception will look like this:


Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
 at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
 at com.mysql.jdbc.ConnectionImpl.init(ConnectionImpl.java:718)
 at com.mysql.jdbc.JDBC4Connection.init(JDBC4Connection.java:46)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
 at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
 ... 5 more

Solution

Solution to access denied error is to remove old data folder from following location.

C:/Documents and Settings/All Users/Application Data/MySQL [refer here]

This directory contains temp files and old DB data including your last login credentials used in previous installation. So, to solve this issue:

  1. Uninstall MYSQL server
  2. Remove everything from above folder
  3. Install a fresh MySQL server

You should be good to login in this in instance of Server.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “SQLException: Access denied for user ‘root’@’localhost’”

  1. Apart from directly going for re-installation one can also check for these settings.

    1.Always check for error log file.
    2.Password for the user “root” has been entered correctly.
    3.Port Number (If multiple instances are running).
    4.Mark Sql files/folders in exception list of Anti-virus.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.