8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 27 存储对象 / 27.4 使用事件调度程序 / 27.4.6 事件调度程序和 MySQL 权限
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 93%

27.4.6 事件调度器和 MySQL 权限27.4.6 The Event Scheduler and MySQL Privileges

要启用或禁用计划事件的执行,需要设置全局 event_scheduler 系统变量的值。这需要具有足够权限来设置全局系统变量。参见 第 7.1.9.1 节,“系统变量权限”。To enable or disable the execution of scheduled events, it is necessary to set the value of the global event_scheduler system variable. This requires privileges sufficient to set global system variables. See Section 7.1.9.1, “System Variable Privileges”.

EVENT 权限管理事件的创建、修改和删除。这个权限可以使用 GRANT 授予。例如,这条 GRANT 语句将 EVENT 权限授予模式名为 myschema 的用户 jon@ghidora:The EVENT privilege governs the creation, modification, and deletion of events. This privilege can be bestowed using GRANT. For example, this GRANT statement confers the EVENT privilege for the schema named myschema on the user jon@ghidora:

GRANT EVENT ON myschema.* TO jon@ghidora;

(我们假设该用户账户已存在,并且我们希望其余状态保持不变。)(We assume that this user account already exists, and that we wish for it to remain unchanged otherwise.)

要将相同的用户授予所有模式的EVENT 权限,请使用以下语句:To grant this same user the EVENT privilege on all schemas, use the following statement:

GRANT EVENT ON *.* TO jon@ghidora;

EVENT 权限具有全局或模式级别范围。因此,尝试在单个表上授予该权限会导致如下所示的错误:The EVENT privilege has global or schema-level scope. Therefore, trying to grant it on a single table results in an error as shown:

mysql> GRANT EVENT ON myschema.mytable TO jon@ghidora;
ERROR 1144 (42000): Illegal GRANT/REVOKE command; please
consult the manual to see which privileges can be used

重要的是要理解,事件是以其定义者的权限执行的,并且它不能执行定义者没有相应权限的任何操作。例如,假设 jon@ghidora 对 myschema 拥有 EVENT 权限。还假设该用户对 myschema 拥有 SELECT 权限,但对该模式没有其他权限。jon@ghidora 可以创建一个类似如下的新事件:It is important to understand that an event is executed with the privileges of its definer, and that it cannot perform any actions for which its definer does not have the requisite privileges. For example, suppose that jon@ghidora has the EVENT privilege for myschema. Suppose also that this user has the SELECT privilege for myschema, but no other privileges for this schema. It is possible for jon@ghidora to create a new event such as this one:

CREATE EVENT e_store_ts
    ON SCHEDULE
      EVERY 10 SECOND
    DO
      INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());

用户等待大约一分钟,然后执行一个 SELECT * FROM mytable; 查询,期望看到表中有几行新数据。然而,表是空的。由于该用户没有针对相关表的 INSERT 权限,因此该事件没有任何效果。The user waits for a minute or so, and then performs a SELECT * FROM mytable; query, expecting to see several new rows in the table. Instead, the table is empty. Since the user does not have the INSERT privilege for the table in question, the event has no effect.

如果检查 MySQL 错误日志 (hostname.err),可以看到该事件正在执行,但它尝试执行的操作失败了:If you inspect the MySQL error log (hostname.err), you can see that the event is executing, but the action it is attempting to perform fails:

2013-09-24T12:41:31.261992Z 25 [ERROR] Event Scheduler:
[jon@ghidora][cookbook.e_store_ts] INSERT command denied to user
'jon'@'ghidora' for table 'mytable'
2013-09-24T12:41:31.262022Z 25 [Note] Event Scheduler:
[jon@ghidora].[myschema.e_store_ts] event execution failed.
2013-09-24T12:41:41.271796Z 26 [ERROR] Event Scheduler:
[jon@ghidora][cookbook.e_store_ts] INSERT command denied to user
'jon'@'ghidora' for table 'mytable'
2013-09-24T12:41:41.272761Z 26 [Note] Event Scheduler:
[jon@ghidora].[myschema.e_store_ts] event execution failed.

由于该用户很可能没有访问错误日志的权限,可以通过直接执行事件的操作语句来验证其是否有效:Since this user very likely does not have access to the error log, it is possible to verify whether the event's action statement is valid by executing it directly:

mysql> INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());
ERROR 1142 (42000): INSERT command denied to user
'jon'@'ghidora' for table 'mytable'

检查信息模式 EVENTS 表显示 e_store_ts 存在且已启用,但其 LAST_EXECUTED 列为 NULL:Inspection of the Information Schema EVENTS table shows that e_store_ts exists and is enabled, but its LAST_EXECUTED column is NULL:

mysql> SELECT * FROM INFORMATION_SCHEMA.EVENTS
     >     WHERE EVENT_NAME='e_store_ts'
     >     AND EVENT_SCHEMA='myschema'\G
*************************** 1. row ***************************
   EVENT_CATALOG: NULL
    EVENT_SCHEMA: myschema
      EVENT_NAME: e_store_ts
         DEFINER: jon@ghidora
      EVENT_BODY: SQL
EVENT_DEFINITION: INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP())
      EVENT_TYPE: RECURRING
      EXECUTE_AT: NULL
  INTERVAL_VALUE: 5
  INTERVAL_FIELD: SECOND
        SQL_MODE: NULL
          STARTS: 0000-00-00 00:00:00
            ENDS: 0000-00-00 00:00:00
          STATUS: ENABLED
   ON_COMPLETION: NOT PRESERVE
         CREATED: 2006-02-09 22:36:06
    LAST_ALTERED: 2006-02-09 22:36:06
   LAST_EXECUTED: NULL
   EVENT_COMMENT:
1 row in set (0.00 sec)

要撤销 EVENT 权限,请使用 REVOKE 语句。在此示例中,从 jon@ghidora 用户帐户中移除架构 myschema 上的 EVENT 权限:To rescind the EVENT privilege, use the REVOKE statement. In this example, the EVENT privilege on the schema myschema is removed from the jon@ghidora user account:

REVOKE EVENT ON myschema.* FROM jon@ghidora;
Important

从用户撤销 EVENT 权限不会删除或禁用该用户可能已创建的任何事件。Revoking the EVENT privilege from a user does not delete or disable any events that may have been created by that user.

由于重命名或删除创建事件的用户,事件不会被迁移或删除。An event is not migrated or dropped as a result of renaming or dropping the user who created it.

假设用户 jon@ghidora 已被授予 EVENT 和 INSERT 权限在 myschema 模式下。然后该用户创建了以下事件:Suppose that the user jon@ghidora has been granted the EVENT and INSERT privileges on the myschema schema. This user then creates the following event:

CREATE EVENT e_insert
    ON SCHEDULE
      EVERY 7 SECOND
    DO
      INSERT INTO myschema.mytable;

在创建此事件之后,root 撤销了 EVENT 权限给 jon@ghidora。然而,e_insert 仍然继续执行,每七秒向 mytable 插入一行新数据。如果 root 执行以下任一语句,结果也是一样的:After this event has been created, root revokes the EVENT privilege for jon@ghidora. However, e_insert continues to execute, inserting a new row into mytable each seven seconds. The same would be true if root had issued either of these statements:

  • DROP USER jon@ghidora;

  • RENAME USER jon@ghidora TO someotherguy@ghidora;

你可以通过在执行 DROP USER 或 RENAME USER 语句之前和之后检查信息模式 EVENTS 表来验证这一点。You can verify that this is true by examining the Information Schema EVENTS table before and after issuing a DROP USER or RENAME USER statement.

事件定义存储在数据字典中。要删除由其他用户账户创建的事件,你必须是 MySQL root 用户或具有必要权限的其他用户。Event definitions are stored in the data dictionary. To drop an event created by another user account, you must be the MySQL root user or another user with the necessary privileges.

用户的 EVENT 权限存储在 mysql.user 和 mysql.db 表的 Event_priv 列中。在这两种情况下,该列的值为 'Y' 或 'N'。默认值为 'N'。mysql.user.Event_priv 仅在某个用户拥有全局 EVENT 权限时才设置为 'Y'(即,使用 GRANT EVENT ON *.* 授予权限)。对于模式级别的 EVENT 权限,GRANT 会在 mysql.db 中创建一行,并将该行的 Db 列设置为模式名称,User 列设置为用户名,Event_priv 列设置为 'Y'。通常不需要直接操作这些表,因为 GRANT EVENT 和 REVOKE EVENT 语句会对其执行所需的操作。Users' EVENT privileges are stored in the Event_priv columns of the mysql.user and mysql.db tables. In both cases, this column holds one of the values 'Y' or 'N'. 'N' is the default. mysql.user.Event_priv is set to 'Y' for a given user only if that user has the global EVENT privilege (that is, if the privilege was bestowed using GRANT EVENT ON *.*). For a schema-level EVENT privilege, GRANT creates a row in mysql.db and sets that row's Db column to the name of the schema, the User column to the name of the user, and the Event_priv column to 'Y'. There should never be any need to manipulate these tables directly, since the GRANT EVENT and REVOKE EVENT statements perform the required operations on them.

五个状态变量提供与事件相关操作的计数(但不包括事件执行的语句;参见 第 27.8 节,“存储程序的限制”)。这些是:Five status variables provide counts of event-related operations (but not of statements executed by events; see Section 27.8, “Restrictions on Stored Programs”). These are:

  • Com_create_event:自上次服务器重启以来执行的 CREATE EVENT 语句的数量。Com_create_event: The number of CREATE EVENT statements executed since the last server restart.

  • Com_alter_event:自上次服务器重启以来执行的 ALTER EVENT 语句的数量。Com_alter_event: The number of ALTER EVENT statements executed since the last server restart.

  • Com_drop_event:自上次服务器重启以来执行的 DROP EVENT 语句的次数。Com_drop_event: The number of DROP EVENT statements executed since the last server restart.

  • Com_show_create_event:自上次服务器重启以来执行的 SHOW CREATE EVENT 语句的次数。Com_show_create_event: The number of SHOW CREATE EVENT statements executed since the last server restart.

  • Com_show_events:自上次服务器重启以来执行的 SHOW EVENTS 语句的次数。Com_show_events: The number of SHOW EVENTS statements executed since the last server restart.

您可以运行语句 SHOW STATUS LIKE '%event%'; 来一次查看所有这些的当前数值。You can view current values for all of these at one time by running the statement SHOW STATUS LIKE '%event%';.