To reset the MySQL root password on Ubuntu 24.04, follow these steps. This works for MySQL 8.x as installed via the default Ubuntu repositories.
1) Stop MySQL service
-
- sudo systemctl stop mysql
2) Create the mysqld directory
We need to create the mysql directory because systemctl removes the mysqld directry and it’s contents when MySQL is stopped. When we start MySQL in safe mode the mysql directory is not automatically created as it would be if MySQL was started using systemctl.
-
- sudo mkdir -p /var/run/mysqld
- sudo chown mysql:mysql /var/run/mysqld
3) Start MySQL in safe mode (skip authentication)
This starts MySQL without asking for a password and prevents remote access during this mode.
-
sudo mysqld_safe --skip-grant-tables --skip-networking &
3) Log in to MySQL without a password
-
- mysql -u root
4) Change the root password
Inside the MySQL prompt, run:
-
- FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
- FLUSH PRIVILEGES;
Replace ‘newpassword’ with your desired root password.
5) Exit MySQL
-
- EXIT;
6) Stop the safe mode MySQL
-
- sudo killall mysqld
(If it doesn’t stop, use sudo pkill -f mysqld_safe)
7) Start MySQL normally
-
- sudo systemctl start mysql
8) Test the new password
-
- mysql -u root -p
- exit;
9) Reboot and check MySQL status
-
- sudo reboot
- log in
- sudo systemctl status mysql
Conclusion
That’s it, your MySQL root password is now reset and everything should be back to normal. This method ensures security is preserved while giving you access again. Just be sure to store your new credentials somewhere safe.