Reset MySQL Root Password on Debian/Ubuntu

  • Last Created On Dec 05, 2024
  • 10
0 0

If you have forgotten your MySQL root password, you can reset it by following the steps in this article. The process is quite simple and works on these operating systems:

  • Ubuntu 16.04
  • Ubuntu 18.04
  • Debian 8
  • Debian 9

Determining your database

The table below will tell you which database software is available in the OS repository by default.

|    Operating System    |  MySQL/MariaDB  |
| ---------------------- | --------------- |
| Ubuntu 16.04           | MariaDB         |
| Ubuntu 18.04           | MariaDB         |
| Debian 8               | MySQL           |
| Debian 9               | MySQL           |

If you are still unsure of the database software you have installed, perform the following command:

mysql -V

If you see mysql Ver xxx Distrib x.x.x-MariaDB, for example, you are running MariaDB. Otherwise, you are running MySQL.

If you have MySQL installed, please follow the guide below to reset your password. If you have MariaDB installed, scroll to the “Resetting your MariaDB password” section.

Resetting your MySQL password

First, we must stop the mysql service and grant password-less access:

service mysql stop
mysqld_safe --skip-grant-tables &

Once the mysql service stops, we can now connect to our database server by performing the following command:

mysql -u root mysql

Now, enter the following into the SQL terminal:

UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;

You can now enter the following into the SQL terminal:

exit

You may now restart the mysql service:

service mysql restart

Resetting your MariaDB password

Since MariaDB is (essentially) the same software, the commands are similar. Like the previous section, we need to stop the mariadb process and grant password-less access before continuing:

service mariadb stop
mysqld_safe --skip-grant-tables &

Now, we can connect to our database server:

mysql -u root mysql

Once you see the SQL terminal, enter the following:

UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;

Exit and restart MariaDB:

exit
service mariadb restart

Conclusion

Congratulations! You’ve successfully reset your MySQL/MariaDB server password.

Views: 10

Recent Articles

  • How to Install JAX with ROCm Acceleratio...
    95
  • Deploy a PyTorch Workspace on a Vultr Cl...
    71
  • Managing Backup Storage
    81
  • Automating FTP Backups in Windows Server
    71
  • Automating FTP Backups in Linux
    75

Popular Articles

  • Our General Terms & Conditions
    2629
  • Our Privacy Policy
    2474
  • Our Cookies Policy
    275
  • Our Terms of Use
    152
  • How to Install JAX with ROCm Acceleratio...
    95