事件由一个特殊的事件调度器线程执行;当我们提到事件调度器时,实际上指的是该线程。当运行时,具有PROCESS权限的用户可以在SHOW PROCESSLIST的输出中看到事件调度器线程及其当前状态,如下文讨论所示。Events are executed by a special event
scheduler thread; when we refer to the Event Scheduler,
we actually refer to this thread. When running, the event
scheduler thread and its current state can be seen by users having
the PROCESS privilege in the output
of SHOW PROCESSLIST, as shown in
the discussion that follows.
全局event_scheduler系统变量决定事件调度器是否在服务器上启用并运行。它具有以下值之一,这些值会按照描述影响事件调度:The global event_scheduler system
variable determines whether the Event Scheduler is enabled and
running on the server. It has one of the following values, which
affect event scheduling as described:
ON:事件调度器已启动;事件调度器线程运行并执行所有计划的事件。ON是默认的event_scheduler值。ON: The Event Scheduler is started; the event scheduler thread runs and executes all scheduled events.ONis the defaultevent_schedulervalue.当事件调度器为
ON时,事件调度器线程会在SHOW PROCESSLIST的输出中显示为守护进程,并且其状态如图所示:When the Event Scheduler isON, the event scheduler thread is listed in the output ofSHOW PROCESSLISTas a daemon process, and its state is represented as shown here:mysql>
SHOW PROCESSLIST\G*************************** 1. row *************************** Id: 1 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: show processlist *************************** 2. row *************************** Id: 2 User: event_scheduler Host: localhost db: NULL Command: Daemon Time: 3 State: Waiting for next activation Info: NULL 2 rows in set (0.00 sec)可以通过将
event_scheduler的值设置为OFF来停止事件调度。Event scheduling can be stopped by setting the value ofevent_schedulertoOFF.OFF:事件调度器已停止。事件调度器线程不运行,不会显示在SHOW PROCESSLIST的输出中,也不会执行任何计划事件。OFF: The Event Scheduler is stopped. The event scheduler thread does not run, is not shown in the output ofSHOW PROCESSLIST, and no scheduled events execute.当事件调度器停止时(
event_scheduler为OFF),可以通过将event_scheduler的值设置为ON来启动它。(参见下一项。)When the Event Scheduler is stopped (event_schedulerisOFF), it can be started by setting the value ofevent_schedulertoON. (See next item.)DISABLED:此值使事件调度器无法运行。当事件调度器为DISABLED时,事件调度器线程不会运行(因此不会出现在SHOW PROCESSLIST的输出中)。此外,事件调度器的状态不能在运行时更改。DISABLED: This value renders the Event Scheduler nonoperational. When the Event Scheduler isDISABLED, the event scheduler thread does not run (and so does not appear in the output ofSHOW PROCESSLIST). In addition, the Event Scheduler state cannot be changed at runtime.
如果事件调度器状态尚未设置为DISABLED,event_scheduler可以在ON和OFF之间切换(使用SET)。在设置此变量时,也可以使用0表示OFF,1表示ON。因此,以下四条语句中的任意一条都可以在mysql客户端中用来开启事件调度器:If the Event Scheduler status has not been set to
DISABLED,
event_scheduler can be toggled
between ON and OFF (using
SET). It
is also possible to use 0 for
OFF, and 1 for
ON when setting this variable. Thus, any of the
following 4 statements can be used in the mysql
client to turn on the Event Scheduler:
SET GLOBAL event_scheduler = ON; SET @@GLOBAL.event_scheduler = ON; SET GLOBAL event_scheduler = 1; SET @@GLOBAL.event_scheduler = 1;
同样,以下四条语句中的任意一条都可以用来关闭事件调度器:Similarly, any of these 4 statements can be used to turn off the Event Scheduler:
SET GLOBAL event_scheduler = OFF; SET @@GLOBAL.event_scheduler = OFF; SET GLOBAL event_scheduler = 0; SET @@GLOBAL.event_scheduler = 0;
如果启用了事件调度器,启用 super_read_only 系统变量将阻止其更新 events 数据字典表中的事件 “last executed” 时间戳。这会导致事件调度器在下一次尝试执行计划事件时停止,同时向服务器错误日志写入一条消息。(在这种情况下,event_scheduler 系统变量不会从 ON 变为 OFF。其含义是,该变量拒绝了 DBA 对事件调度器启用或禁用的 意图,而事件调度器的实际启动或停止状态可能与之不同。)如果随后在启用后禁用 super_read_only,服务器会根据需要自动重新启动事件调度器。If the Event Scheduler is enabled, enabling the
super_read_only system variable
prevents it from updating event “last executed”
timestamps in the events data dictionary
table. This causes the Event Scheduler to stop the next time it
tries to execute a scheduled event, after writing a message to
the server error log. (In this situation the
event_scheduler system variable
does not change from ON to
OFF. An implication is that this variable
rejects the DBA intent that the Event
Scheduler be enabled or disabled, where its actual status of
started or stopped may be distinct.). If
super_read_only is subsequently
disabled after being enabled, the server automatically restarts
the Event Scheduler as needed.
尽管 ON 和 OFF 有数字等价,但通过 event_scheduler 使用 SELECT 或 SHOW
VARIABLES 显示的值总是 OFF、ON 或 DISABLED 之一。DISABLED 没有数字等价。因此,在设置此变量时,通常更倾向于使用 ON 和 OFF 而不是 1 和 0。Although ON and OFF have
numeric equivalents, the value displayed for
event_scheduler by
SELECT or SHOW
VARIABLES is always one of OFF,
ON, or DISABLED.
DISABLED has no numeric
equivalent. For this reason, ON and
OFF are usually preferred over
1 and 0 when setting this
variable.
请注意,尝试设置 event_scheduler 而未将其指定为全局变量会导致错误:Note that attempting to set
event_scheduler without
specifying it as a global variable causes an error:
mysql< SET @@event_scheduler = OFF;
ERROR 1229 (HY000): Variable 'event_scheduler' is a GLOBAL
variable and should be set with SET GLOBAL
只能在服务器启动时将事件调度器设置为 DISABLED。如果 event_scheduler 为 ON 或 OFF,则无法在运行时将其设置为 DISABLED。此外,如果事件调度器在启动时设置为 DISABLED,您无法在运行时更改 event_scheduler 的值。It is possible to set the Event Scheduler to
DISABLED only at server startup. If
event_scheduler is
ON or OFF, you cannot set
it to DISABLED at runtime. Also, if the Event
Scheduler is set to DISABLED at startup, you
cannot change the value of
event_scheduler at runtime.
要禁用事件调度器,请使用以下两种方法之一:To disable the event scheduler, use one of the following two methods:
作为启动服务器时的命令行选项:As a command-line option when starting the server:
--event-scheduler=DISABLED
在服务器配置文件 (
my.cnf,或在 Windows 系统上为my.ini) 中,包含服务器可以读取的行(例如,在[mysqld]部分中):In the server configuration file (my.cnf, ormy.inion Windows systems), include the line where it can be read by the server (for example, in a[mysqld]section):event_scheduler=DISABLED
要启用事件调度程序,请在不使用 --event-scheduler=DISABLED 命令行选项的情况下重新启动服务器,或者在适当情况下移除或注释掉服务器配置文件中包含 event-scheduler=DISABLED 的行。或者,在启动服务器时,您可以使用 ON(或 1)或 OFF(或 0)来代替 DISABLED 值。To enable the Event Scheduler, restart the server without the
--event-scheduler=DISABLED
command-line option, or after removing or commenting out the line
containing event-scheduler=DISABLED
in the server configuration file, as appropriate. Alternatively,
you can use ON (or 1) or
OFF (or 0) in place of the
DISABLED value when starting the server.
当 event_scheduler 设置为 DISABLED 时,您可以发出事件操作语句。在这种情况下不会生成任何警告或错误(前提是这些语句本身是有效的)。然而,计划事件在该变量设置为 ON(或 1)之前无法执行。一旦设置完成,事件调度线程会执行所有满足调度条件的事件。You can issue event-manipulation statements when
event_scheduler is set to
DISABLED. No warnings or errors are generated
in such cases (provided that the statements are themselves
valid). However, scheduled events cannot execute until this
variable is set to ON (or
1). Once this has been done, the event
scheduler thread executes all events whose scheduling conditions
are satisfied.
使用 --skip-grant-tables 选项启动 MySQL 服务器会导致 event_scheduler 被设置为 DISABLED,覆盖命令行或 my.cnf 或 my.ini 文件中设置的任何其他值(缺陷 #26807)。Starting the MySQL server with the
--skip-grant-tables option causes
event_scheduler to be set to
DISABLED, overriding any other value set either
on the command line or in the my.cnf or
my.ini file (Bug #26807).
有关用于创建、修改和删除事件的 SQL 语句,请参阅 第 27.4.3 节,“事件语法”。For SQL statements used to create, alter, and drop events, see Section 27.4.3, “Event Syntax”.
MySQL 在 INFORMATION_SCHEMA 数据库中提供了一个 EVENTS 表。可以查询此表以获取关于服务器上已定义的计划事件的信息。有关详细信息,请参见 第 27.4.4 节,“事件元数据”,以及 第 28.3.14 节,“INFORMATION_SCHEMA EVENTS 表”。MySQL provides an EVENTS table in the
INFORMATION_SCHEMA database. This table can be
queried to obtain information about scheduled events which have
been defined on the server. See Section 27.4.4, “Event Metadata”,
and Section 28.3.14, “The INFORMATION_SCHEMA EVENTS Table”, for more
information.
有关事件调度和 MySQL 权限系统的信息,请参见 第 27.4.6 节,“事件调度器和 MySQL 权限”。For information regarding event scheduling and the MySQL privilege system, see Section 27.4.6, “The Event Scheduler and MySQL Privileges”.