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: lock a MariaDB account for a while instead of dropping it

Mariadb
~/tips/tip-mariadb-account-lock

ALTER USER 'app'@'localhost' ACCOUNT LOCK;

Blocks logins for a user while keeping its password and grants.

You suspect an account is no longer used, but you’re not sure enough to delete it. Locking answers the question without losing anything.

One statement closes the account:

ALTER USER 'app'@'localhost' ACCOUNT LOCK;

New logins now fail with ERROR 4151: Access denied, this account is locked. The grants and password stay exactly as they were, and undoing it takes a second:

ALTER USER 'app'@'localhost' ACCOUNT UNLOCK;

To see the state of an account, SHOW CREATE USER 'app'@'localhost'; ends with ACCOUNT LOCK when it is locked.

My approach to an account nobody claims: lock it, wait a few weeks, and see who complains. Silence means it can go, with a note of its grants first. A complaint means a one line fix and a new entry in your documentation.

The lock stops new connections only. Sessions that are already open carry on, so for a leaked password, also find them with SHOW PROCESSLIST and end each with KILL and its ID. Applications with connection pools can hang on to a session for a long time, which can make a locked account seem perfectly healthy for hours.

Remember too that MariaDB treats 'app'@'localhost' and 'app'@'%' as separate accounts. Locking one leaves the other wide open, so check mysql.user for every host entry with that name.

Further reading: ALTER USER documentation, Account locking

More Mariadb tips ALTER TABLE t ADD COLUMN z INT INVISIBLE; CREATE SEQUENCE s START WITH 100 INCREMENT BY 10; CREATE OR REPLACE TABLE scratch ( mariadb --init-command="SET SESSION sql_mode='STRICT_ALL_TABLES'" mydb mariadb-admin -u root -p extended-status --relative --sleep=10 \ All Mariadb tips (15) All topics

Link