MySQL reset password for user account

last updated in Categories , ,

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
OR
mysql -u root -p -h localhost mysql
OR
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';
OR
SET PASSWORD FOR 'userNameHere'@'localhost' = 'passwordHere';
OR
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:
FLUS PRIVILEGES;
exit

Step 4 – Test new password

Now password has been changes. It is time to test it. Again syntax is
mysql -u jerry -p
OR
mysql -u jerry -p -h localhost
OR
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:
SELECT USER(),CURRENT_USER();
Sample session from above commands:
MariaDB MySQL reset password for user account

References

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.