Reset your MySQL root password

Forgetting passwords isn't generally a good idea. It does happen, though, and if it happens to your MySQL server, this is the process you need to follow in order to recover it.

First of all, you need to stop the running MySQL server. You need to be root on your system, then:

# /etc/init.d/mysql stop

Now, we need to make a text file called an init file. The contents of this file will be executed in a moment when we restart the MySQL server in a special way. So, create a new text file in your home folder and place this in the file:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');

Obviously, substitute in your desired password (leaving the quotes intact). Save the file as passwordinit.

Now, we need to start the MySQL server with this init file. Still as root on the system, run:

# mysqld_safe --init-file=~/passwordinit &

As soon as you press Enter, the password has been changed. Now I recommend restarting the MySQL server in the normal way, as follows:

# /etc/init.d/mysql restart

And that's it. Be more careful next time.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Reset your MySQL root password

  1. # Posted on 11 March 2008 at 09:12 AM

    <strong>Story added...</strong>

    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here:

    http://www.fsdaily.com/EndUser/Reset_your_MySQL_root_password...



  2. # Posted on 12 March 2008 at 12:28 AM

    [...] FOSSwire » Reset your MySQL root password Forgetting passwords isn’t generally a good idea. It does happen, though, and if it happens to your MySQL server, this is the process you need to follow in order to recover it. (tags: mysql password recovery sql) [...]



  3. # Posted on 08 April 2008 at 12:45 AM

    [...] Originally published on FOSSwire [...]



  4. # Posted on 14 April 2008 at 03:24 PM

    <strong>Resettare la password di root di Mysql...</strong>

    In caso di smarrimento della password di root di Mysql è possibile seguire questi semplici passi per resettare la password e inserirne una nuova. Prima di tutto, è necessario arrestare il server MySQL in esecuzione:

    sudo /etc/init.d/mysql stop

    ...



  5. # Posted on 23 April 2008 at 09:38 PM

    [...] Originally published on FOSSwire [...]



  6. Anonymous (guest)

    # Posted on 23 June 2008 at 04:47 AM

    In where should i type all this commands such as

    /etc/init.d/mysql , and how should i opent the thing. Thnx



  7. Peter (guest)

    # Posted on 23 June 2008 at 09:24 AM

    Open up a terminal/console program, then log in as root by typing the following and pressing Enter:

    <code>su -</code>

    Then type the commands in the tutorial (remember, the # or $ sign isn't to be typed in as well, it's just to symbolise the end of the prompt).

    If you're on Ubuntu, <strong>su -</strong> won't work, so instead prefix all the commands in the tutorial with <strong>sudo</strong>.



Home » Articles » Reset your MySQL root password