To reset a forgotten MySQL root password on Ubuntu, stop the active service, start MySQL with the --skip-grant-tables option, log in without a password, update the root user password via an ALTER USER command, and restart the database server normally. [1, 2]
Step 1: Stop MySQL Service
Stop the running server:
sudo systemctl stop mysql
[1]
Step 2: Start MySQL Without Grant Tables
Create a run directory if it is missing:
sudo mkdir -p /var/run/mysqld
Give ownership to the mysql user:
sudo chown mysql:mysql /var/run/mysqld
Start MySQL safely while skipping permission checks:
sudo mysqld_safe --skip-grant-tables --skip-networking &
[1]
Step 3: Connect and Update Password
Open the MySQL shell as root without a password:
mysql -u root
Reload the grant tables inside the prompt:
FLUSH PRIVILEGES;
Change your root password (replace NewPassword123 with your strong password):
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword123';
Exit the shell:
EXIT;
[1, 2, 3]
Step 4: Restart MySQL Normally
Shut down the background safe-mode process:
sudo killall mysqld
Start the standard service:
sudo systemctl start mysql
Test your login with the new password:
mysql -u root -p
[1]
If you run into any errors during startup or if you are using an older version of MySQL, tell me the error message so I can help fix it