How to Recover the MySQL 8.0 root Password on Ubuntu

How to Recover the MySQL 8.0 root Password on Ubuntu

If you forget the MySQL 8.0 root password on Ubuntu, you can reset it by starting MySQL with skip-grant-tables. Below is the safest method with commands you can copy and run directly.

Step 1: Stop the MySQL Service

sudo systemctl stop mysql

Step 2: Start MySQL with Skip-Grant-Tables (No Password Required)

Create a temporary directory (MySQL 8.0 requires a writable directory):

sudo mkdir /var/run/mysqld sudo chown mysql:mysql /var/run/mysqld

Then start MySQL while skipping the grant tables:

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

Explanation:

* --skip-grant-tables skips password verification

* --skip-networking disables network access for added security

* mysqld_safe keeps MySQL running in the background

Wait a few seconds until you see “ready for connections”.

Step 3: Log Into MySQL Without a Password

mysql -u root

Step 4: Refresh Privileges and Reset the root Password

Run the following inside the MySQL shell:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

For example:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass123!';

If you prefer MySQL’s default caching_sha2_password (default in MySQL 8):

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass123!';

Finish with:

FLUSH PRIVILEGES;

Then exit:

exit;

Step 5: Kill mysqld_safe and Restore Normal Service

Find the mysqld_safe PID:

ps aux |
grep mysqld

Terminate it:

sudo kill -9 PID

Then start MySQL normally:

sudo systemctl start mysql

Step 6: Test Login

mysql -u root -p

Enter the new password you just set.

🔧 Common Issues

❗ 1. Error: Access denied

This means privileges weren’t refreshed. Run:

FLUSH PRIVILEGES;

❗ 2. mysqld_safe Cannot Be Started

Some Ubuntu systemd installations don’t include mysqld_safe. You can start MySQL manually:

sudo mysqld --skip-grant-tables --skip-networking --user=mysql &


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *