Mysql 简明教程

MySQL - Show Users

您可能已经知道,MySQL 是一个多用户数据库,允许多个用户同时对其进行处理。但是您是否曾经想过这些用户是谁?

As you might have already known, MySQL is a multi-user database that allows multiple users to work on it simultaneously. But have you ever wondered who these users might be?

MySQL 为每个用户提供一个帐户,该帐户通过用户名和密码进行身份验证。而这些帐户的详细信息存储在数据库的“user”表中。此表包含一些详细信息,例如用户名、此用户连接到的主机,以及所述用户拥有的其他权限等。

MySQL provides an account to each user that is authenticated with a username and a password. And details of these accounts are stored in the "user" table in the database. This table contains details like username, the host this user is connected from, and other privileges the said user has etc.

The MySQL SHOW Users

MySQL 不提供任何直接命令来显示(列出)所有用户。但是,这些用户帐户的详细信息存储在数据库中的“user”表中。因此,我们可以使用 SELECT 语句列出此表的內容。

MySQL does not provide any direct command to show (list out) all the users. However, the details of these user accounts is stored in the "user" table within the database. Hence, we can use the SELECT statement to list out the contents of this table.

Syntax

以下是显示 MySQL 数据库中用户的语法 −

Following is the syntax to show users in a MySQL database −

SELECT * FROM mysql.user;

Example

要查看此“user”表的结构,请将以下查询与 DESC 命令一起使用 −

To see the structure of this "user" table, use the following query with the DESC command −

DESC mysql.user;

现在,在此示例中,我们正在列出系统本地 MySQL 数据库中的所有用户 −

Now, in this example, we are listing out all the users in the MySQL database local to a system −

SELECT Host, User, User_attributes, account_locked
FROM mysql.user;

Output

获得的输出如下所示 −

The output obtained is as shown below −

实际的用户表包含比本章显示的更多的列/字段。但是,为了简单起见,此处只显示了一些信息。

The actual user table contains a lot more columns/fields than what is displayed in this chapter. Here, however, only some information is displayed for simplicity.

请注意,这些用户的列表是系统本地的。因此,并非所有系统都会给出相同输出(默认用户除外)。

Note that list of these users are local to a system. Hence, not all systems would give the same output (apart from the default users).

Show Current User

不仅是所有用户的列表,MySQL 还可以查看当前用户。这是在 user() 或 current_user() 函数的帮助下完成的。

Not only the list of all users, MySQL also has a provision to see the current user. This is done with the help of user() or current_user() functions.

Syntax

以下是显示当前用户的语法 −

Following is the syntax to show the current user −

SELECT user();
or
SELECT current_user();

Example

使用以下查询,让我们使用 user() 函数在 MySQL 数据库中显示当前登录用户的用户名 −

Using the following query, let us display the username of the currently logged in user in MySQL database using the user() function −

SELECT user();

Output

以下是所获得的输出 −

Following is the output obtained −

Example

在这里,我们正在使用 current_user() 函数来显示当前用户 −

In here, we are using the current_user() function to show the current user −

SELECT current_user();

Output

获得的输出如下 −

The output obtained is as follows −

Show Currently Logged in Users

当前用户和当前登录用户之间的区别在于,当前用户是正在执行查询的用户;而当前登录用户列表包括所有目前连接到 MySQL 服务器的活动用户。

The difference between current users and currently logged in users is that, current user is the user that is executing the queries; whereas, currently logged in user list includes all the active users that are connected to the MySQL server at the moment.

可以使用 SELECT 语句从“information_schema.processlist”表中提取此信息。

This information can be extracted from the "information_schema.processlist" table using the SELECT statement.

Example

在以下查询中,我们正在检索所有当前已登录用户的信息 -

In the following query, we are retrieving the information of all the currently logged in users −

DESC information_schema.processlist;

Output

以下是上面代码的输出: -

Following is the output of the above code −

Example

在此处,我们正在从信息模式中检索当前用户、主机、数据库和命令的信息 -

In here, we are retrieving information of current users, host, database, and command from the information_schema −

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

Output

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

Show Users Using a Client Program

我们还可以使用客户端程序来显示有关 MySQL 用户的信息。

We can also display information about the MySQL users using a client program.

Syntax

以下是使用各种编程语言显示有关 MySQL 用户的信息的语法 -

Following are the syntaxes to display information regarding MySQL users in various programming languages −

Example

以下是这些程序 −

Following are the programs −