Is this correct to change the root password of MySql?

echo "use mysql; update user set password=PASSWORD("NEWPASSWORD") where User='root'; flush privileges; quit;" | mysql -u root -pOLDPASSWORD

I've seen this doc and a few others, but I don't find one "simple" definitive answer in a few lines.

Also, should I stop mysql server before doing it, and restarting it after? (I tried mysql stop or mysql -uroot -pOLDPASSWORD stop on my Debian but none of them worked).

Note: mysql -V gives mysql Ver 14.14 Distrib 5.5.40, for debian-linux-gnu (x86_64).

1 Answer 1


The proper command to do so is:

mysql -uroot -poldp4ssw0rd -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newp4ssw0rd');"

assuming that oldp4ssw0rd and newp4ssw0rd are the old and new passwords.

You do not need to stop or restart the MySQL server at any time. In fact, the service needs to be running for you to connect to it and issue the command above.

Note: this command solves your problem; however, in general it is not a good idea to pass passwords as command line arguments, as they could be seen by other logged-in users.

  • Thanks. Is this the same password than when using update user set password=PASSWORD("NEWPASSWORD") where User='root'? Also what command do you usually use to restart? service mysql restart?
    – Basj
    Dec 4, 2017 at 11:15
  • 1) Yes. 2) Yes, if you're using Debian or RHEL6. /etc/init.d/mysql restart also works.
    – dr_
    Dec 4, 2017 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .