# 列出用户

本教程将向您展示如何在MySQL数据库中列出用户。

## MySQL显示用户：列出所有用户

要印出所有MySQL中的所有用户，您是不是想使用MySQL `SHOW USERS`命令？ 不幸的是，MySQL没有类似[SHOW DATABASES](http://www.yiibai.com/mysql/show-databases.html)，[SHOW TABLES](http://www.yiibai.com/mysql/show-tables.html)等那样的`SHOW USERS`命令，因此列出MySQL数据库服务器中的所有用户，可使用以下查询：

```sql
SELECT 
   user 
FROM 
   mysql.user;
```

在此语句中，我们从`mysql`数据库的`user`表查询用户数据信息。

要执行此查询，您必须以管理员身份登录到MySQL数据库服务器。

```sql
C:\Users\Administrator> mysql -u root -p
Enter password: ***********
mysql> use mysql;
Database changed
mysql> SELECT user FROM user;
```

以下显示了以上查询的输出：

```sql
mysql> USE mysql
Database changed
mysql> SELECT user FROM user;
+------------+
| user       |
+------------+
| rfc        |
| superadmin |
| mysql.sys  |
| root       |
| super      |
+------------+
5 rows in set (0.27 sec)
```

如上所见，在本地数据库中有五个用户。

要获取有关用户表的更多信息，可以使用以下命令预览其列：

```sql
DESC user;
```

例如，要显示用户和其他信息(如主机，帐户锁定和密码到期状态)，请使用以下查询：

```sql
SELECT 
    user, 
    host, 
    account_locked, 
    password_expired
FROM
    mysql.user;
```

执行上面查询语句，得到以下结果 -

```sql
mysql> SELECT 
    user, 
    host, 
    account_locked, 
    password_expired
FROM
    mysql.user;
+------------+-----------+----------------+------------------+
| user       | host      | account_locked | password_expired |
+------------+-----------+----------------+------------------+
| root       | localhost | N              | N                |
| mysql.sys  | localhost | Y              | N                |
| superadmin | %         | N              | N                |
| super      | localhost | N              | N                |
| rfc        | %         | N              | N                |
+------------+-----------+----------------+------------------+
5 rows in set
```

## 显示当前用户

要获取有关当前用户的信息，请使用`user()`函数，如以下语句所示：

```sql
mysql> SELECT user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set
```

或者使用`current_user()`函数：

```sql
mysql>  SELECT current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set
```

在本示例的情况下，当前用户是：`root@localhost`。

## 显示当前登录的用户

要列出当前登录MySQL数据库服务器的所有用户，请执行以下语句：

```sql
SELECT 
    user, 
    host, 
    db, 
    command 
FROM 
    information_schema.processlist;
```

执行上面语句，得到以下结果

```sql
mysql> SELECT 
    user, 
    host, 
    db, 
    command 
FROM 
    information_schema.processlist;
+------+-----------------+----------+---------+
| user | host            | db       | command |
+------+-----------------+----------+---------+
| root | localhost:51897 | NULL     | Sleep   |
| root | localhost:51898 | yiibaidb | Sleep   |
| root | localhost:55313 | NULL     | Query   |
| root | localhost:52939 | mysql    | Sleep   |
+------+-----------------+----------+---------+
4 rows in set
```

正如使用可以看到的，目前有四个用户登录在MySQL数据库中，一个是执行“查询”状态，其它三个是“睡眠”状态。

在本教程中，您已经学习了如何通过查询`mysql`数据库中的用户表中的数据来列出MySQL数据库服务器中的所有用户。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-ba-zhang/lie-chu-yong-hu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
