MySQL User Password Change
Passwords need changing: on a schedule, when someone leaves the team, or after a suspected leak. MySQL changes them with ALTER USER (or SET PASSWORD), and adds features that make it safe: dual passwords for zero-downtime rotation, random passwords, expiry policies, and a recovery procedure for a forgotten root password.
1. ALTER USER … IDENTIFIED BY
ALTER USER 'user'@'host' IDENTIFIED BY 'new_password';
ALTER USER 'priya'@'localhost' IDENTIFIED BY 'Pr1ya#New2026';
SELECT user, DATE(password_last_changed) = CURDATE() AS changed_today FROM mysql.user WHERE user = 'priya';
Result: 1 row
| user | changed_today |
|---|---|
| priya | 1 |
The new password works immediately; the old one is rejected:
SELECT CURRENT_USER() AS logged_in_as;
Result: 1 row
| logged_in_as |
|---|
| priya@localhost |
CREATE USER privilege (or UPDATE on the mysql schema) to change other accounts' passwords.2. Changing Your Own Password
Any user can change their own password. MySQL can require the current password first (PASSWORD REQUIRE CURRENT):
ALTER USER 'priya'@'localhost' PASSWORD REQUIRE CURRENT;
Query OK, 0 rows affected
ALTER USER USER() IDENTIFIED BY 'Pr1ya#Third26';
ERROR 3892 (HY000): Current password needs to be specified in the REPLACE clause in order to change it.
ALTER USER USER() IDENTIFIED BY 'Pr1ya#Third26' REPLACE 'Pr1ya#New2026';
Query OK, 0 rows affected
SET PASSWORD = 'new' REPLACE 'current'; does the same.
3. Random Passwords
Let MySQL generate a strong password and show it once:
ALTER USER 'priya'@'localhost' IDENTIFIED BY RANDOM PASSWORD;
Result: 1 row
| user | host |
|---|---|
| priya | localhost |
4. Zero-Downtime Rotation with Dual Passwords
ALTER USER 'stock_app'@'localhost' IDENTIFIED BY 'St0ck#New2026' RETAIN CURRENT PASSWORD;
Query OK, 0 rows affected
SELECT 'old password still works' AS status;
Result: 1 row
| status |
|---|
| old password still works |
SELECT 'new password works' AS status;
Result: 1 row
| status |
|---|
| new password works |
After every application server uses the new password:
ALTER USER 'stock_app'@'localhost' DISCARD OLD PASSWORD;
Query OK, 0 rows affected
5. Password Expiry Policies
ALTER USER 'priya'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
ALTER USER 'stock_app'@'localhost' PASSWORD EXPIRE NEVER;
SELECT user, password_lifetime FROM mysql.user WHERE user IN ('priya', 'stock_app');
Result: 2 rows
| user | password_lifetime |
|---|---|
| priya | 90 |
| stock_app | 0 |
| Option | Effect |
|---|---|
PASSWORD EXPIRE | Expire now: user must set a new password at next login |
PASSWORD EXPIRE INTERVAL n DAY | Expire every n days |
PASSWORD EXPIRE NEVER | Never expire (common for service accounts using rotation) |
PASSWORD EXPIRE DEFAULT | Follow the global default_password_lifetime |
PASSWORD HISTORY n / PASSWORD REUSE INTERVAL n DAY | Prevent reusing recent passwords |
6. From the Command Line
# change your own password interactively
mysqladmin -u priya -p password
# MySQL Shell
mysqlsh priya@localhost --sql -e "ALTER USER USER() IDENTIFIED BY 'New#Pass2026' REPLACE 'Old#Pass2026'"
7. Resetting a Forgotten root Password
If nobody knows the root password, restart MySQL with a one-time init file:
- Stop the MySQL service (
sudo systemctl stop mysqldornet stop MySQL84). - Create a text file, for example
/tmp/reset.sql, containingALTER USER 'root'@'localhost' IDENTIFIED BY 'N3w#Root2026'; - Start the server once with
mysqld --init-file=/tmp/reset.sql(as the mysql OS user). - Log in with the new password, then stop the server and start it normally as a service.
- Delete the init file.
--skip-grant-tables method also works but leaves the server open to anyone while it runs; prefer --init-file.8. Good Practice
- Use long, random passwords and store them in a secrets manager.
- Rotate service passwords with RETAIN CURRENT / DISCARD OLD.
- Enable the
validate_passwordcomponent to enforce strength. - Never put passwords in shell history or scripts committed to Git; use option files with strict permissions (
~/.my.cnf, mode 600) ormysql_config_editor.
9. Summary
ALTER USER 'u'@'h' IDENTIFIED BY 'new';changes a password; users can change their own withALTER USER USER() … REPLACE 'current'.IDENTIFIED BY RANDOM PASSWORDgenerates a strong password.- Dual passwords allow rotation with no downtime.
- Expiry, history and reuse policies enforce good habits; reset root with --init-file.