How to Change User Password in MySQL

How to Change User Password in MySQL

MySQL is one of the most popular and widely-used database systems in the world. Because of its versatile relational database management system, many different types of software and applications utilize it for data storage and management. In MySQL, knowing how to change the user’s password is important. This will allow you to protect sensitive data and ensure that your databases remain secure. In this article, we’ll show you a step-by-step guide on changing your user password in MySQL.

Steps on Changing User Password in MySQL

Changing the user password in MySQL involves understanding the version you’re working with, as different versions might require different commands. Please be aware that you have the required admin privileges to update the password, and there’s no need for any special permissions to continue. Here’s a step-by-step guide to help you through the process.

Note: Before you begin, please remember to replace ‘username’, ‘new_password’, and ‘old_password’ with the actual username and the relevant passwords. Also, ensure you follow best practices for creating secure passwords.

#1 Check the MySQL Version

Open the terminal and log in to MySQL using this command.

mysql -u username -p

You’ll be prompted to enter your password. Once you’re logged in, you can run the following SQL command to see the version:

SELECT VERSION();

The MySQL version will be displayed as the output. You can also check the version without logging into the MySQL shell by running:

mysql --version 

This will display the MySQL client version, along with some other information, directly in your command-line interface.

#2 For MySQL version 5.7.6 or Later

Run the following SQL command to change the password.

ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

#3 For MySQL version before 5.7.6

Use this SQL command to change the password.

SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');

If the two methods mentioned earlier don’t work, you can also use the ‘mysqladmin’ command. Check the next step for details.

#4 Using mysqladmin (Command-Line Tool)

In the command line, run the following command:

mysqladmin -u username -p'old_password' password 'new_password'

#5 Flush Privileges

This ensures that the changes are applied immediately. Run the command below.

FLUSH PRIVILEGES;

You’ve done it! You’ve completed updating your password, enhancing the security of your database. This simple step of changing your password helps protect your MySQL database against potential hacking attempts. It’s an essential measure to ensure that your information remains safe and secure.

Conclusion

Searching for a managed database solution? Choose Gcore Managed Database for PostgreSQL so you can focus on your core business while we manage your database.

  • 99.9% SLA for uninterrupted service with high-availability architecture
  • Adjustable database resources for changing demands
  • Currently in free public beta

Start managing your database

How to Change User Password in MySQL

Subscribe
to our newsletter

Get the latest industry trends, exclusive insights, and Gcore
updates delivered straight to your inbox.