8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

27.4.1 事件调度器概述27.4.1 Event Scheduler Overview

MySQL 事件是根据计划运行的任务。因此,我们有时称它们为 定时 事件。当您创建一个事件时,实际上是在创建一个命名的数据库对象,其中包含一个或多个将在一个或多个固定间隔执行的 SQL 语句,并从特定的开始日期和时间执行到结束日期和时间。从概念上讲,这与 Unix 的 crontab(也称为“cron 作业”)或 Windows 任务计划程序的理念类似。MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. When you create an event, you are creating a named database object containing one or more SQL statements to be executed at one or more regular intervals, beginning and ending at a specific date and time. Conceptually, this is similar to the idea of the Unix crontab (also known as a “cron job”) or the Windows Task Scheduler.

这种类型的计划任务有时也被称为“时间触发器”,这意味着这些对象是由时间的流逝触发的。虽然这在本质上是正确的,但我们更倾向于使用事件一词,以避免与第 27.3 节,“使用触发器”中讨论的触发器类型混淆。事件更具体一点,不应与“临时触发器”混淆。触发器是一个数据库对象,其语句会在给定表上发生特定类型事件时执行,而(计划)事件是一个对象,其语句会在指定时间间隔过去后执行。Scheduled tasks of this type are also sometimes known as “temporal triggers”, implying that these are objects that are triggered by the passage of time. While this is essentially correct, we prefer to use the term events to avoid confusion with triggers of the type discussed in Section 27.3, “Using Triggers”. Events should more specifically not be confused with “temporary triggers”. Whereas a trigger is a database object whose statements are executed in response to a specific type of event that occurs on a given table, a (scheduled) event is an object whose statements are executed in response to the passage of a specified time interval.

虽然 SQL 标准中没有事件调度的规定,但在其他数据库系统中已有先例,你可能会注意到这些实现与 MySQL 服务器中的实现有一些相似之处。While there is no provision in the SQL Standard for event scheduling, there are precedents in other database systems, and you may notice some similarities between these implementations and that found in the MySQL Server.

MySQL 事件具有以下主要特性和属性:MySQL Events have the following major features and properties:

  • 在 MySQL 中,事件由其名称和所分配的模式唯一标识。In MySQL, an event is uniquely identified by its name and the schema to which it is assigned.

  • 事件根据计划执行特定操作。此操作由 SQL 语句组成,如果需要,可以在 BEGIN ... END 块中使用(参见 第 15.6 节,“复合语句语法”)。事件的时间安排可以是 一次性 或 重复性。一次性事件只执行一次。重复性事件会在固定时间间隔重复执行,其计划可以指定开始日期和时间、结束日期和时间、两者或都不指定。(默认情况下,重复性事件的计划在创建后立即开始,并无限期持续,直到它被禁用或删除。)An event performs a specific action according to a schedule. This action consists of an SQL statement, which can be a compound statement in a BEGIN ... END block if desired (see Section 15.6, “Compound Statement Syntax”). An event's timing can be either one-time or recurrent. A one-time event executes one time only. A recurrent event repeats its action at a regular interval, and the schedule for a recurring event can be assigned a specific start day and time, end day and time, both, or neither. (By default, a recurring event's schedule begins as soon as it is created, and continues indefinitely, until it is disabled or dropped.)

    如果重复事件在其调度间隔内未终止,可能导致事件的多个实例同时执行。如果这是不希望的,您应建立机制防止同时执行实例。例如,您可以使用 GET_LOCK() 函数,或行锁或表锁。If a repeating event does not terminate within its scheduling interval, the result may be multiple instances of the event executing simultaneously. If this is undesirable, you should institute a mechanism to prevent simultaneous instances. For example, you could use the GET_LOCK() function, or row or table locking.

  • 用户可以使用用于此目的的 SQL 语句创建、修改和删除计划事件。语法无效的事件创建和修改语句会以适当的错误信息失败。用户可能在事件的操作中包含需要该用户实际上并不拥有的权限的语句。事件创建或修改语句成功,但事件的操作会失败。详情请参见第 27.4.6 节,“事件调度器和 MySQL 权限”。Users can create, modify, and drop scheduled events using SQL statements intended for these purposes. Syntactically invalid event creation and modification statements fail with an appropriate error message. A user may include statements in an event's action which require privileges that the user does not actually have. The event creation or modification statement succeeds but the event's action fails. See Section 27.4.6, “The Event Scheduler and MySQL Privileges” for details.

  • 事件的许多属性可以使用 SQL 语句进行设置或修改。这些属性包括事件的名称、时间、持久性(即在其计划到期后是否保留)、状态(启用或禁用)、要执行的操作,以及它所属的模式。请参见 第 15.1.3 节,“ALTER EVENT 语句”。Many of the properties of an event can be set or modified using SQL statements. These properties include the event's name, timing, persistence (that is, whether it is preserved following the expiration of its schedule), status (enabled or disabled), action to be performed, and the schema to which it is assigned. See Section 15.1.3, “ALTER EVENT Statement”.

    事件的默认定义者是创建该事件的用户,除非事件已被更改,在这种情况下,定义者是最后发出影响该事件的ALTER EVENT语句的用户。任何在事件定义的数据库上拥有EVENT权限的用户都可以修改事件。参见第 27.4.6 节,“事件调度器和 MySQL 权限”。The default definer of an event is the user who created the event, unless the event has been altered, in which case the definer is the user who issued the last ALTER EVENT statement affecting that event. An event can be modified by any user having the EVENT privilege on the database for which the event is defined. See Section 27.4.6, “The Event Scheduler and MySQL Privileges”.

  • 事件的操作语句可以包含存储程序中允许的大多数 SQL 语句。有关限制,请参见第 27.8 节,“存储程序的限制”。An event's action statement may include most SQL statements permitted within stored routines. For restrictions, see Section 27.8, “Restrictions on Stored Programs”.