Change MySQL Root Password: Secure Your Database Management!

How to change or reset the MySQL root password

The MySQL root password guards the keys to your entire database — every table, every user, every record. Whether you’re rotating it as good practice or you’ve genuinely forgotten it on a server you administer, changing it is straightforward once you know the right command for your situation. We at GetMyPassword walk you through changing the root password when you know it, and the recovery process when you don’t.

Reset a forgotten MySQL root password
Resetting a forgotten MySQL root password from the command line.

Change the root password when you know it

If you can still log in, a single command does the job. The modern, recommended way is ALTER USER from inside the MySQL shell:

  • Log in with mysql -u root -p, then run:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPassword';
  • Finish with FLUSH PRIVILEGES; to reload the grant tables.

Prefer one line from the terminal? mysqladmin works too:
mysqladmin -u root -p'oldpassword' password "YourNewPassword"

Reset a forgotten root password

Locked out completely? You can reset it by starting MySQL without its permission checks, but you’ll need administrator access to the server:

  1. Stop the MySQL service.
  2. Restart it with --skip-grant-tables --skip-networking so it accepts a connection without a password.
  3. Connect with mysql -u root and run FLUSH PRIVILEGES;
  4. Set the new password:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPassword';
  5. Restart MySQL normally and log in with the new password.

A note for MySQL 8.0 and later

From MySQL 8.0, the default authentication plugin is caching_sha2_password, and the old mysql_native_password is deprecated (and removed in 8.4). If a client needs the legacy plugin, specify it explicitly:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YourNewPassword';

The --skip-grant-tables mode disables all authentication, so anyone could connect. Always pair it with --skip-networking and do the reset quickly, then restart normally.

Choose a password worthy of root

A database root account is one of the most valuable credentials on any server, so don’t settle for something short. Generate a long, high-entropy password with our password generator and store it in a secure place rather than a config note. Our guide to strong passwords explains why length is your best defence against brute-force attacks.

Frequently asked questions

How do I change the MySQL root password?

Log in with mysql -u root -p and run ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘newpassword’; then FLUSH PRIVILEGES;. You can also use the mysqladmin command from the terminal.

How do I reset a forgotten MySQL root password?

Stop MySQL, restart it with –skip-grant-tables –skip-networking, connect as root, run FLUSH PRIVILEGES, set a new password with ALTER USER, then restart MySQL normally.

Why does ALTER USER fail on MySQL 8?

MySQL 8 defaults to the caching_sha2_password plugin. If a client expects the old one, specify it with IDENTIFIED WITH caching_sha2_password BY ‘password’, as mysql_native_password is deprecated.

Help your friends stay safe. Share this article!