Change MySQL root password

Q. How do I change MySQL server root password under Linux / UNIX operating system at shell prompt?

ADVERTISEMENTS

A. You need to mysqladmin command which is a client for performing administrative operations. You can use it to check the server’s configuration and current status, to create and drop databases, change mysql root password and more.

How do I change mysql root password?

If you are setting a root password first time, use any one of the following command syntax:
mysqladmin password “NEW-PASSWORD
OR
mysqladmin -u root password “NEW-PASSWORD
OR
mysqladmin -u root -h localhost password “NEW-PASSWORD

To set password to d3s9cXq, enter:
# mysqladmin -u root password "d3s9cXq"

Please note that if the NEW-PASSWORD value contains spaces or other characters that are special to your command interpreter, you need to enclose it within quotes. Thus, the next time you invoke mysqladmin (or any other client program such as mysql) using the same account, you will need to specify the new password. So to login as root, enter the command:
# mysql -u root -p
OR
# mysql -h localhost -u root -p

Where,

  • -u root: Specify username
  • -p : Prompt for password

If you would like to change current password d3s9cXq to OlzzYs$qz, enter the following command:
# mysqladmin -u root -p password "OlzzYs$qz"
Output:

Enter password: <enter old password>
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
4 comments… add one
  • Nick May 10, 2008 @ 14:29

    Where do you type those commands??

  • 🐧 nixCraft May 10, 2008 @ 14:43

    At a shell prompt.

  • crazy guy Jan 8, 2012 @ 9:30

    wat’s a shell prompt?? is it the same as mysql console??

  • kristina Oct 31, 2015 @ 5:07

    Pls help.. i have tried everything and still can’t login

    error i get is “Access denied for user ‘root’@’localhost’ (using password: YES)”

    I am using the correct password and am on a MAC

    I entered the following:
    # mysql -h localhost -u root -p
    and # mysqladmin -u root password “d3s9cXq”

    but keep getting the same error.
    thanks

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.