MySQL Unlock Account

Unlock is a mechanism that allows the user to release all locks or any specific lock associated with the account. In this article, we will learn how to unlock the user accounts in the MySQL server.

When the CREATE USER… UNLOCK statement creates a new user account, the new user stored as a locked state.

If we want to release a lock from the existing user account, we need to use the ALTER USER… ACCOUNT UNLOCK statement as follows:

In this syntax, we have to first specify the user account name that we want to release a lock after the ALTER USER keyword. Next, we need to provide the ACCOUNT UNLOCK clause next to the user name. It is to note the IF EXISTS option can also be used to unlock the account only if it has existed in the server.

MySQL also allows us to unlock multiple user accounts at the same time by using the below statement:

In this syntax, we need to provide a list of comma-separated user name for unlocking multiple accounts within a single query. If we do not specify the ACCOUNT UNLOCK clause with the statement, the account unlocking state remains unchanged.

MySQL uses the account_locked column of the mysql.user system table to store the account locking state. We can use the SHOW CREATE USER statement to validate whether the account is unlocked or locked. If this column value is Y, it means the account is locked. If it contains N, it means the account is unlocked.

If we will try to connect to the account without unlocking, MySQL issues an error that writes the below message to the error log:

MySQL User Account Unlocking Examples

Let us understand how to unlock the user accounts through examples. First, we will create a new user account named javatpoint@localhost in the locked state using the below statement:

Next, we will execute the below statement to show the user account and its status:

We should get the below output:

MySQL Unlock Account

In this output, we can see that the account_locked column in the mysql.user system table indicates Y. It means the username javatpoint is locked on the server.

If we try to connect with this account without unlocking in the MySQL Server, it returns the following error:

Here is the error message:

MySQL Unlock Account

Thus, we can use the ALTER USER statement to unlock the account before accessing it as follows:

In the below output, we can see that the account_locked column status is N. It means the user account javatpoint does not have any lock.

MySQL Unlock Account

In this article, we have learned how we can use the ALTER TABLE ACCOUNT UNLOCK statement to release a lock from an existing user account.


Next TopicMySQL Queries




Latest Courses