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

You may receive the “Access denied for user ‘root’@’localhost'” message while accessing mysql from the command prompt.

The error message states that the Mysql password for user ‘root’ is incorrect and need to reset the password using the skip-grant-tables method.

ERROR 1045 (28000): Access denied for user 'root'@'localhost' 
(using password: NO)

How to reset root Mysql password in Linux?

First stop the mysql service

# /etc/init.d/mysql stop

Make sure all the mysql processes are stopped by executing the killall command

# killall -9 mysqld

Next, start mysql is safe mode i.e. the privileges will be skipped while connecting to the mysql server

# /usr/bin/mysqld_safe --skip-grant-tables &

now, connect to your Mysql prompt using the ‘mysql’ command

# mysql
 Welcome to the MySQL monitor. Commands end with ; or \g.
 Your MySQL connection id is 23056
 mysql>

Goto the ‘mysql’ database

mysql> use mysql;

Now set a password for user ‘root’

mysql> update user set password=PASSWORD("passhere") where user='root';

OR you can also set a blank password for user ‘root’

mysql> update user set password=PASSWORD("") where user='root';

Once done, reload privileges and quit

mysql> flush privileges;
mysql> quit

Now, restart the mysql service

# /etc/init.d/mysql restart

and you should be able to connect to your mysql server:

# mysql
OR
# mysql -uroot -p
This entry was posted on Sunday, March 14th, 2010 and is filed under Mysql & PostGres SQL. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

2 Responses to “Mysql: Access denied for user ‘root’@’localhost’”

  1. shawn

    Thank You soo much!!!!! I have been struggling with this blasted problem for days and now I can finally enter mysql! Such a beautiful and extremely well written tutorial on how to solve this problem!! Whoever you are, THANK YOU!:-)

  2. admin

    Welcome 🙂