Resetting the MySQL Root Password
This guide explains how to reset the MySQL root password, for example, if you have forgotten it.
Note: You must have root access on the server to perform these steps.
Steps to Reset the MySQL Root Password
-
Log in via SSH to your server.
Note: Run the following commands as the root user. You can either log in directly as root (not recommended for security reasons) or use su / sudo.
-
Stop the MySQL server:
-
AlmaLinux / Fedora:
service mysqld stop
-
Debian / Ubuntu:
service mysql stop
-
Restart MySQL with grant tables disabled:
mysqld_safe --skip-grant-tables &
Important: The & runs the command in the background, allowing you to continue typing commands.
Running MySQL this way is highly insecure—only do this briefly to reset the password.
-
Log into MySQL:
mysql
-
Reset the root password:
UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';
Replace 'new-password' with your desired root password.
-
Apply changes and exit:
FLUSH PRIVILEGES;
exit;
-
Stop MySQL safely:
mysqladmin -u root -p shutdown
-
Enter the new root password when prompted.
-
Start MySQL normally:
-
AlmaLinux / Fedora:
service mysqld start
-
Debian / Ubuntu:
service mysql start
Your MySQL root password is now successfully reset.