Recover MySQL root Password

Posted on in Categories Debian Linux, Linux, MySQL, OpenBSD, RedHat/Fedora Linux, Solaris, Suse Linux, Tips, Troubleshooting, Ubuntu Linux, UNIX last updated April 18, 2006

You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.

Step # 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password.

Step # 3: Connect to mysql server as the root user.

Step # 4: Setup new mysql root account password i.e. reset mysql password.

Step # 5: Exit and restart the MySQL server.

Here are commands you need to type for each step (login as the root user):

Step # 1 : Stop mysql service

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld.

Step # 2: Start to MySQL server w/o password:

# mysqld_safe --skip-grant-tables &
Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step # 3: Connect to mysql server using mysql client:

# mysql -u root
Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step # 5: Stop MySQL Server:

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables

Step # 6: Start MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

311 comment

    1. You can recover MySQL database server password with following five EASY steps.

      Why is it that EVERYTIME someone writes something is easy, it is not?

      I have no idea how to shut off mysql. And there is no explanation here.

        1. I am at lubuntu 13.04 and i Can’t use mysqld commands
          i.e when i use # /etc/init.d/mysql stop

          This is shown on terminal:

          “Rather than invoking init scripts through /etc/init.d, use the service(8)
          utility, e.g. service mysql stop

          Since the script you are attempting to invoke has been converted to an
          Upstart job, you may also use the stop(8) utility, e.g. stop mysql”

          when I use # mysqld_safe –skip-grant-tables &
          this is shown:

          140122 23:07:14 mysqld_safe Can’t log to error log and syslog at the same time. Remove all –log-error configuration options for –syslog to take effect.
          140122 23:07:14 mysqld_safe Logging to ‘/var/log/mysql/error.log’.
          140122 23:07:14 mysqld_safe A mysqld process already exists

          (I think this post are a bit old)

          1. Worked to me after using ps -A to check what process is running Mysql then Killing it with kill command, after I folowed the steps and worked well. Tanks!
            This post Is OLD BUT GOLD!

  1. Um… I’ve been trying to actually change the password for about 3 days now, and I tried this tutorial… No errors, but when I start the MySQL client and type in my password it just beeps and goes away 🙁 Is there any other way?

  2. I’ve done everything exactly as it says , but still canot connect with my mysql server. I allways have this error: ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
    Even when i do NOT use a password , it just not working! i tryed to reintall mysql through synaptic , but still the same shit… No difference. Can enyone give an advise?

  3. Hi,

    Can I remove an existing password following you guide, except that i leave this portion:

    mysql> update user set password=PASSWORD(“NEW-ROOT-PASSWORD”) where User=’root’;

    as this :

    mysql> update user set password=PASSWORD(“”) where User=’root’;

    Would this work? I need to remove the password for a bit, for testing purposes…and then set the password again….any help/advise is much appreciated…

    -nisha-

  4. Hi again,

    The problem I mentioned above, well, I tested it,and it works….thank you for the perfect guide 😉
    This is the i-can’t-remember-number-of-times that one of your articles has helped me… so, thanks for that too!

  5. Gleb, you need to use the unix shell as root to kill the mysql server process directly, then you can follow the mysql start without privileges command and get back in that way.

    If you don’t know how to get a process list and send it a signal, you should read up just a little bit on that.

    man ps
    man kill

    But something like:
    ps aux | grep mysql
    kill -TERM [processnumber# from command above]

    (yes there is a one step command, but I personally ashew wildcards in my kill commands)

  6. Thank you very much. Windows users, you can do all of those commands from the command prompt, make sure you run it in admin mode, and change directory to where the mysql files are located, so when you run those commands it works. 🙂

  7. THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU.

    Most sincerely,
    Darrell Goodman
    Toronto, Canada

  8. This didn’t work for me. I now get the following when trying to log in: (I’m logged in as root):

    ctru2:~ # mysql -u root -p
    Enter password:
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

    What does this mean, and how do I get around it?

  9. I have tried your solution 5 times. But not working :((.
    [[email protected] /]# /etc/init.d/mysqld stop
    Stopping MySQL: [ OK ]
    [[email protected]45903 /]# mysqld_safe –skip-grant-table &
    [1] 2278
    [[email protected] /]# Starting mysqld daemon with databases from /var/lib/mysql
    mysql
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 1
    Server version: 5.0.83 Source distribution

    Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> update user set password=’blocialdb123′ where user =’root’;
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 0 Changed: 0 Warnings: 0

    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    mysql> quit;
    Bye
    [[email protected] /]# /etc/init.d/mysqld stop
    STOPPING server from pid file /var/run/mysqld/mysqld.pid
    090730 13:58:21 mysqld ended

    Stopping MySQL: [ OK ]
    [1]+ Done mysqld_safe –skip-grant-table
    [[email protected] /]# /etc/init.d/mysqld start
    Starting MySQL: [ OK ]
    [[email protected] /]# mysql -u root -p
    Enter password:
    ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
    [[email protected] /]#

  10. hi , at first i want to thank you for your post its very useful for me
    actually i do all of above but when i want to access mysql with root (#mysql -u root -p)
    it said that ” ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES) ”

    could you please help me about this

  11. Awesome, I have been playing using MySQL on and off for almost 2years to understand it from a higher level and still getting use to it! This is the best straight forward instructions I’ve seen….and trust me I’ve looked at heaps 🙂

    So from a newbie, THANKS for taking the time!
    John B.

  12. How do i log in as root user if I do not know the password? Also, when I use the # /etc/init.d/mysql stop, it tells me that ‘#’ is not recognized as an internal or external command. I am not very knowledgeable about these things. I am on a windows system and I don’t know the root password. Please help. Thanks.

  13. Hi guys. This help looks soo helpfull but i dont know how to use it. i really need to reset my password. but i dont know how to Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option. I went to mysqlbinmysqld but i cant type or do anything! can someone help me how to do those steps?

  14. This was absolutely what they say, hitting the nail on the head. I was getting the error message and was hitting my hed on the wall when this gentleman came along and it worked like smooth butter.
    Thanx man , you rock

  15. I tried many times… Do not work for me…
    I Can’t change the password and I can’t remember the previous one.
    Maybe I doing some mistake with the line:

    update user set password=PASSWORD(“MyPassword”) where User=’root’;
    when I run: mysql -u root -p

    I put MyPassword…. And I get this error:

    ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

    Does anybody knows what I doing wrong?

  16. actually i used wrong command….
    update mysql.user set password=”root” where user=”root” instead of
    update mysql.user set password=password(“root”) where user=”root”
    plz temme how to recover my password…thnx in advance

    1. !!!! FULL — BUT QUICK — REINSTALL !!!!

      To the ones still suffering from this persistent — and annoying — issue, the best solution I’ve come across so far is to remove & re-install mysql (using yum, apt-get, dpkg, or whatever you like the most).

      Now, the tricky step that most of the online tutorials forget to mention is that after removing mysql and — of course — BEFORE reinstalling it, you MUST remove the original db’s directory, usually under /var/lib/mysql/

      The latter is specially true for CentOS users.

      Here´s a wrap-up of the whole process:

      1) Make sure mysql is not running:
      [[email protected] ~] # /etc/init.d/mysqld stop

      2) Remove mysql:
      [[email protected] ~] # yum remove mysql-server
      [[email protected] ~] # yum remove mysql

      2) Remove the offending db:
      [[email protected] ~] # rm -rf /var/lib/mysql

      3) Re-install mysql:
      [[email protected] ~] # yum install mysql-server
      [[email protected] ~] # yum install mysql
      [[email protected] ~] # yum install mysql-devel

      4) Re-install php’s access to mysql:
      [[email protected] ~] # yum install php-mysql

      5) Start your newly installed db engine:
      [[email protected] ~] # /etc/init.d/mysqld start

      Now you can log in without using a password.

      6) Set a new password for root:
      [[email protected] ~] # mysql
      mysql> USE mysql;
      mysql> UPDATE user SET Password=PASSWORD(‘new-password’) WHERE user=’root’;
      mysql> FLUSH PRIVILEGES;
      mysql> EXIT

      I hope this solution saves you some headaches. It took me several hours to find out what was causing the problem.

      Cheers to all.
      Theo

  17. hi
    10.6 OS 64bit
    i couldn’t get
    bash-3.2# /etc/init.d/mysql start
    bash: /etc/init.d/mysql: No such file or directory

    on a mac the path is /usr/local/mysql/support-files/mysql.server start

    so i used this /usr/local/mysql/support-files/mysql.server start –skip-grant-tables ( got this from here)

    and followed your instructions

    any idea why mysqld doesn’t work to start or stop server

    i get this
    bash-3.2# mysqld stop
    110321 13:33:12 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql-5.5.10-osx10.6-x86_64/data/ is case insensitive
    110321 13:33:12 [ERROR] Fatal error: Please read “Security” section of the manual to find out how to run mysqld as root!

    110321 13:33:12 [ERROR] Aborting

    110321 13:33:12 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

  18. I don’t have the MySQL root password as I had help setting it up, and the he never gave me the root password I now I can’t find the idiot (for Months).

    I tried the tutorial above, but I can’t get in. The error I get is “ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)”

    Where am I supposed to insert the new username and password?

    Thanks!
    Mats

  19. I encountered the stunning situation where there was no root user

    replace

    update user set password=PASSWORD(“NEW-ROOT-PASSWORD”) where User=’root’;

    with

    insert into user(Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv, Create_view_priv, Show_view_priv, Create_routine_priv, Alter_routine_priv, Create_user_priv, ssl_type, ssl_cipher, x509_issuer, x509_subject, max_questions, max_updates, max_connections, max_user_connections) values ('localhost', 'root', PASSWORD("my_password"), 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y',   'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y',    'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0');
    
    1. Same here — no root user, 0 rows affected on the update… couldn’t figure it out. your insert worked fine for me, then ran the update to set the password, then flushed privs, then restarted mysql…. done and working now… thanks!

  20. I think there is a mis-communication in your tiourtal…I copied and pasted these two lines:./bin/mysqladmin -u root password ‘**PASSWORD**’./bin/mysqladmin -u root -h **IP_ADDRESS** password ‘**PASSWORD**’and I got this error:Unknown MySQL server host ‘**IP_ADDRESS**’ (1)’Check that mysqld is running on **IP_ADDRESS** and that the port is 3306.You can check this by doing ‘telnet **IP_ADDRESS** 3306′What should I do?

  21. [email protected]:~$ mysqld_safe –skip-grant-tables
    nohup: ignoring input and redirecting stderr to stdout
    Starting mysqld daemon with databases from /var/lib/mysql
    mysqld_safe[7459]: started
    STOPPING server from pid file /var/run/mysqld/mysqld.pid
    mysqld_safe[7469]: ended

    mysqld started in safe mode but suddenly it ended as above. Any idea ??

  22. Worked very well. Thanks!!
    I did it on both development and live server hosting critical University databases today running Suse Linux. Saved me a lot after many days.
    There were one or things I had to add *:

    (1) Stop the server: /etc/init.d/mysql stop (be sure to do it at off peak hours!)
    (2) Start the mysql daemon: # mysqld_safe –skip-grant-tables &
    (3)* While its running, open another terminal and login to mysql
    # mysql -u root (hit enter)
    (4) Now you are logged in.
    mysql>use mysql;
    mysql>update user set password=PASSWORD(“YOUR_NEW_PASS”) where User=’root’;
    you should see the output: Query ok etc.
    mysql>flush privileges;
    mysql>quit
    (5) stop the server: /etc/init.d/mysql stop
    (6) *Confirm that no mysql processes are still running (in my case they were and made the server fail to start). To check this: run this
    ps ax | grep mysql
    Now kill the process IDs of any mysql still running in the background using kill PID
    e.g. kill 4425. Do not use kill -9 as this may crash the database entirely!!
    Confirm no mysql process is still running: ps ax | grep mysql
    (7) Then start the server: /etc/init.d/mysql start
    (8) login with your new password: mysql -u root -p

    That worked for me on SLES10. hope it helps someone.

    Thanks.

  23. What about specifying an alternate port? I’m trying

    mysqld_safe –skip-grant-tables –port=3307

    and getting:

    130530 21:05:21 mysqld_safe Logging to ‘/var/log/mysqld-error.log’.
    130530 21:05:21 mysqld_safe A mysqld process already exists

    It doesn’t acknowledge the –port parameter ….

  24. Can you please tell me what is going on?
    It never finishes step 2

    [email protected]:~$ sudo /etc/init.d/mysql stop
    * Stopping MySQL database server mysqld [ OK ]
    [email protected]:~$ sudo mysqld_safe –skip-grant-tables &
    [3] 5595
    [email protected]:~$ 131030 16:44:40 mysqld_safe Can’t log to error log and syslog at the same time. Remove all –log-error configuration options for –syslog to take effect.
    131030 16:44:40 mysqld_safe Logging to ‘/var/log/mysql/error.log’.
    131030 16:44:40 mysqld_safe A mysqld process already exists

    I tried opening a second terminal with
    sudo mysql -u root
    but it told me that it wouldn’t open with no password. What is the story with the log files? Is this is what is stopping it?

    Thanks,
    Ilan

  25. This page save my ass.

    But I logged to mysql as debian-sys-maint user, the password is located at /etc/mysql/debian.cnf then I followed the steps on this page.
    The debian-sys-maint user has full privileges.

  26. Hi,

    Is there any way to view the password of Mysql command line client.
    I dont want to change or reset the password because of the changing month password policy and I would be in a trouble.

  27. Somehow the password field changed to authentication_string.

    UPDATE mysql.user
        SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
        WHERE User = 'root' AND Host = 'localhost';
    
  28. hiii , am using edureka for hadoop purpose in sql prompt i was forgott the password, i have already tried all the process u have mentioned in upper steps. But the prblm is that whenever i stop the sqld process and goes for start sqlserver without password its showing permission denied and if i tried the same process using sudo mode it will ask for sudo edureka password can u suggest me how should i recover my password.If u need then i will provide its screenshot also.

Leave a Comment