How to reset MySQL Root Password in Linux


First, Let’s stop the mysql service, In Red Hat’s family (Red Hat Enterprise, Fedora, CentOS) the Mysql Daemon is called ‘mysqld‘, but in Debianlike Ubuntu based distributions and OpenSuse its called ‘mysql‘. I’ll be using the debian based naming
sudo service mysql stop
sudo /etc/init.d/mysql stop
Now, we’ll start the server in safe mode and skip grant tables so that it wont ask for the password we had lost when we try to login
sudo mysqld_safe –skip-grant-tables
you should see something like this
111026 11:05:46 mysqld_safe Logging to syslog.
111026 11:05:46 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Now open a new terminal window and run the mysql console using the main mysql database named ‘mysql‘ using the root user
mysql –user=root mysql
Then we’ll reset the password from the mysql’s users table, flush privieges, then exit the mysql console
UPDATE user SET password=PASSWORD('new-password') WHERE user='root';
flush privileges;
Start again the mysql daemon
sudo service mysql stop
sudo service mysql start
That’s All :)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.