Last updated

Homelab

MySQL: Change or Reset a User's Password

Rotate an app's database password, change your own, or get back in when the root password is lost. All with current MySQL 8.0 commands.

Related guide: Full MySQL users and permissions guide

1. Change another user's password

Run as an admin (sudo mysql). Update the app's config right after.

ALTER USER 'appuser'@'localhost' IDENTIFIED BY 'new-long-random-password';

2. Change your own password

ALTER USER USER() IDENTIFIED BY 'new-long-random-password';

3. Lost MySQL root on Ubuntu?

On Ubuntu, root uses auth_socket, so the Linux root user can usually still get in with no password.

sudo mysql

4. Force a password change later

Expire a password so the user must pick a new one at next login.

ALTER USER 'appuser'@'localhost' PASSWORD EXPIRE;

5. Verify

Log in with the new password.

mysql -u appuser -p -e 'SELECT CURRENT_USER();'

6. Troubleshooting

The app breaks after a change: its config still has the old password. Weak password rejected: the validate_password component is on; use a longer, mixed password.

SHOW VARIABLES LIKE 'validate_password%';

FAQ

Do I need FLUSH PRIVILEGES after ALTER USER?

No. ALTER USER takes effect immediately.

How do I generate a strong password?

Use your password manager or run openssl rand -base64 24.

Further reading: DigitalOcean: create a new user and grant permissions in MySQL