Reader Stacks

How to Reset the MySQL 8 Root Password on Ubuntu 20.04

MySQL 8's stricter authentication plugin requirements mean resetting the root password takes a few more steps than older MySQL versions — safe mode, then an explicit ALTER USER.

How to Reset the MySQL 8 Root Password on Ubuntu 20.04

Resetting a lost or forgotten MySQL root password on Ubuntu requires starting the server in a special safe mode that skips authentication checks — MySQL 8's stricter default authentication requirements mean a couple of extra steps compared to how this worked on older MySQL versions.

Stopping the MySQL service

sudo systemctl stop mysql

Starting MySQL in safe mode, skipping grant tables

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

--skip-grant-tables starts MySQL without enforcing any authentication at all — genuinely necessary to get in without the password you're trying to reset, but this is also why --skip-networking matters alongside it, temporarily blocking any remote connections while the server is in this wide-open state.

Connecting without a password

mysql -u root

Selecting the mysql system database and flushing privileges first

FLUSH PRIVILEGES;

Running FLUSH PRIVILEGES before the actual password change is a specific requirement in MySQL 8's safe-mode reset process — skipping it is a common reason the subsequent ALTER USER statement fails in this specific scenario.

Setting the new password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';

MySQL 8 changed its default authentication plugin, and using ALTER USER (rather than the older SET PASSWORD syntax many older tutorials still reference) is the version-appropriate way to reset the password under this newer authentication system.

Exiting and restarting MySQL normally

EXIT;
sudo systemctl stop mysql
sudo systemctl start mysql

Verifying the new password works

mysql -u root -p

Why this specific sequence matters

Each step exists for a specific reason: safe mode to bypass the lost-password lockout, --skip-networking to prevent anyone else from connecting during that vulnerable window, FLUSH PRIVILEGES to work around a MySQL 8-specific quirk in the grant-tables-skipped state, and the ALTER USER syntax specifically because MySQL 8's default authentication plugin changed from earlier versions — skipping or reordering any of these is the most common reason this process fails partway through on a first attempt.