DeutschEnglish

Submenu

 - - - By CrazyStat - - -

1. September 2013

MySQL: reset a forgotten MySQL root password

Filed under: DBMS,Linux,Security,Server Administration — Christopher Kramer @ 14:37

Just in case you don’t know the password of your MySQL root user anylonger, this is how you can set a new one.

Windows users, follow this guide.

Linux / Unix users: This is for Debian (wheezy) and MySQL 5.5, but should work more or less the same on any linux/unix:

  1. Stop the MySQL server
    service mysql stop
  2. Create a init file:
    touch /etc/mysql/mysql-init
  3. Write the following into this file (adjust the PW):
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
  4. Restart MySQL with this init-file:
    mysqld_safe --init-file=/etc/mysql/mysql-init
  5. The new password should work now. Try it:
    mysql -u root -p
  6. Restart MySQL normally:
    service mysql restart
  7. Remove the init-file
    rm /etc/mysql/mysql-init

And you’re done. I hope this helps somebody.

Note: if it does not work, it is most likely because the MySQL process cannot access the init-file (e.g. because of missing access rights). In this case check your (sys)logs for stuff like this:

mysqld: 130901 14:20:30 [ERROR] /usr/sbin/mysqld: File '/root/mysql-init' not found (Errcode: 13)

I had this problem first because I tried creating the init-file in /root where the mysql-process was not able to read it.

Recommendation

Try my Open Source PHP visitor analytics script CrazyStat.

8 Comments »

  1. Great! Worked, and saved me 🙂
    THANK YOU

    Comment by Giovanni — 29. January 2015 @ 13:48

  2. Permission denied 🙁

    Comment by Simon — 4. February 2015 @ 20:20

  3. @Simon: are you logged in as root? or tried sudo?

    Comment by Christopher Kramer — 5. February 2015 @ 10:37

  4. Yo dude! Certainly helped me! Thanks

    Comment by JimmehWhy — 10. April 2015 @ 23:13

  5. in windows it is not working

    Comment by Bharath k — 28. May 2015 @ 08:15

  6. @Bharath k: The guide is for Linux 😀
    But on Windows, it is working more or less the same way. See: http://dev.mysql.com/doc/mysql-windows-excerpt/5.5/en/resetting-permissions-windows.html

    Comment by Christopher Kramer — 28. May 2015 @ 10:34

  7. Thank you so much! This worked great on my raspberry!

    Comment by Daniel — 11. February 2016 @ 11:15

  8. Thanks, worked for my Raspberry Pi 3, but couldn’t work out how to close it. Ctrl+Z

    (Might have been because I didn’t press enter after the SET PASSWORD FOR line?)

    Comment by abisdad — 3. July 2017 @ 15:52

RSS feed for comments on this post. TrackBack URL

Leave a comment

%d bloggers like this: