blog.dotkam.com stats

AddThis Social Bookmark Button

MySQL: Reset Lost Root Password

Here is a quick Step-by-Step “how to” which helps restoring MySQL root password that was lost/forgotten.

It happens to everybody, especially if several distributed (different) systems are maintained, where the password is not the same. Here is what needs to be done to restore it:

Step 1: Stop MySQL daemon if it is currently running

Depending on the operating system MySQL is installed on, the daemon can be checked/stopped differently. Here is an example on how to do it in Unix-like systems.

NOTE: You might need to run it as a Unix System superuser (root) - depending on how the system is configured,
          and what permissions your Unix account is granted)

Here is how to stop/kill the existing mysql daemon, in case it is running:

      ps -ef | grep mysql - checks if mysql/mysqld is one of the running processes.
 
      pkill mysqld             - kills the daemon, if it is running.

Note: if pkill (’process kill’) is not on a particular Unix system, use kill -9 ‘pid’, where ‘pid’ corresponds to processes that were found with ps -ef | grep mysql

Step 2: Run MySQL safe daemon with skipping grant tables

      mysqld_safe --skip-grant-tables &

Step 3: Login to MySQL as root with no password

      mysql -u root mysql

Step 4: Run UPDATE query to reset the root password

In MySQL command line prompt issue the following two commands:

      UPDATE user SET password=PASSWORD("ualue=42") WHERE user="root";
      FLUSH PRIVILEGES;

“ualue=42” is a common password for “The Hitchhiker’s Guide to the Galaxy” people which reads “Ultimate Answer to Life, the Universe, and Everything=42“

Step 5: Stop MySQL safe daemon

Follow the first two steps, but this time kill (pkill) “mysqld_safe” instead of “mysqld”

Step 6: Start MySQL daemon

Depending on the operating system (Unix-like examples):

      /etc/rc.d/rc.mysql start

OR

      /etc/init.d/mysql start

OR

      /etc/rc.5/mysql start

etc.. check existing MySQL configuration

Step 7: Root password is reset and ready to use

Password is reset. Privileges are flushed. Start MySQL and login as root with the password set in step 4:

      mysql -u root -p mysql

Note: sometimes (most of the time) ‘root user’ privileges are required for the system (OS) in order to stop/start processes

what is next? Reset Lost Password in Sun Application Server


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

19 Responses to “MySQL: Reset Lost Root Password”

  1. Dude, you rock. I’m a total n00b with database stuffs … and hunting out this blog (?) on dial-up was so worth it for such a straight forward solution. Thank you …

  2. Thanksfor the help but I still hate mysql

  3. Nice script, I think I should try it. Thanks

  4. […] forget and loose passwords at least once every so often. Some time ago I wrote a tutorial on how to reset lost root password in mysql, and here is another similar tutorial on how to reset the lost domain password but this time for […]

  5. I can’t thank you enough for your guide. I am truly grateful.

  6. Thanks! Works fine for me!!!

  7. welcome :)

  8. I get the same error 1045 access denied for user ‘root’@'localhost’(using password: YES) but I am using Windows Vista and have forgotten my password

  9. @jeffrey,

    What step did you follow on Windows? I suspect that you did not stop/start the right mysql processes. Also did you find how to run “mysqld_safe”?

    – Toly

  10. Thanks a lot!

  11. Don’t use kill -9 with mysql! It shutdowns uncleanly. It’s very bad for a database! Use /etc/init.d/mysql stop

    http://speculation.org/garrick/kill-9.html

  12. Sorry. I am a total zilch in mysql.

    Question: in step 7, with this command ( mysql -u root -p mysql) , are you logging in as root with a password of mysql. As set in step 4? I thought the password set in Step 4 was: ‘value=42′.

    Should I backup everything first before I do the above.

  13. @Max,

    It can be confusing if you read it literally, but what really happens is you are telling mysql to log you in with the root user “-u root”, who will provide the password “-p”, AND choose a db named “mysql” for this user.

    Check out “Step 3″, see there is “mysql -u root mysql”? That is exactly the same statement as in “Step 7″ except in “Step 3″ you are not saying “-p” —> “I am going to provide the password for this user”.

    – Toly

  14. None of those methods worked on 5.0.32-Debian_7etch5. I tried init file method, tried skipping grant tables and various combination of user - no luck to reset the password.

  15. @auriux,

    Make sure before you run

    mysqld_safe --skip-grant-tables &

    all myslq (mysqld, etc..) processes are killed (not running). You can check if that is the case by

    ps -ef | grep mysql

    Only when none of the mysql processes are running you can start mysqld_safe.

    Let me know if you have any difficulties, and if you do, at what step?

    Good Luck,
    – Toly

  16. I’ve tried this (because it seems a root password is installed by default on my system). I can login ok thanks to the skip-grant-tables option, but i am not able to login again after I restart the system.

    This is on Dragonfly BSD.

  17. @colin,

    It might be that there is a startup script somewhere that resets a myslq server password in case it is empty (wild guess).

    Try to actually SET the password, instead of filling it blank. Let me know if it works for you,

    – Toly

  18. Thx for sharing Toly!

tell me something...
  1. (required)
  2. (valid email required)
  3. Captcha
  4. (required)