Blog

03/06/2019

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
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)]>

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:

References

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

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.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...