Mysql 简明教程

MySQL - Drop User

删除 MySQL 中的用户将取消用户对特定数据库的访问和权限。这由数据库管理员执行,以维护安全性和控制谁可以与数据库系统交互,确保只有经过授权的用户才能访问和操作数据。

Dropping users in MySQL will remove a user’s access and permissions on a specific database. This is performed by database administrators to maintain security and control over who can interact with the database system, ensuring that only authorized users can access and manipulate the data.

The MySQL Drop User Statement

你可以使用 DROP USER 语句在 MySQL 中删除一个或多个现有用户。一旦你删除了一个帐户,它的所有权限都将被删除。要执行此语句,你需要有 CREATE USER 权限。

You can drop/delete one or more existing users in MySQL using the DROP USER Statement. Once you delete an account, all privileges of it are deleted. To execute this statement, you need to have CREATE USER privilege.

Syntax

以下是 DROP USER 语句的语法 −

Following is the syntax of the DROP USER statement −

DROP USER [IF EXISTS] 'username'@'hostname';

其中, user_name 是你需要删除的 MySQL 用户的名称。

Where, user_name is the name of the MySQL user you need to delete.

Example

假设我们已经创建了一个名为 'TestUser' 的 MySQL 用户帐户,如下所示 −

Suppose, we have created a MySQL user account named 'TestUser' as shown below −

CREATE USER TestUser@localhost IDENTIFIED BY 'password1';

以下是所获得的输出 −

Following is the output obtained −

Query OK, 0 rows affected (0.04 sec)

你可以使用以下查询验证用户列表 −

You can verify the list of users using the following query −

SELECT user FROM MySQl.user;

表将显示如下 -

The table will be displayed as shown below −

现在,让我们使用 DROP USER 语句删除上面创建的 'TestUser' 帐户,如下所示 −

Now, let us delete the 'TestUser' account created above using the DROP USER statement as shown below −

DROP USER TestUser@localhost;

执行上述代码后,我们能看到如下所示的输出 −

After executing the above code, we can see the output as shown below −

Query OK, 0 rows affected (0.02 sec)

Verification

一旦删除一个表,如果你使用 SELECT 语句验证下面所示的用户列表,你将发现它的名称从列表中消失了 −

Once a table is dropped, if you verify the list of the users as shown below using the SELECT statement, you will find that its name is missing from the list −

SELECT user FROM MySQl.user;

获得的表如下 −

The table obtained is as follows −

Removing Multiple Users

你还可以使用 DROP ROLE 语句一次删除多个用户。角色用于管理数据库系统中的权限和访问控制。通过删除角色,你可以撤销与该角色关联的所有权限。−

You can also delete multiple users at once using the DROP ROLE statement. Roles are used to manage permissions and access control in a database system. By dropping a role, you revoke all privileges associated with that role. −

Example

让我们首先创建两个角色“MyAdmin”和“MyDeveloper” −

Let us start by creating two roles 'MyAdmin' and 'MyDeveloper' −

CREATE ROLE 'MyAdmin', 'MyDeveloper';

获得的输出如下 −

The output obtained is as follows −

Query OK, 0 rows affected (0.01 sec)

现在,让我们使用 DROP ROLE 语句删除这些角色 −

Now, let us remove these roles using the DROP ROLE statement −

DROP ROLE 'MyAdmin', 'MyDeveloper';

这个查询将有效地从数据库中删除这两个角色 −

This query will effectively delete both roles from the database −

Query OK, 0 rows affected (0.01 sec)

The IF EXISTS clause

如果你尝试删除一个不存在的 MySQL 用户,将生成一个错误。为了解决这个问题,MySQL 提供了 IF EXISTS 子句,它可用于 DROP USER 语句。

If you try to drop a MySQL user that doesn’t exist, an error will be generated. To address this issue, MySQL provides the IF EXISTS clause, which can be used with the DROP USER statement.

因此,IF EXISTS 子句允许你在用户存在的情况下删除它,并且它处理数据库中找不到指定用户的情况。

Hence, the IF EXISTS clause allows you to drop a user if they exist, and it handles situations where the specified user is not found in the database.

Example

在下面的查询中,我们尝试删除“demo”用户。但是,它会导致一个错误,因为用户不存在数据库中 −

In the below query, we are attempting to drop the 'demo' user. However, it results in an error because the user doesn’t exist in the database −

DROP USER demo@localhost;

下面显示了产生的输出:

The output produced is as shown below −

ERROR 1396 (HY000): Operation DROP USER failed for 'demo'@'localhost'

如果你像下面所示使用 IF EXISTS 子句和 DROP USER 语句,将删除指定用户,并且如果具有给定名称的用户不存在,将忽略该查询 −

If you use the IF EXISTS clause along with the DROP USER statement as shown below, the specified user will be dropped and if a user with the given name doesn’t exist, the query will be ignored −

DROP USER IF EXISTS demo;

获得的输出如下 −

The output obtained is as follows −

Query OK, 0 rows affected, 1 warning (0.01 sec)

Dropping User Using a Client Program

在本节中,我们将看到各种客户端程序,以从 MySQL 中删除现有用户。

In this section we are going to see various client programs to drop an existing user from MySQL.

Syntax

以下是使用各种编程语言删除 MySQL 用户的语法 −

Following are the syntaxes to drop a MySQL user in various programming languages −

Example

以下是 MySQL 中删除用户的客户端程序 −

Following are the client programs to drop an user in MySQL −