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:
References
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |