MySQL reset password for user account

I am a new Linux system user. How do I reset a password for a user account named jerry using mysql command line option?

mysql is a simple SQL shell for MySQL and MariaDB database server. You need to use the mysql command to reset a password for a user account on a Linux or Unix-like system. If you are using MS-Windows use mysql.exe file and the sql commands are same. The SET PASSWORD sql command assigns a password to an existing MariaDB user account. Newer version of MySQL/MariaDB server can use the ALTER USER sql command too. The procedure to reset the password for the user account named jerry as follows:

Step 1 – Login as root

Run mysql command:
mysql -u root -p mysql
mysql -u root -p -h localhost mysql
mysql --user root --password --host localhost msyql
Sample outputs:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

Step 2 – MySQL reset password for user named jerry

The syntax is as follows to reset password (depends upon your version of mysql/mariadb server):
ALTER USER userNameHere IDENTIFIED BY 'passwordHere';
SET PASSWORD FOR 'userNameHere'@'localhost' = 'passwordHere';
SET PASSWORD FOR 'userName'@'localhost' = PASSWORD('newPass');
For example, if you had an entry with User and Host column values of ‘jerry’ and ‘localhost’, you would write the statement like this at the mysql/maridb shell prompt:
SET PASSWORD FOR 'jerry'@'localhost' = PASSWORD('newPassHere');
Sample outputs:

Query OK, 0 rows affected (0.00 sec)

Step 3 – Exit from mysql shell

You must flush privileges and exit to the shell using following two sql commands:

Step 4 – Test new password

Now password has been changes. It is time to test it. Again syntax is
mysql -u jerry -p
mysql -u jerry -p -h localhost
mysql --user jerry --password --host localhost db_name_here
When promoted type password you set earlier in step # 2. You can verify who you are with the following sql command:
Sample session from above commands:
MariaDB MySQL reset password for user account


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum