0
0
MysqlHow-ToBeginner · 3 min read

How to Change User Password in MySQL Quickly and Safely

To change a user password in MySQL, use the ALTER USER 'username'@'host' IDENTIFIED BY 'new_password'; command. There is no need to run FLUSH PRIVILEGES; as ALTER USER applies changes immediately.
📐

Syntax

The basic syntax to change a MySQL user password is:

  • ALTER USER 'username'@'host' IDENTIFIED BY 'new_password'; - changes the password for the specified user.

Replace username with the MySQL user name, host with the host (often localhost), and new_password with the desired password.

sql
ALTER USER 'username'@'host' IDENTIFIED BY 'new_password';
💻

Example

This example changes the password for user alice connecting from localhost to MyNewPass123!.

sql
ALTER USER 'alice'@'localhost' IDENTIFIED BY 'MyNewPass123!';
Output
Query OK, 0 rows affected (0.01 sec)
⚠️

Common Pitfalls

Common mistakes when changing MySQL passwords include:

  • Not specifying the correct host part of the user, which defaults to localhost but might be different.
  • Using the deprecated SET PASSWORD syntax in newer MySQL versions.
  • Not having sufficient privileges to alter user accounts.
sql
/* Wrong: missing host or wrong user format */
ALTER USER 'alice' IDENTIFIED BY 'pass';

/* Correct: specify user and host */
ALTER USER 'alice'@'localhost' IDENTIFIED BY 'pass';
📊

Quick Reference

CommandPurpose
ALTER USER 'user'@'host' IDENTIFIED BY 'password';Change the password for a MySQL user
SELECT User, Host FROM mysql.user;List all MySQL users and their hosts

Key Takeaways

Use ALTER USER with 'username'@'host' to change passwords in MySQL.
Specify the correct host part for the user to avoid errors.
Avoid deprecated commands like SET PASSWORD in modern MySQL versions.
Ensure you have proper privileges to change user passwords.