MySQL - 解锁用户帐户



MySQL 中的帐户锁定和解锁是为了提高数据库安全性,防止未经授权的事务或可疑活动。

MySQL 解锁用户帐户

要检查帐户是否已解锁,MySQL 在 `mysql.user` 表中提供了 `account_locked` 属性,该属性分别保存 `Y` 或 `N` 值。如果该属性保存 `N` 值,则表示该帐户处于解锁模式。

默认情况下,在 MySQL 中创建的所有新用户帐户都是解锁的。

解锁新帐户

可以使用 `CREATE USER... ACCOUNT UNLOCK` 语句解锁在 MySQL 中创建的新帐户。默认情况下,新创建的帐户始终处于解锁状态,除非另有说明。但是,`ACCOUNT UNLOCK` 子句主要用于帐户处于锁定状态时。

语法

以下是 `CREATE USER... ACCOUNT UNLOCK` 语句的语法:

CREATE USER username@hostname IDENTIFIED BY 'new_password' ACCOUNT UNLOCK;

示例

在下面的查询中,我们使用 `CREATE USER` 语句在 MySQL 中创建一个新的用户帐户:

CREATE USER testuser@localhost IDENTIFIED BY 'qwerty' ACCOUNT UNLOCK;

输出

以下是上述代码的输出:

Query OK, 0 rows affected (0.02 sec)

验证

我们可以使用以下 `SELECT` 语句验证 `testuser` 的帐户是否已解锁:

SELECT User, Host, account_locked FROM mysql.user WHERE User = 'testuser';

上述代码的输出如下所示:

用户 主机 account_locked
testuser localhost N

示例

正如我们上面所了解到的,新创建的用户帐户默认情况下是解锁的。请看下面的例子:

CREATE USER demo@localhost IDENTIFIED BY '000000';

输出

产生的结果如下:

Query OK, 0 rows affected (0.02 sec)

验证

我们可以使用以下 `SELECT` 语句验证新创建的帐户是否默认解锁:

SELECT User, Host, account_locked FROM mysql.user WHERE User = 'demo';

获得的输出如下:

用户 主机 account_locked
demo localhost N

Learn MySQL in-depth with real-world projects through our MySQL certification course. Enroll and become a certified expert to boost your career.

解锁现有帐户

我们可以使用 `ALTER USER... ACCOUNT UNLOCK` 语句解锁之前已锁定的 MySQL 现有帐户。

语法

以下是 `ALTER USER... ACCOUNT UNLOCK` 语句的语法:

ALTER USER username@hostname ACCOUNT UNLOCK;

示例

我们首先检索现有帐户“sample”的信息,包括其用户名、主机及其帐户锁定的状态:

SELECT User, Host, account_locked FROM mysql.user WHERE User = 'sample';

我们可以在下面的输出中看到用户帐户已锁定:

用户 主机 account_locked
test localhost Y

现在,我们将使用 `ALTER USER` 语句解锁现有帐户“sample”:

ALTER USER sample@localhost ACCOUNT UNLOCK;

输出

以下是上述查询的输出:

Query OK, 0 rows affected (0.00 sec)

验证

我们可以使用以下 `SELECT` 查询验证帐户是否已解锁:

SELECT User, Host, account_locked FROM mysql.user WHERE User = 'sample';

正如我们在下面的输出中看到的,`sample@localhost` 帐户现在已解锁,并且可以根据其权限进行访问:

用户 主机 account_locked
sample localhost N

使用客户端程序解锁用户帐户

现在,在本节中,让我们讨论如何使用各种客户端程序解锁 MySQL 用户。

语法

以下是语法:

以下是使用 PHP 解锁 MySQL 用户帐户的语法:

$sql = "ALTER USER user_name ACCOUNT UNLOCK"; $mysqli->query($sql);

以下是使用 JavaScript 解锁 MySQL 用户帐户的语法:

sql= "CREATE USER username@hostname IDENTIFIED BY 'new_password' ACCOUNT UNLOCK"; con.query(sql, function (err, result) { if (err) throw err; console.log(result); });

以下是使用 Java 解锁 MySQL 用户帐户的语法:

String sql = "ALTER USER USER_NAME@LOCALHOST ACCOUNT UNLOCK"; statement.execute(sql);

以下是使用 Python 解锁 MySQL 用户帐户的语法:

sql = f"ALTER USER '{username_to_unlock}'@'localhost' ACCOUNT UNLOCK"; cursorObj.execute(sql);

示例

以下是各种编程语言中解锁用户的程序:

$dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass); if($mysqli->connect_errno ) { printf("Connect failed: %s", $mysqli->connect_error); exit(); } //printf('Connected successfully.'); $sql = "ALTER USER Sarika ACCOUNT UNLOCK"; if($mysqli->query($sql)){ printf("User has been unlocked successfully..!"); } if($mysqli->error){ printf("Failed..!" , $mysqli->error); } $mysqli->close();

输出

获得的输出如下:

User has been unlocked successfully..!
var mysql = require('mysql2'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "Nr5a0204@123" }); //Connecting to MySQL con.connect(function (err) { if (err) throw err; console.log("Connected!"); console.log("--------------------------"); sql = "CREATE USER testuser@localhost IDENTIFIED BY 'qwerty' ACCOUNT UNLOCK;" con.query(sql); sql = "SELECT User, Host, account_locked FROM mysql.user WHERE User = 'testuser';"; con.query(sql, function(err, result){ if (err) throw err; console.log(result); }); });

输出

产生的输出如下所示:

Connected!
--------------------------
[ { User: 'testuser', Host: 'localhost', account_locked: 'N' } ]
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class UnlockUserAccount { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/TUTORIALS"; String user = "root"; String password = "password"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection(url, user, password); Statement st = con.createStatement(); //System.out.println("Database connected successfully...!"); String sql = "ALTER USER Vivek@localhost ACCOUNT UNLOCK"; st.execute(sql); System.out.println("User 'Vivek' account unlocked successfully...!"); }catch(Exception e) { e.printStackTrace(); } } }

输出

获得的输出如下所示:

User 'Vivek' account unlocked successfully...!
import mysql.connector # creating the connection object connection = mysql.connector.connect( host='localhost', user='root', password='password' ) username_to_unlock = 'newUser' # Create a cursor object for the connection cursorObj = connection.cursor() cursorObj.execute(f"ALTER USER '{username_to_unlock}'@'localhost' ACCOUNT UNLOCK") print(f"User '{username_to_unlock}' account is unlocked successfully.") cursorObj.close() connection.close()

输出

以下是上述代码的输出:

User 'newUser' account is unlocked successfully.
广告