MySQL: How to reset the root account

Published:

I just managed to mess up the MySQL root account. Not a smart idea. After some MySQL manual reading and some serious Google-Fu, I figured out how to fix it.

  1. Stop the mysql server
  2. Add the following to the my.cnf file
[mysqld]
skip-grant-tables
  1. Start the mysql server
  2. Start a mysql client and run the following query
REPLACE INTO mysql.user VALUES ('localhost','root',PASSWORD('blah'),'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);
  1. Quit the mysql client
  2. Stop the mysql server
  3. Remove what we added to my.cnf in step 2
  4. Restart the mysql server

Tadaa.