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:

Either do this as (linux user) root or prefix every command with sudo:

  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.

11 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

  9. touch /etc/mysql/mysql-init only gives can’t change – acces denied….

    Comment by yoeri — 18. February 2018 @ 12:48

  10. @yoeri: You should do this as root or use sudo.

    Comment by Christopher Kramer — 19. February 2018 @ 14:29

  11. […] You tried a dozen different howtos to change the root password of MySQL (5.7+) in a recent Ubuntu version (18.04+) and did not succeed? Like the one I posted in 2013? […]

    Pingback by MySQL root password in Ubuntu cannot be changed - Christosoft Blog — 2. February 2020 @ 16:09

RSS feed for comments on this post. TrackBack URL

Leave a comment