How Do I Reset MYSQL Root Password?
I'm having trouble accessing my MYSQL database. I think I need to change the root password. How do I do this?
2 Replies
You may see this error when attempting to connect:
MySQL - ERROR 1045 - Access denied
This is how you will know there is a permissions error.You will first need to stop MYSQL first. Then, you can reset your password. You will also want to check your MYSQL version before we begin. Then, you can adjust the version to the one you are using.
Run this command to check: mysql --version
To stop MYSQL
sudo systemctl stop mysql.service
Restart MYSQL
sudo dpkg-reconfigure mysql-server-5.5
Now, we can work on resetting the password. Follow the steps below.
Login command
mysql -u root
Manually reset password
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root'
or If using MYSQL 5.7
UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
Flush MYSQL privilege
FLUSH PRIVILEGES;
This StackOverflow forum post may help answer additional questions on how to reset your root password.
I hope this has been helpful!
Probably the easiest way to do it is through the command line.
For Ubuntu/Debian systems, you should be able to follow this with no difficulty.
Firstly, you want to stop MySQL if it is currently running.
sudo /etc/init.d/mysql stop
Next, we want to bring MySQL back up but with the --skip-grant-tables option.
sudo mysqld_safe --skip-grant-tables &
Once done, we should be able to connect to the database like so:
mysql -uroot
When you reach the mysql prompt, use the following series of queries to reset the root password:
USE MYSQL;
UPDATE USER SET AUTHENTICATION_STRING=PASSWORD("newpassword") WHERE USER='root';
FLUSH PRIVILEGES;
QUIT
Lastly, bring MySQL back down and then restart it normally:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start