Dawlatzai Ghousi
2 min readMay 31, 2021

--

How to Reset the MySQL Root Password in Ubuntu Server:

As a developer, I have faced this problem many times, to solve the problem I remove MySQL software from the webserver and then install it again (which is not fair).

One day I forgot the password of my main SQL account and stacked it what to do with the problem? And how to solve the problem? Because we have not any database backup and can’t uninstall the existing MySQL software.

finally, we found a good and tricky technique for resetting the SQL password user account and it has several phases:

  1. Stop MySQL service using this command:
    > sudo service mysql stop
  2. Create a mysqld directory for attempting to reset your root password inside /var/run/ destination.
    > sudo mkdir -p /var/run/mysqld
  3. Run the command for changing user group ownership.
    > sudo chown mysql:mysql /var/run/mysqld
  4. Stop mysql service again.
    > sudo service mysql stop
  5. Run mysqld_safe to login sql to skip the grant table.
    > sudo mysqld_safe — skip-grant-tables & mysql -uroot
  6. Then change the user password using this command.
    > update mysql.user set authentication_string=password(‘your_password’) where user=’your_username’;
  7. Then reloads the grant tables in the mysql database enabling the changes to take effect without reloading or restarting mysql service using flush privileges command.
    > flush privileges;
  8. Finally, quit MySQL using this command.
    quit
  9. After that, we kill the MySQL process using this command.
    > sudo killall mysql
  10. In the end, we restart the MySQL service.
    > sudo service mysql start
  11. To login in MySQL using new password, we use this command>
    > mysql -u root -pyour_password

Tested in MySQL 5.7 running in Ubuntu 18.04

--

--

Dawlatzai Ghousi

Sotware Developer at The Independent Administrative Reform and Civil Service Commission(IARCSC)