brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: rotate a MySQL password without downtime using a second one

Mysql
~/tips/tip-mysql-retain-current-password

ALTER USER 'appuser'@'192.0.2.10'

Lets old and new passwords both work while applications are updated.

Changing a database password usually means a race: the moment you change it, every application still holding the old one breaks. MySQL lets an account have two for a while.

Set the new password and keep the current one as a fallback:

ALTER USER 'appuser'@'192.0.2.10'
  IDENTIFIED BY 'new-long-password'
  RETAIN CURRENT PASSWORD;

Both now work. Update your applications one at a time and restart them at your own pace. When all of them use the new one, drop the old:

ALTER USER 'appuser'@'192.0.2.10' DISCARD OLD PASSWORD;

That last step is the one I’d put on a calendar. Until you run it, the old password is as good as ever, so a rotation after a leak isn’t finished.

A few rules apply. There is only one secondary slot, so running RETAIN CURRENT PASSWORD twice in a row throws away the original and keeps your first new one. Changing the authentication plugin discards the secondary. An empty new password empties both.

Permissions matter too. Changing your own account this way needs APPLICATION_PASSWORD_ADMIN, and doing it for other accounts needs CREATE USER. It is MySQL syntax, so don’t expect it elsewhere. Also, type the statement in a session whose history you trust, because it contains the password in plain text, even though the server masks it in its logs.

Further reading: Password management: dual passwords

More Mysql tips FOR UPDATE SKIP LOCKED; mysql_config_editor set --login-path=shop \ SET PERSIST max_connections = 300; ALTER TABLE orders ADD COLUMN note VARCHAR(100), ALGORITHM=INSTANT; SELECT * FROM sys.schema_unused_indexes All Mysql tips (8) All topics

Link