๐Ÿ”‘ MySQL Change User Password โ€“ Securely Update Account Credentials


๐Ÿงฒ Introduction โ€“ Why Change User Passwords?

Changing user passwords in MySQL is essential for maintaining database security, password rotation policies, and responding to account compromises. Whether you’re resetting a forgotten password or enforcing stricter access controls, MySQL provides secure and flexible ways to update user credentials.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to change a MySQL userโ€™s password securely
  • Methods using ALTER USER, SET PASSWORD, and admin overrides
  • Best practices for password policies
  • Version compatibility notes for MySQL 5.x and 8+

๐Ÿ› ๏ธ 1. ALTER USER โ€“ Recommended Method (MySQL 5.7.6+ / 8.0+)

๐Ÿ”น Syntax

ALTER USER 'username'@'host' IDENTIFIED BY 'NewPassword';

๐Ÿ”น Example

ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'StrongerP@ss123!';

Explanation:
This updates the password for app_user to the new string securely.

โœ… Preferred method in MySQL 5.7.6 and above
โœ… Supports password expiration, validation, and history


๐Ÿ” 2. SET PASSWORD โ€“ Alternative Method

๐Ÿ”น Syntax (For Current Logged-In User)

SET PASSWORD = 'NewPassword';

๐Ÿ”น Syntax (For Another User)

SET PASSWORD FOR 'username'@'host' = 'NewPassword';

๐Ÿ”น Example

SET PASSWORD FOR 'report_user'@'%' = 'ReporT@123!';

Note:
MySQL 8.0 uses ALTER USER as the preferred method. SET PASSWORD is still supported but lacks some policy controls.


๐Ÿ‘๏ธ 3. Check Current User

SELECT CURRENT_USER();

โœ… Useful before running SET PASSWORD if you’re unsure who you’re logged in as.


๐Ÿงฏ 4. Reset Password as Root (Admin Recovery)

๐Ÿ”น Steps:

  1. Log in as root or another admin-level account
  2. Run:
ALTER USER 'username'@'host' IDENTIFIED BY 'NewSecureP@ss!';

โœ… Ideal for forgotten passwords or force resets by DBAs


๐Ÿ”‘ 5. Password Expiration & Policy (MySQL 8+)

๐Ÿ”น Force Password Expiry

ALTER USER 'username'@'host' PASSWORD EXPIRE;

Effect:
Prompts user to change password upon next login.


๐Ÿ”น Set Password Lifetime (In Days)

ALTER USER 'username'@'host' PASSWORD EXPIRE INTERVAL 60 DAY;

โœ… Enforces password rotation policies.


๐Ÿ“˜ Password Change Summary Table

MethodUse CaseVersion
ALTER USERGeneral password updateMySQL 5.7.6+ / 8
SET PASSWORDLegacy or same-user changeAll versions
PASSWORD EXPIREForce user to reset passwordMySQL 8+
RENAME USERUse to rename (not change password)All versions

๐Ÿ“‹ Best Practices

โœ… Tip๐Ÿ’ก Why It Matters
Always use ALTER USER in modern MySQLMore secure and future-proof
Use strong, complex passwordsProtects against brute-force and credential reuse
Set password expiration for sensitive accountsEnforces regular credential rotation
Restrict password changes to root or DBAEnsures secure account management
Rotate passwords regularly in productionReduces risk of long-term exposure

๐Ÿš€ Real-World Use Cases

ScenarioCommand Used
Reset app password on deployALTER USER 'api_user'@'localhost' IDENTIFIED BY 'N3wApp!Pass'
Expire password for contractorsALTER USER 'vendor'@'%' PASSWORD EXPIRE;
Password rotation for auditorsALTER USER 'audit'@'%' PASSWORD EXPIRE INTERVAL 90 DAY;
Reset admin account after breachALTER USER 'admin'@'localhost' IDENTIFIED BY 'SecReT123!';

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Changing MySQL user passwords is a fundamental security task. MySQL provides modern, secure methods (ALTER USER) to update passwords while supporting legacy commands for backward compatibility.

๐Ÿ” Key Takeaways

  • Use ALTER USER for secure password changes (MySQL 5.7.6+)
  • Use SET PASSWORD for legacy or in-session updates
  • Apply password expiration policies for sensitive users
  • Only privileged users should change othersโ€™ passwords
  • Strong passwords and regular rotation reduce risks

โš™๏ธ Real-World Relevance

Password management is critical for DevOps, database administration, multi-tenant apps, and security compliance (SOC2, HIPAA, PCI-DSS).


โ“ FAQ โ€“ Changing MySQL Passwords


โ“ What is the recommended way to change a password?

โœ… Use:

ALTER USER 'username'@'host' IDENTIFIED BY 'NewP@ssword!';

โ“ How do I change my own password?

SET PASSWORD = 'NewMyP@ss!';

(Only works if you’re logged in as that user.)


โ“ Can I force users to change their passwords?

โœ… Yes:

ALTER USER 'user'@'host' PASSWORD EXPIRE;

โ“ Is it safe to store passwords in SQL queries?

โŒ No. Use secure, encrypted connection tools or management scripts.


โ“ Can I see a userโ€™s password in plain text?

โŒ No. MySQL stores hashed passwordsโ€”you cannot retrieve them.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”‘ MySQL Change User Password

Or Copy Link

CONTENTS
Scroll to Top