How to Change or Create MySQL Passwords

MySQL is a powerful database management system that requires secure passwords for its users. In this guide, we will walk you through the steps to create or alter MySQL passwords, including how to set a default password when installing MySQL for the first time and how to update an existing password.

Step 1: Open Terminal or Command Prompt

To start, open a terminal or command prompt on your Linux system or Windows machine. If you’re using Linux, navigate to the directory where MySQL is installed by default (usually /usr/local/mysql). On Windows, navigate to the directory C:\Program Files\MySQL\MySQL Server [Version] \bin.

Step 2: Create a Default Password for Root User

If you’re installing MySQL for the first time, the root user will not have a password. To create a default password, run the following command:

mysqladmin -u root password [PASSWORD_PRETENDIDA]

Replace [PASSWORD_PRETENDIDA] with your desired password.

Alternatively, if you’ve already installed MySQL and want to set a new default password for the root user, use the following command:

mysqladmin -u root password [PASSWORD_PRETENDIDA]

Replace [PASSWORD_PRETENDIDA] with your desired password.

Step 3: Update an Existing Password

If you’ve already created a password for your MySQL user and want to update it, use the following command:

mysqladmin -u [UTILIZADOR] -p'[PASSWORD_ACTUAL]' PASSWORD '[PASSWORD_PRETENDIDA]'

Replace:

  • [UTILIZADOR] with the username of the MySQL user whose password you want to update.
  • [PASSWORD_ACTUAL] with the current password for the user.
  • [PASSWORD_PRETENDIDA] with your desired new password.

For example:

mysqladmin -u john -p'myoldpassword' password 'newpassword'

This command updates the password for the user “john” to “newpassword”.

Step 4: Verify Password Change

To verify that the password change was successful, use the following command:

mysql -u [UTILIZADOR] -p'[PASSWORD]'

Replace:

  • [UTILIZADOR] with the username of the MySQL user.
  • [PASSWORD] with your desired new password.

For example:

mysql -u john -p'newpassword'

If the command succeeds, you will be prompted to enter the new password. If not, try again or contact your system administrator for assistance.

Conclusion:
Changing or creating MySQL passwords is an essential part of database management. By following these steps, you can ensure that your MySQL users have secure and unique passwords to prevent unauthorized access to your database.