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
- Stop the MySQL service:
sudo service mysql stop
- Start it without grant tables:
sudo mysqld --skip-grant-tables &
- Connect as root:
mysql -u root mysql
- Set a new password (replace MYSECRET):
UPDATE user SET Password=PASSWORD('MYSECRET') WHERE User='root'; FLUSH PRIVILEGES; exit; - Stop the temporary process:
sudo pkill mysqld
- 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.