Resetting a lost MySQL root password

MySQL root password allows full access to the MySQL database and allows for all actions to be undertaken including creating new users, new databases, setting access rules and so on.

You might get the following error (Error 1045) while trying to key in the wrong password->
Enter password:
mysqldump: Got error: 1045: Access denied for user ‘root’@’localhost’ (using password: YES) when trying to connect

If you set a root password previously, but have forgotten it, you can set a new password using these steps->

1) Stop MySQL-
/etc/init.d/mysqld stop

2) Start MySQL in Safe Mode (This enables anyone to connect without a password and with all privileges. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting) –
mysqld_safe –skip-grant-tables &

3) Login without a Pwd-
mysql -u root

4) Reset the password by executing the following statement
mysql> UPDATE mysql.user SET Password=PASSWORD(“MyNewPwd”)¬†where User=’root’;
mysql> FLUSH PRIVILEGES;

5) Stop and Start MySQL Service-

/etc/init.d/mysqld stop

/etc/init.d/mysqld start

6) Login again using the new password and you should be good to go.

mysql -u root -p newpwd


Happy Learning!

Cheers!
Aman Kharbanda

Advertisements