MySQL Security – User Account Locking

March 14, 2018

When thinking about security within a MySQL installation, you should consider a wide range of possible procedures / best practices and how they affect the security of your MySQL server and related applications. MySQL provides many tools / features / plugins in order to protect your data including some advanced features like Transparent Data Encryption aka TDEAudit, Firewall, Password Management, Password Validation Plugin, etc…

MySQL Security

For security reasons some context require you to setup a user account locking policy. Thus an unauthorized user is not able (anymore) to login to the MySQL server. In this 3rd article of the MySQL 5.7 Security series, we will see how to [un]lock a user account.


User Account Locking

MySQL supports locking and unlocking user accounts using the ACCOUNT LOCK and ACCOUNT UNLOCK clauses for the CREATE USER and ALTER USER statements:

  • When used with CREATE USER, these clauses specify the initial locking state for a new account. In the absence of either clause, the account is created in an unlocked state.
  • When used with ALTER USER, these clauses specify the new locking state for an existing account. In the absence of either clause, the account locking state remains unchanged.


So let’s create a user batchman { not this one 🙂 } with all privileges on sakila database:


Account locking state is recorded in the account_locked column of the mysql.user table:

The output from SHOW CREATE USER indicates whether an account is locked or unlocked:

Indeed accounts are unlock by default.

The new user account is allowed to connect to the server and can handle sakila’s objects:


Lock account

What if your security policy requires you to lock this account?

ACCOUNT LOCK clause is the answer :

batchman@localhost is now locked!

Any connection attempt with this user will failed :



If the user is still connected you’ll need to kill the connection.



The server increments the Locked_connects status variable that indicates the number of attempts to connect to a locked account and writes a message to the error log :



In order to go further

MySQL 5.7 Security Series

  1. Password Validation Plugin
  2. Password Management
  3. User Account Locking


Reference Manual


MySQL Security


Thanks for using MySQL!


2 Responses to “MySQL Security – User Account Locking”

  1. […] User Account Locking […]

  2. […] User Account Locking […]

Leave a Reply