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

  1. 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.


  1. Stop the MySQL server:

  • AlmaLinux / Fedora:

service mysqld stop
  • Debian / Ubuntu:

service mysql stop

  1. 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.


  1. Log into MySQL:

mysql

  1. Reset the root password:

UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';

Replace 'new-password' with your desired root password.


  1. Apply changes and exit:

FLUSH PRIVILEGES;
exit;

  1. Stop MySQL safely:

mysqladmin -u root -p shutdown
  • Enter the new root password when prompted.


  1. Start MySQL normally:

  • AlmaLinux / Fedora:

service mysqld start
  • Debian / Ubuntu:

service mysql start

Your MySQL root password is now successfully reset.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution