我的CODE:
set global event_scheduler=1; /*开启执行计划*/
Create event if not exists clear_database_test on schedule /*这个单词错了 应为 SCHEDULE*/
every 1 day starts '2011-01-13 12:00:00' /*每天1点开始执行*/
DO call clear_database();
一个帮助信息:
以下的文章主要向大家描述的是MySQL定时执行的实际操作步骤,以及在MySQL定时执行的实际操作过程中值得我们大家注意的几点的描述,如果你对其相关的实际操作有兴趣了解的话,你就浏览以下的文章了。
查看event是否开启
show variables like '%sche%';
将事件计划开启
set global event_scheduler =1;
创建存储过程test
CREATE PROCEDURE test () BEGIN update examinfo SET endtime = now() WHERE id = 14; END;
创建event e_test
create event if not exists e_test on schedule every 30 second on completion preserve do call test();
每隔30秒将执行存储过程test,将当前时间更新到examinfo表中id=14的记录的endtime字段中去.
关闭事件任务
alter event e_test ON COMPLETION PRESERVE DISABLE;
开户事件任务
alter event e_test ON COMPLETION PRESERVE ENABLE;
以上测试均成功,测试环境为mysql 5.4.2-beta-community mysql community server(GPL)
以上的相关内容就是对MySQL定时执行的介绍,望你能有所收获。
上述的相关内容就是对MySQL定时执行的描述,希望会给你带来一些帮助在此方面。
原文标题:MySQL中的定时执行
连接:http://www.cnblogs.com/jembai/archive/2009/12/14/1623403.html
------------------------------------------------------------------------------------------------------------------------------------------------------------------
另外一个帮助信息:
MYSQL的事件是5.1新增加的,如果想体验,建议升级版本。
至于语法我就不多说了,手册上讲的很详细,我来说说几个要点以及一些实例。
注意事项:
1、EVENT权限是针对模式的(在MYSQL中也就是库的级别),不能对单独表来赋予权限。
2、必须在全局开启。
3、性能上的损失一定得考虑到。
mysql> show variables like '%event%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> set global event_scheduler = on;
Query OK, 0 rows affected (0.00 sec)
mysql> use event;
Database changed
例子:
我们来创建一个简单的文章表:
mysql>createtable article (id serial,title varchar(64)notnull, author_name varchar(64),content mediumtextnotnull, create_time datetime notnull,update_time datetime notnull);
Query OK, 0 rows affected (0.01 sec)
以及统计表:
mysql>createtable report (id intnotnullauto_increment primary key, r_date datenotnull,aid intnotnull,total intnotnull);
Query OK, 0 rows affected (0.01 sec)
mysql> 插入测试数据。。。
我们来建立一个存储过程。
mysql> delimiter ||
mysql>create procedure sp_report()
-> begin
->insertinto report(r_date,aid,total)selectdate(update_time)as r_date, id,count(1)from article group by date(create_time) order by r_date asc;
->end||
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
创建EVENT;
在一分钟后执行这个存储过程。
mysql>create event report_dawn on schedule at date_add(now(),interval 1 minute) on completion preserve do call sp_report();
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
| 7 | event_scheduler | localhost | NULL | Daemon | 5 | Waiting for next activation | NULL |
mysql> select * from report;
Empty set (0.00 sec)
察看现在的EVENT
mysql> show create event report_dawn\G
*************************** 1. row ***************************
Event: report_dawn
sql_mode:
time_zone: SYSTEM
Create Event: CREATE EVENT `report_dawn` ON SCHEDULE AT '2008-03-21 15:46:57' ON COMPLETION PRESERVE DISABLE DO call sp_report()
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
mysql>
我们来查看更新后的结果:
mysql> select * from report;
+----+------------+-----+-------+
| id | r_date | aid | total |
+----+------------+-----+-------+
| 1 | 2008-03-21 | 1 | 3 |
| 2 | 2008-03-22 | 16 | 1 |
| 3 | 2008-03-23 | 4 | 2 |
| 4 | 2008-03-23 | 6 | 2 |
| 5 | 2008-03-23 | 7 | 1 |
| 6 | 2008-03-23 | 8 | 2 |
| 7 | 2008-03-23 | 10 | 2 |
| 8 | 2008-04-13 | 12 | 1 |
| 9 | 2008-04-13 | 13 | 2 |
+----+------------+-----+-------+
9 rows in set (0.00 sec)
现在看看这个EVENT的状态,
mysql> select event_schema,event_name,status from information_schema.events where event_schema = 'event';
+--------------+-------------+----------+
| event_schema | event_name | status |
+--------------+-------------+----------+
| event | report_dawn | DISABLED |
+--------------+-------------+----------+
1 row in set (0.00 sec)
已经停止运行了。
mysql> select * from report;
+----+------------+-----+-------+
| id | r_date | aid | total |
+----+------------+-----+-------+
| 1 | 2008-03-21 | 1 | 3 |
| 2 | 2008-03-22 | 16 | 1 |
| 3 | 2008-03-23 | 4 | 2 |
| 4 | 2008-03-23 | 6 | 2 |
| 5 | 2008-03-23 | 7 | 1 |
| 6 | 2008-03-23 | 8 | 2 |
| 7 | 2008-03-23 | 10 | 2 |
| 8 | 2008-04-13 | 12 | 1 |
| 9 | 2008-04-13 | 13 | 2 |
| 10 | 2008-03-21 | 1 | 3 |
| 11 | 2008-03-22 | 16 | 1 |
| 12 | 2008-03-23 | 4 | 2 |
| 13 | 2008-03-23 | 6 | 2 |
| 14 | 2008-03-23 | 7 | 1 |
| 15 | 2008-03-23 | 8 | 2 |
| 16 | 2008-03-23 | 10 | 2 |
| 17 | 2008-04-13 | 12 | 1 |
| 18 | 2008-04-13 | 13 | 2 |
+----+------------+-----+-------+
18 rows in set (0.00 sec)
多了9条记录,
不过默认修改时间后。
在运行完毕后没有保存它。
因为时间已经过去了。
mysql> select event_schema,event_name,status from information_schema.events where event_schema = 'event';
Empty set (0.00 sec)
ON COMPLETION [NOT]PRESERVE
这个选项用来确认事件在执行完毕后是否保存其定义。