mysql event timer

 

Common tasks using events are to create tables, insert data, delete data, empty tables, and delete tables.

SHOW VARIABLES LIKE 'event_scheduler';

SET GLOBAL event_scheduler = 1;

CREATE EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE|DISABLE]
[COMMENT 'comment']
DO sql_statement


ALTER EVENT event_name  ENABLE/DISABLE

------------------------

create event schedule_add
on SCHEDULE EVERY 10 SECOND
on COMPLETION PRESERVE ENABLE
COMMENT 'Add data regularly'
do
insert into member(cname) values('hello');


alter event schedule_add  DISABLE;

SELECT * FROM mysql.event;

 

 

First, the basic concept
mysql5.1 version began to introduce the concept of event. Event is a "time trigger", which is different from the event triggering of triggers. Event is similar to linux crontab scheduled tasks for time triggering. By using it alone or by calling a stored procedure, at a specific point in time, the related SQL statement or stored procedure is triggered.

2. Scope
of application For operations that have fixed requirements at regular intervals, such as creating tables, deleting data, etc., events can be used to process them.


For example: use event to automatically create three tables that need to be used in the next month at 1:00 am on the 1st of each month.

3. Use authority
When using event to call SQL statement alone, the user needs to have event authority for viewing and creating, and when calling the SQL statement, the user needs to have the authority to execute the SQL. Event privilege settings are stored in the Event_priv field of the mysql.user table and mysql.db table.


When event and procedure are used together, users need to have create routine permission to view and create stored procedures, execute permission is required when calling stored procedures to execute, and users need to have the permission to execute SQL when stored procedures call specific SQL statements.
There are several types of EVENT commands: (1) Query the mysql.event table; (2) Through the SHOW EVENTS command; (3) Through the SHOW FULL EVENTS command; (4) By querying the information_schema.events table (5) SHOW CREATE EVENT. In short, the frequency of event usage is low, and it is recommended to use the root user for creation and maintenance.
 









 

4. Basic grammar

4.1 Turn on the timer
For the event to work, the MySQL constant GLOBAL event_scheduler must be on or 1.

-- Check if the timer is enabled

Copy the code The code is as follows:

SHOW VARIABLES LIKE 'event_scheduler';


-- Enable timer 0:off 1:on 

Copy the code The code is as follows:

SET GLOBAL event_scheduler = 1; 


当你设定事件计划为0 或OFF,即关闭事件计划进程的时候,不会有新的事件执行,但现有的正在运行的事件会执行到完毕

对于我们线上环境来说,使用event时,注意在主库上开启定时器,从库上关闭定时器,event触发所有操作均会记录binlog进行主从同步,从库上开启定时器很可能造成卡库。切换主库后之后记得将新主库上的定时器打开。
请特别注意!

 

4.2 创建事件
CREATE EVENT 的语法如下:

CREATE EVENT
[IF NOT EXISTS] ---------------------------------------------*标注1
event_name -----------------------------------------------------*标注2

ON SCHEDULE schedule ------------------------------------*标注3 
[ON COMPLETION [NOT] PRESERVE] -----------------*标注4
[ENABLE | DISABLE] ----------------------------------------*标注5 
[COMMENT 'comment'] --------------------------------------*标注6 
DO sql_statement -----------------------------------------------*标注7

说明:
 

标注1:[IF NOT EXISTS]
 

使用IF NOT EXISTS,只有在同名event不存在时才创建,否则忽略。建议不使用以保证event创建成功。

标注2:event_name
 

名称最大长度可以是64个字节。名字必须是当前Dateabase中唯一的,同一个数据库不能有同名的event。

使用event常见的工作是创建表、插入数据、删除数据、清空表、删除表。

为了避免命名规范带来的不便,最好让事件名称具有描述整个事件的能力。建议命名规则如下为:动作名称_(INTO/FROM_)表名_TIME,例如:
1.     每月创建(清空/删除)fans表:  
create(truncate/drop)_table_fans_month;
2.     每天从fans表插入(删除)数据: 
insert(delete)_into(from)_fans_day;
标注3:ON SCHEDULE
 

ON SCHEDULE 计划任务,有两种设定计划任务的方式:
 

1. AT 时间戳,用来完成单次的计划任务。

2. EVERY 时间(单位)的数量时间单位[STARTS 时间戳] [ENDS时间戳],用来完成重复的计划任务。

在两种计划任务中,时间戳可以是任意的TIMESTAMP 和DATETIME 数据类型,时间戳需要大于当前时间。

在重复的计划任务中,时间(单位)的数量可以是任意非空(Not Null)的整数式,时间单位是关键词:YEAR,MONTH,DAY,HOUR,MINUTE 或者SECOND。

提示: 其他的时间单位也是合法的如:QUARTER, WEEK, YEAR_MONTH,DAY_HOUR,DAY_MINUTE,DAY_SECOND,HOUR_MINUTE,HOUR_SECOND, MINUTE_SECOND,不建议使用这些不标准的时间单位。

标注4: [ON COMPLETION [NOT] PRESERVE]
 

ON COMPLETION参数表示"当这个事件不会再发生的时候",即当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而PRESERVE的作用是使事件在执行完毕后不会被Drop掉,建议使用该参数,以便于查看EVENT具体信息。

标注5:[ENABLE | DISABLE]
参数Enable和Disable表示设定事件的状态。Enable表示系统将执行这个事件。Disable表示系统不执行该事件。


可以用如下命令关闭或开启事件:

ALTER EVENT event_name  ENABLE/DISABLE
标注6:[COMMENT 'comment']
 

注释会出现在元数据中,它存储在information_schema表的COMMENT列,最大长度为64个字节。'comment'表示将注释内容放在单引号之间,建议使用注释以表达更全面的信息。

标注 7: DO sql_statement
 

DO sql_statement字段表示该event需要执行的SQL语句或存储过程。这里的SQL语句可以是复合语句,例如:
BEGIN
CREATE TABLE test1;//创建表(需要测试一下)
DROP TABLE test2;//删除表
CALL proc_test1();//调用存储过程
END

使用BEGIN和END标识符将复合SQL语句按照执行顺序放在之间。当然SQL语句是有限制的,对它的限制跟函数Function和触发器Trigger 中对SQL语句的限制是一样的,如果你在函数Function 和触发器Trigger 中不能使用某些SQL,同样的在EVENT中也不能使用。明确的来说有下面几个:

LOCK TABLES
UNLOCK TABLES
CREATE EVENT
ALTER EVENT
LOAD DATA

4.3  执行逻辑
For (已建立事件each event that has been created)

If (事件的状态非DISABLE)
And (当前时间在ENDS时间之前)
And (当前时间在STARTS时间之后)
And (在上次执行后经过的时间)
And (没有被执行)
Then:
建立一个新的线程
传递事件的SQL语句给新的线程
(该线程在执行完毕后会自动关闭)

4.4 修改事件
使用ALTER EVENT 来修改事件,具体的ALTER语法如下,与创建事件的语法类似:

ALTER EVENT
event_name

ON SCHEDULE schedule
[RENAME TO new_event_name]
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement

 
4.5 删除事件
EVENT使用DROP EVENT语句来删除已经创建的事件,语法如下:


DROP EVENT
[IF EXISTS]
event_name

 
但当一个事件正在运行中时,删除该事件不会导致事件停止,事件会执行到完毕为止。使用DROP USER和DROP DATABASE 语句同时会将包含其中的事件删除。

五、常用实例
每隔一秒自动调用e_test()存储过程

复制代码代码如下:

CREATE EVENT IF NOT EXISTS e_test
ON SCHEDULE EVERY 1 SECOND
ON COMPLETION PRESERVE
DO CALL e_test();


每个月的一号凌晨1 点执行STAT()存储过程:

复制代码代码如下:

CREATE  EVENT  NOT EXISTS  STAT
ON  SCHEDULE  EVERY  1  MONTH  STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON  COMPLETION  PRESERVE  ENABLE
DO
BEGIN
CALL STAT();
END

 

转载:http://www.jb51.net/article/38319.htm

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397063&siteId=291194637