Recovering a Lost MySQL Root Password

Recovering a Lost MySQL Root Password

If nobody remembers the MySQL root password, you can still get back in as long as you have sudo access to the server. The trick is to start MySQL briefly without its permission checks.

Steps

  1. Stop the MySQL service:
    sudo service mysql stop
  2. Start it without grant tables:
    sudo mysqld --skip-grant-tables &
  3. Connect as root:
    mysql -u root mysql
  4. Set a new password (replace MYSECRET):
    UPDATE user SET Password=PASSWORD('MYSECRET') WHERE User='root'; FLUSH PRIVILEGES; exit;
  5. Stop the temporary process:
    sudo pkill mysqld
  6. Start MySQL normally:
    sudo service mysql start

Newer MySQL versions

The PASSWORD() function shown above was removed in MySQL 8. On newer servers, run FLUSH PRIVILEGES; and then ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password'; instead. While grant tables are off, anyone on the server can connect, so finish quickly.

Stuck on something similar? Our server and automation work team is happy to take a look.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.