How to reset the MySQL root password

Reset a forgotten MySQL or MariaDB root password using safe mode or the init-file method.

Reset the MySQL or MariaDB root password when the current password is forgotten or unknown by restarting the server in safe mode.

Prerequisites

  • Root (sudo) access to the Linux server
  • MySQL or MariaDB server installed

Step-by-Step: Reset the MySQL Root Password

1. Stop the MySQL Service

sudo systemctl stop mysql

2. Start MySQL in Safe Mode (Skip Grant Tables)

sudo mysqld_safe --skip-grant-tables --skip-networking &

The --skip-grant-tables flag disables authentication. The --skip-networking flag prevents remote connections during the password reset for security.

3. Connect to MySQL Without a Password

mysql -u root

4. Reset the Root Password

For MySQL 5.7+:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '{YOUR_NEW_PASSWORD}';

For MySQL 5.6 and MariaDB:

FLUSH PRIVILEGES;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{YOUR_NEW_PASSWORD}');

5. Restart MySQL Normally

sudo killall mysqld
sudo systemctl start mysql

6. Test the New Password

mysql -u root -p

Common Issues

"ERROR 1045 Access denied" after reset— The ALTER USER syntax differs between MySQL versions. Use the correct syntax for your version. See MySQL: ERROR 1045 Access denied.