I accidentally deleted my MySQL root user today on one of my dev servers. I thought I'd write a quickie about how I restored it.

The basic information I gathered from the MySQL manual, but of course I needed to restore the account, not just reset the password. It's a simple matter of replacing that init file with the proper SQL to create the user.

  1. root# /etc/init.d/mysqld stop
  2. root# su mysql
  3. mysql$ pwd
  4. /home/mysql
  5. mysql$ echo "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'my_pass' WITH GRANT OPTION;"> mysql-init
  6. mysql$ mysqld_safe --init-file=/home/mysql/mysql-init &
  7. mysql$ rm mysql-init
  8. mysql$ mysql -uroot -p   # woot, works

(Note that it's important that you switch to the same user that MySQL uses when running mysqld_safe. This might be 'mysql' or something else, you'll need to check out your init files to see.)

4 Responses to “Quickie: Restoring a deleted MySQL root user”

  1. Chris Says:

    Thank you, these instructions worked great for me once I performed them as the mysql user as you say.

  2. Matt Says:

    After much ado.. your instructions worked! Thanks!

  3. Paulo Prieto Says:

    Christopher you just saved my life. Thanks a lot!!

  4. Juliano Moreira Says:

    Hi Chris, quick question.. do i need to create a user mysql on my server? I don't think I have a user mysql on my server. Thanks!

Leave a Reply