Restoring the MySQL root password

This may not be obvious from the documentation, but is quite simple once we know how to. We need to

  • Put some commands in a file
  • Stop the MySQL server daemon.
  • Briefly run the server with an –init-file
  • Start the server

Put these commands in a file /tmp/foo:

UPDATE mysql.user SET password=PASSWORD('very-secret-password') WHERE user='root';

Then run these commands as root:

# /etc/init.d/mysqld stop
# /usr/libexec/mysqld --init-file=/tmp/foo &
# killall mysqld
# /etc/init.d/mysqld start

Command details will vary between Linux distributions.

The killall command is used because the init script can only stop the server if the server has been started also by the init script. Not to worry; the server will catch the signal and shut down cleanly.

That’s it!

You can reach me by email at “lars dash 7 dot sdu dot se” or by telephone +46 705 189090

View source for the content of this page.