SET DEFAULT ROLE

Contents

  1. 语法
  2. 描述
  3. 示例

语法

SET DEFAULT ROLE { role | NONE } [ FOR user@host ]

描述

SET DEFAULT ROLE 语句为指定(或当前)用户设置默认角色。当用户连接时,将自动启用默认角色(连接建立后立即执行隐式的 SET ROLE 语句)。

要能够将角色设置为默认角色,必须已将该角色授予该用户,并且需要有权限启用该角色(如果您不能执行 SET ROLE X,则无法执行 SET DEFAULT ROLE X)。要为另一个用户设置默认角色,需要对 mysql 数据库具有写入访问权限。

要删除用户的默认角色,请使用 SET DEFAULT ROLE NONE [ FOR user@host ]。如果角色被删除撤销,则不会删除默认角色的记录,因此如果随后重新创建或授予该角色,则它将再次成为用户的默认角色。

默认角色存储在 mysql.user 表/视图的 default_role 列中,以及 Information Schema APPLICABLE_ROLES 表 中,因此可以查看这些内容以查看已分配给用户的默认角色。

示例

为当前用户设置默认角色:

SET DEFAULT ROLE journalist;

从当前用户中删除默认角色:

SET DEFAULT ROLE NONE;

为其他用户设置默认角色。在将其设置为默认角色之前,必须已将该角色授予该用户:

CREATE ROLE journalist;
CREATE USER taniel;

SET DEFAULT ROLE journalist FOR taniel;
ERROR 1959 (OP000): Invalid role specification `journalist`

GRANT journalist TO taniel;
SET DEFAULT ROLE journalist FOR taniel;

查看 mysql.user:

select * from mysql.user where user='taniel'\G
*************************** 1. row ***************************
                  Host: %
                  User: taniel
...
               is_role: N
          default_role: journalist
...

删除其他用户的默认角色

SET DEFAULT ROLE NONE FOR taniel; 

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.