Mysql 简明教程
MySQL - Unlock User Account
在 MySQL 中引入账户锁定和解锁,其目的是通过阻止未经授权的交易或可疑活动来提高数据库安全性。
Account Locking and Unlocking in MySQL is introduced to increase security of the database by preventing unauthorized transactions or suspicious activities.
MySQL Unlock User Account
为了检查一个账户是否已解锁,MySQL 在“mysql.user”表中提供了“account_locked”属性,它将分别保存“Y”或“N”值。如果该属性保存“N”值,则该账户被认为处于解锁模式。
To check whether an account is unlocked or not, MySQL provides the 'account_locked' attribute in the 'mysql.user' table that will hold either 'Y' or 'N' values respectively. If the attribute holds the 'N' value, then the account is said to be in the unlock mode.
Unlocking New Accounts
您可以使用 CREATE USER… ACCOUNT UNLOCK 语句解锁在 MySQL 中创建的新账户。默认情况下,除非另有规定,否则新创建的账户始终处于解锁状态。但是,当一个账户处于锁定状态时,ACCOUNT UNLOCK 子句用得最多。
You can use the CREATE USER… ACCOUNT UNLOCK statement to unlock new accounts created in MySQL. By default, the newly created accounts are always unlocked unless specified otherwise. However, the ACCOUNT UNLOCK clause is mostly used when an account is in the locked state.
Syntax
以下是 CREATE USER… ACCOUNT UNLOCK 语句的语法:
Following is the syntax of CREATE USER… ACCOUNT UNLOCK statement −
CREATE USER username@hostname
IDENTIFIED BY 'new_password' ACCOUNT UNLOCK;
Example
在以下查询中,我们使用 CREATE USER 语句在 MySQL 中创建一个新用户账户:
In the following query, we are creating a new user account in MySQL using the CREATE USER statement −
CREATE USER testuser@localhost
IDENTIFIED BY 'qwerty' ACCOUNT UNLOCK;
Output
以下是上面代码的输出: -
Following is the output of the above code −
Query OK, 0 rows affected (0.02 sec)
Verification
我们可以使用以下 SELECT 语句验证“testuser”账户是否已解锁:
We can verify whether the account of the 'testuser' is unlocked or not using the following SELECT statement −
SELECT User, Host, account_locked
FROM mysql.user WHERE User = 'testuser';
以上代码的输出如下所示 −
Output of the above code is as shown below −
Unlocking Existing Accounts
我们可以使用 ALTER USER… ACCOUNT UNLOCK 语句解锁此前在 MySQL 中锁定的现有帐户。
We can use the ALTER USER… ACCOUNT UNLOCK statement unlock existing accounts in MySQL that are locked beforehand.
Syntax
以下是 ALTER USER… ACCOUNT UNLOCK 语句的语法 −
Following is the syntax of ALTER USER… ACCOUNT UNLOCK statement −
ALTER USER username@hostname ACCOUNT UNLOCK;
Example
我们首先检索现有帐户“sample”的信息,包括用户名、主机及其帐户锁定状态 −
We are first retrieving the information of the existing account 'sample', including its username, host, and the status of its account lock −
SELECT User, Host, account_locked
FROM mysql.user WHERE User = 'sample';
我们可以在下面的输出中看到,用户帐户已锁定 −
We can see in the output below that the user account is locked −
现在,我们将使用 ALTER USER 语句解锁现有帐户“sample” −
Now, we will unlock the existing account 'sample' using the ALTER USER statement −
ALTER USER sample@localhost ACCOUNT UNLOCK;
Output
以下是以上查询的输出:
Following is the output of the above query −
Query OK, 0 rows affected (0.00 sec)
Verification
我们可以使用下面的 SELECT 查询,验证帐户是否已解锁 −
We can verify whether the account is unlocked or not using the following SELECT query −
SELECT User, Host, account_locked
FROM mysql.user WHERE User = 'sample';
正如我们在下面的输出中看到,现在“sample@localhost”帐户已解锁,并且可以根据其权限进行访问 −
As we can see in the below output, the 'sample@localhost' account is now unlocked and can be accessed according to its privileges −