These are the terminal commands to reset your MySQL password for your “root” user.
This is useful if you forgot your MySQL password on your local LAMP server. This is a fix for errors like these:
ERROR 1045: Access denied for user: 'root@localhost' (Using
password: NO)
ERROR 1045: Access denied for user: 'root@localhost' (Using
password: YES)
To reset your MySQL password, follow these steps and enter the commands in a terminal:
- Stop MySQL using this command:
sudo /etc/init.d/mysql stop
- This command will let you connect to MySQL without entering a password:
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
- Next, the terminal may “freeze,” so simply open a new terminal window (CTRL + ALT + ‘T’).
In the new terminal window, enter the following commands:
- Connect to MySQL without a password:
mysql -u root
- Flush privileges:
FLUSH PRIVILEGES;
- Update your MySQL password to a new password with these 2 commands. Be sure to change ‘new_password’ on both commands to your own custom password.
SET PASSWORD FOR root@'localhost' = PASSWORD('new_password'); UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
The response message should say that one or more rows were affected. This means your MySQL root password was successfully updated.
- Flush privileges again:
FLUSH PRIVILEGES;
- Exit from MySQL:
exit
- Stop and start MySQL one last time:
sudo /etc/init.d/mysql stop; sudo /etc/init.d/mysql start;
That’s it. Now, you should be able to connect to MySQL normally, using your new password.
Questions and Comments are Welcome