How to reset the MySQL root password
This is a usual situation where you have added a password to the root user during the MySQL installation, but later on, you forgot it and can’t login. Don’t worry, we have been to the same situation and will help you to change the password in a few steps.
- The first thing to do is to stop the MySQL server:
sudo /etc/init.d/mysql stop
- Start the server with additional flags:
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
--skip-grant-tables flag will start the server without the grant tables in MySQL system schema. That means no privileges and unrestricted access to all databases, so it is best to also disable remote connections by using the
- Use the MySQL client to connect back to the server:
- Now, let’s reload the grant tables:
- Update the password for
update user set authentication_string=password('new_pass') where user='root';
update user set plugin="mysql_native_password" where User='root';
mysql_native_password is the default authentication plugin used. After executing the above query just restart the server and you can loggin with new password.
Change the root password in MariaDB 10.x
In the latest MariaDB versions the above steps are simplified. You can skip steps 1 and 2 from above. Start from the 3 step and use
sudo when login to MySQL client. The 4 and 5 steps are the same. To verify if the new password works: