How to reset MySQL root password?

If you are using Linux and ever lose your MySQL root password or never set a root password and are now unable to access it, so don’t worry, there is an easy way to reset the MySQL root password. On all the modern Linux distributions you can recover the MySQL root password in just a few easy steps.

How to Reset MySQL Root Password

1. Stop all the MySQL Processes

On Ubuntu and Debian distributions.

service mysql stop

On CentOS, Fedora, and RHEL distributions.

service mysqld stop

2. Start MySQL Server with Safe Mode

mysqld_safe --skip-grant-tables &

3. Connect to the MySQL Server

mysql -u root

4. Setup a New MySQL Root Password

use mysql;
update user set password=PASSWORD("YourNewPassword") where User='root';
flush privileges;
exit

5. Stop MySQL Server and Start MySQL

On Ubuntu and Debian distributions.

service mysql stop
service mysql start

On CentOS, Fedora, and RHEL distributions.

service mysqld stop
service mysqld start

Now you can test your new MySQL root password via logging in to it normally.

mysql -u root -p

After entering the above command, you will be prompted for a new MySQL root password. You can verify whether it is working or not in the shell.

This way you can reset the MySQL root password and make it accessible again.

Category: .

Leave a Reply to Pedro Cancel reply

Your email address will not be published. Required fields are marked *

Responses

  1. Pedro Avatar
    Pedro

    Hi Atul, I have a problem because mine setup says “Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’”.

    1. AtulHost Avatar

      This error occurs due to multiple installations of mysql.

      Try a service restart or server reboot once. If you are still facing this issue even after restarting mysql services then run the following commands:

      ps -A|grep mysql

      Kill the process by using:

      sudo pkill mysql

      and then run command:

      ps -A|grep mysqld

      Also Kill this process by running:

      sudo pkill mysqld

      Now you are fully set just run the following commands:

      service mysql restart
      mysql -u root -p

      Now have very well working mysql again. Have a backup of your all database because nothing is more important than your precious data.

  2. Melker Avatar
    Melker

    Hi! Very good help, the only problem I have is that I don’t get MySQL to start. This is the error I get: Job for mariadb.service failed because a timeout was exceeded.

    See “systemctl status mariadb.service” and “journalctl -xe” for details. (And on top of that, my MySQL is named MariaDB for some reason?)

    1. AtulHost Avatar

      Hi Melker, first of all, do not get confused with MySQL named as MariaDB it is a child of the same branch that follows the new rule (maybe you have installed or upgraded to MariaDB from MySQL).

      About the error, it seems to be an error with either permission or any app is creating a problem for MySQL to start. I will suggest to read the error log and find the culprit where it log will show something like “DENIED”. Then try to troubleshoot that error.

      TIP: If the error is about APPARMOR then quick fix is ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ and reboot.