How-To Geek

Change or Set the MySQL Root password

For every database, you should set the root or sa passwords to something other than the default, unless you want to get hacked. For mysql, the system administrator user is called root. You will use the mysqladmin utility from a command line to set the new password. Notice that there are two commands to be run.


mysqladmin -u root password “newpassword”

mysqladmin -u root -h host_name password “newpassword”


mysqladmin -u root password ws8dr8as3

mysqladmin -u root -h localhost password ws8dr8as3

You will also want to restart the database server after running this command

sudo /etc/init.d/mysql restart

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/11/06

Comments (12)

  1. Lior

    I don’t know why, but I needed to use
    mysqladmin -u root -p”newpassword” -h host_name password “newpassword”
    instead your second command.

  2. xunshirine

    After the second command terminal gives that

    mysqladmin: connect to server at ‘localhost’ failed
    error: ‘Access denied for user ‘root’@’localhost’ (using password: NO)’

    How can I solve that?
    Thanks in advance.

  3. Steven Bao

    Works like a charm on MySQL5. Thanks for the tip.

  4. DW

    Resetting the password this way will put the password in your shell’s history:
    $ history | grep mysqladmin

    For that reason, you might want to consider setting/resetting the password from the mysql client:

    $ mysql -u root mysql
    mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘newpasswordgoeshere’);

  5. JDO

    That will also leave it in a file, the .mysql_history file.

  6. Kabi

    Good Stuff poeple

  7. luis

    Thanks DW i’m locking for this command in the entire internet!!

  8. antonio1

    that not working in case of first time setting the password for root. The message is “Access denied for user ‘root’@’localhost’ (using passowrd: NO)”
    I think it must be a way using mysqladmin…

  9. russjones

    This will be very useful.

  10. zygot

    Nice article. So simple yet very educating.

  11. reality

    Thanks fro the additional information.

  12. shis

    Thanks. This will help me a lot.

More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!