The Command
Log in as an administrative account. On Ubuntu and Debian the MySQL root account usually authenticates through the system root user, so no password is needed:
sudo mysql
Otherwise use mysql -u root -p, which asks for the password instead of taking it on the command line. Then change the password:
ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
ALTER USER updates the grant tables in memory as well, so FLUSH PRIVILEGES is not needed.
Find the Right Host Part
A MySQL account is a user name plus a host. 'app_user'@'localhost' and 'app_user'@'%' are two separate accounts with separate passwords. List them first:
SELECT user, host, plugin FROM mysql.user WHERE user = 'app_user';
Change the password of the account your application actually connects with. An application on the same server usually connects as localhost; one on another server matches % or a narrower host pattern. More on accounts is in MySQL list users.
Keep the Password off the Command Line
- Do not use
-pSTRONG_PASSWORD: a password typed after-pappears in your shell history and can briefly show in the process list. - Prefer the interactive prompt: type
ALTER USERinside the mysql client. The MySQL client leaves lines that contain IDENTIFIED or PASSWORD out of its history file by default. The MariaDB client has no such filter, so on MariaDB check~/.mysql_historyafterward. - Let the server generate one: on MySQL 8.0.18 and later,
IDENTIFIED BY RANDOM PASSWORDsets a random password and prints it once.
ALTER USER 'app_user'@'localhost' IDENTIFIED BY RANDOM PASSWORD;
Change Your Own Password
Any user can change their own password without naming the account:
ALTER USER USER() IDENTIFIED BY 'STRONG_PASSWORD';
If the account or the server requires the current password for such changes, add it with REPLACE 'CURRENT_PASSWORD' after the new one. This is a MySQL clause; on MariaDB write CURRENT_USER() in place of USER() and leave it out.
The Authentication Plugin
MySQL 8.0 and later use caching_sha2_password by default. The plugin column in the query above shows which one an account uses. To change the password and the plugin in one statement:
ALTER USER 'app_user'@'localhost'
IDENTIFIED WITH caching_sha2_password BY 'STRONG_PASSWORD';
Do not switch accounts to the older native password plugin: it is disabled by default in MySQL 8.4 and removed in 9.0. If an old client or driver cannot connect with caching_sha2_password, update the client library instead.
MariaDB Differences
- Same statement:
ALTER USER … IDENTIFIED BYworks on current MariaDB releases. - Different plugins: MariaDB accounts normally use
mysql_native_password. Acaching_sha2_passwordplugin exists only in recent releases (11.4.9, 11.8.4, 12.1 and later), is not loaded by default, and is meant for moving accounts from MySQL. The root account usesunix_socket, sosudo mysqlworks without a password. - No random or dual passwords:
RANDOM PASSWORDandRETAIN CURRENT PASSWORDare MySQL features.
See MariaDB vs MySQL for other differences.
Rotate Without Downtime
On MySQL 8.0.14 and later, an account can keep its old password for a while. It cannot be combined with a plugin change in the same statement:
ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD' RETAIN CURRENT PASSWORD;
Both passwords work until you update every application and then run ALTER USER 'app_user'@'localhost' DISCARD OLD PASSWORD;.
Update the Application
Put the new password in the application's configuration, such as an .env file or wp-config.php, and restart the application or PHP-FPM if it caches the connection settings. Then check its log. "Access denied" after a change usually means the wrong host part or an old password left in a config file; see MySQL access denied for user. If you have lost the root password itself, follow how to reset the MySQL root password, and to create a new account instead, see MySQL create user.