≡ Menu

How To Change MySQL Root Password

Question: How do I change MySQL root Password?

Answer: You can change MySQL root password using one of the following 3 methods.

These methods can be used on both Windows and Unix Environment including Ubuntu, Debian, CentOS, Fedora, RedHat, Arch Linux, SUSE etc.,

Method 1. How to Change MySQL Root Password Using mysqladmin Command?

You can change the mySQL root password using mysqladmin command as shown below. Please note that there is no space between -p and currentpassword.

# mysqladmin -u root -pcurrentpassword password 'newpassword'

Once you’ve changed it make sure you can login with your new password successfully as shown below.

# mysql -u root -pnewpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.25-rc-community MySQL Community Server (GPL)
mysql>

Method 2. How to Change MySQL Root Password From MySQL Prompt Using UPDATE SQL Command?

You can also use the standard update SQL command combined with the MySQL password function to change the password as shown below.

Login to MySQL root account using old password

# mysql -u root -poldpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.25-rc-community MySQL Community Server (GPL)
mysql>

Use the UPDATE Command to change root password.

mysql> UPDATE user SET password=PASSWORD('newpassword') WHERE user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Verify the new MySQL root password

Once you’ve changed it make sure you can login with your new password successfully as shown below.

# mysql -u root -pnewpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.25-rc-community MySQL Community Server (GPL)
mysql>

Method 3. How to Set MySQL Root Password Using mysqladmin Command?

This method works only if there is no password currently assigned for the root account. If you don’t have any password assigned to the root account, set the password without giving current password as shown below.

# mysqladmin -u root password 'newpassword'
[Note: There is no currentpassword for root in this example]

How to Change MySQL Regular User (non-root) Password Using mysqladmin Command?

You can use the same mysqladmin command to change password for a mySQL end-user account as shown below.

# mysqladmin -u jsmith -pcurrentpassword password 'newpassword'

How to Change MySQL Regular User (non-root) Password From MySQL Prompt Using UPDATE SQL Command?

You can also use the standard update SQL command combined with the MySQL password function to change the password of a non-root user as shown below.

mysql> UPDATE user SET password=PASSWORD('newpassword') WHERE user='ramesh';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Andrey July 17, 2009, 8:04 am

    What if you do not know the current password? Create a fresh MySql installation. Create some database. Copy everything from the mysql directory of the problem database to the directory of the new database. Change everything as needed and copy back to the mysql directory. And do not forget to stop the servers before copying files.

  • Ramesh Natarajan July 20, 2009, 12:31 am

    @Andrey,

    Thanks for the suggestion on what to do if someone do not know the current password. There is a better way to rest the forgotten root password on MySQL, which will be posted in the next article this Wed.

  • existing May 24, 2011, 5:47 pm

    When I run this query:
    “mysql> UPDATE user SET password=PASSWORD(‘newpassword’) WHERE user=’root’;”

    I get this result:

    “Query OK, 0 row affected (0.00 sec)
    Rows matched: 0 Changed: 0 Warnings: 0”

    It recognizes and updates if I target a different user, but not with “root”. Any ideas what is going on?

  • muhammed sekertekin October 7, 2011, 9:30 pm

    Wow your the best thank very nice and informative article this made my day…. muhammed sekertekin

  • prasad January 3, 2012, 4:24 am

    HI ,
    I FORGOT OLD PASSWORD AND NEED TO SET NEW ONE PLS. HELP

  • Lionel April 7, 2012, 10:19 am

    Your post helped me to set up phpmyadmin on localhost which was starting to confuse me because I could not login to my phpmyadmin when my root password is empty.

  • dhanesh mane October 16, 2012, 12:21 am

    I used this, works well. thx bro.

    UPDATE user SET password=PASSWORD(‘newpassword’) WHERE user=’root’;

  • Jnr December 11, 2012, 2:24 am

    Hi, when I use the update command line
    UPDATE user SET password=PASSWORD(‘newpassword’) WHERE user=’root’;
    I get the ERROR 1046 No database selected
    I am using mysql 5.5 command line client

  • vijay April 6, 2013, 8:19 pm

    I am also getting “No Database selected Error”

  • Frolic July 9, 2013, 3:43 am

    When you modify the “user” table directly with “UPDATE”, don’t forget to “FLUSH PRIVILEGES;” as the last step. Otherwise the changes may not come into effect.

  • Sasha April 3, 2014, 7:00 pm

    As usual, the Geek Stuff pulls through. Thanks!

  • Rajasekar1 December 18, 2016, 10:46 pm

    Hi,

    Am upgraded from mysql 5.6 to 5.7 but old root password not working.
    Tried resetting root password as u mentioned above but still not able to login.
    Getting ERROR 1524 (HY000): Plugin ‘*FAD5B411E62504980677E94A0F69DB643C54B389’ is not loaded.
    Need help…