How to reset a forgotten MySQL root password?

mysql root
mysql root

To be able to modify the root password of MySQL, you must be able to connect to it. However, if you have forgotten your current MySQL database password or if for some unknown reason it no longer works, then you find yourself in a dramatic situation.

Well, don't panic, in this article I'll show you how to fix this little problem. so here's how to change a MySQL password.

Before starting, you must be logged in to your Linux session as root. Then, you should stop the MySQL server with the following command:

# / etc / init.d / mysql stop

Then we will restart MySQL without password with this command:

mysqld_safe --skip-grant-tables &

We will then connect to MySQL with the command line client

mysql -u root

Now enter the following commands to update the root password (obviously replacing newpassword with yours):

mysql> use mysql; mysql> update user set password = PASSWORD ("newpassword") where user = "root"; mysql> flush privileges; mysql> quit

Now all you have to do is stop and restart MySQL in "normal" mode. To do this, enter:

/etc/init.d/mysql stop /etc/init.d/mysql start

Then connect with the MySQL client

mysql -u root -p

Enter your new password and you're done. 😉