MySQL error – Access denied for user ‘root’@’localhost’

If you get an error message like this:

Access denied for user: 'username@localhost' (Using password: YES)

then the problem is that user can not access the database. This can have several causes:

First of all, you use the correct name and password of your MySQL user for connecting to your database.

Secondly, make sure the your database user has the password and it is not blank. If your database user does not have any password then create a password for your database user by following the steps below.

Start the MySQL server instance or daemon with the --skip-grant-tables option (security setting).

Open your MS-DOS command prompt using “cmd” inside the Run window. Inside it navigate to your MySQL bin folder, such as C:\MySQL\bin using the cd command.

Execute the following command in the command prompt:

> mysqld --skip-grant-tables

Execute the following statements in the command prompt to set the password for root user

> mysql -u root mysql
$mysql> UPDATE user SET Password=PASSWORD('your new password') where USER='root';

Execute below command to flush the changes

mysql> FLUSH PRIVILEGES;

If you face the unknown field Password error above then use the below command:

update user set authentication_string=password('my_password') where user='root';

Finally, restart the instance/daemon without the --skip-grant-tables option.

> /etc/init.d/mysql restart

You should now be able to connect with your new password.

> mysql -u root -p
Enter password: your new password

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *