Last Updated: 28 June, 2023
To change a user's password in MySQL, we can follow these steps:
Connect to MySQL: Open a terminal or command prompt and enter the following command, replacing username with our MySQL username and password with our current password:
mysql -u username -p
Enter the current password when prompted.
Once we're connected to the MySQL server, switch to the MySQL database by running the following command:
USE mysql;
To view the current users and their privileges, we can run the following query:
SELECT user, host FROM user;
To change the password for a specific user, use the UPDATE statement with the SET PASSWORD clause. The following example shows how to change the password for a user named myuser:
UPDATE user SET authentication_string = PASSWORD('new_password') WHERE user = 'myuser';
In MySQL 8.0 and above, the authentication_string field is used to store the password.
Note: If we are using an older version of MySQL (prior to 5.7.6), use SET PASSWORD instead of authentication_string.
After updating the password, you need to flush the privileges so that the changes take effect:
FLUSH PRIVILEGES;
Finally, exit the MySQL prompt by typing:
EXIT;
That's it! The password for the specified user should now be changed to the new password you provided. Remember to use the updated password when connecting to MySQL in the future.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com