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/mysqldGive ownership to the mysql user:
sudo chown mysql:mysql /var/run/mysqldStart 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 rootReload 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';Step 4: Restart MySQL Normally
Shut down the background safe-mode process:
sudo killall mysqldStart the standard service:
sudo systemctl start mysqlTest 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