mysql之event
mysql之event
http://blog.csdn.net/lxgwm2008/article/details/9088521
Mysql事件调度器(Event Scheduler)类似于定时器,可以在某一个时间点执行一个SQL语句或一个语句块(BEGIN ... END);或者每隔固定间隔重复执行。类似于Linux下的crontab,或Windows下的Task Scheduler。
- mysql> show processlist;
- +-----+-----------------+-----------+------+---------+------+-----------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +-----+-----------------+-----------+------+---------+------+-----------------------------------+------------------+
- | 1 | system user | | | Daemon | 0 | Waiting for event from ndbcluster | NULL |
- | 3 | root | localhost | test | Query | 0 | NULL | show processlist |
- | 203 | event_scheduler | localhost | NULL | Daemon | 2 | Waiting for next activation | NULL |
- +-----+-----------------+-----------+------+---------+------+-----------------------------------+------------------+
- 3 rows in set (0.00 sec)
- set global event_scheduler = on
- set @@global.event_scheduler = on
- set global event_scheduler = 1
- set @@global.event_scheduler = 1
- SET GLOBAL event_scheduler = OFF
- SET @@global.event_scheduler = OFF
- SET GLOBAL event_scheduler = 0
- SET @@global.event_scheduler = 0
- mysql< SET @@event_scheduler = OFF;
- ERROR 1229 (HY000): Variable ‘event_scheduler‘ is a GLOBAL variable and should be set with SET GLOBAL
- mysql> select * from mysql.event \G
- *************************** 1. row ***************************
- db: test
- name: evt_insert
- body: insert into account values(NULL, 100.00)
- definer: root@localhost
- execute_at: NULL
- interval_value: 10
- interval_field: SECOND
- created: 2013-06-13 14:07:54
- modified: 2013-06-13 14:07:54
- last_executed: 2013-06-13 19:29:44
- starts: 2013-06-13 18:07:54
- ends: NULL
- status: ENABLED
- on_completion: DROP
- sql_mode:
- comment:
- originator: 1
- time_zone: SYSTEM
- character_set_client: utf8
- collation_connection: utf8_general_ci
- db_collation: utf8_general_ci
- body_utf8: insert into account values(NULL, 100.00)
- 1 row in set (0.01 sec)
- mysql> select * from information_schema.events \G
- *************************** 1. row ***************************
- EVENT_CATALOG: def
- EVENT_SCHEMA: test
- EVENT_NAME: evt_insert
- DEFINER: root@localhost
- TIME_ZONE: SYSTEM
- EVENT_BODY: SQL
- EVENT_DEFINITION: insert into account values(NULL, 100.00)
- EVENT_TYPE: RECURRING
- EXECUTE_AT: NULL
- INTERVAL_VALUE: 10
- INTERVAL_FIELD: SECOND
- SQL_MODE:
- STARTS: 2013-06-13 14:07:54
- ENDS: NULL
- STATUS: ENABLED
- ON_COMPLETION: NOT PRESERVE
- CREATED: 2013-06-13 14:07:54
- LAST_ALTERED: 2013-06-13 14:07:54
- LAST_EXECUTED: 2013-06-13 15:30:14
- EVENT_COMMENT:
- ORIGINATOR: 1
- CHARACTER_SET_CLIENT: utf8
- COLLATION_CONNECTION: utf8_general_ci
- DATABASE_COLLATION: utf8_general_ci
- 1 row in set (0.01 sec)
- mysql> show create event evt_insert \G
- *************************** 1. row ***************************
- Event: evt_insert
- sql_mode:
- time_zone: SYSTEM
- Create Event: CREATE DEFINER=`root`@`localhost` EVENT `evt_insert` ON SCHEDULE EVERY 10 SECOND STARTS ‘2013-06-13 14:07:54‘ ON COMPLETION NOT PRESERVE ENABLE DO insert into account values(NULL, 100.00)
- character_set_client: utf8
- collation_connection: utf8_general_ci
- Database Collation: utf8_general_ci
- 1 row in set (0.00 sec)
- mysql> show events \G
- *************************** 1. row ***************************
- Db: test
- Name: evt_insert
- Definer: root@localhost
- Time zone: SYSTEM
- Type: RECURRING
- Execute at: NULL
- Interval value: 10
- Interval field: SECOND
- Starts: 2013-06-13 14:07:54
- Ends: NULL
- Status: ENABLED
- Originator: 1
- character_set_client: utf8
- collation_connection: utf8_general_ci
- Database Collation: utf8_general_ci
- 1 row in set (0.00 sec)
ERROR 1144 (42000): Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be used
- 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)
- SHOW STATUS LIKE ‘%event%‘;
- mysql> show status like ‘%event%‘;
- +--------------------------------------+-------+
- | Variable_name | Value |
- +--------------------------------------+-------+
- | Com_alter_event | 0 |
- | Com_create_event | 1 |
- | Com_drop_event | 0 |
- | Com_show_binlog_events | 0 |
- | Com_show_create_event | 2 |
- | Com_show_events | 3 |
- | Com_show_relaylog_events | 0 |
- | Ndb_api_event_data_count_injector | 1 |
- | Ndb_api_event_nondata_count_injector | 0 |
- | Ndb_api_event_bytes_count_injector | 204 |
- | Ndb_api_event_data_count | 1 |
- | Ndb_api_event_nondata_count | 0 |
- | Ndb_api_event_bytes_count | 204 |
- +--------------------------------------+-------+
- 13 rows in set (0.00 sec)
- CREATE
- [DEFINER = { user | CURRENT_USER }]
- EVENT
- [IF NOT EXISTS]
- event_name
- ON SCHEDULE schedule
- [ON COMPLETION [NOT] PRESERVE]
- [ENABLE | DISABLE | DISABLE ON SLAVE]
- [COMMENT ‘comment‘]
- DO event_body;
- schedule:
- AT timestamp [+ INTERVAL interval] ...
- | EVERY interval
- [STARTS timestamp [+ INTERVAL interval] ...]
- [ENDS timestamp [+ INTERVAL interval] ...]
- interval:
- quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE | DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
- create event evt_insert
- on schedule every 10 second
- do update myschema.mytable set mycol = mycol + 1;
- mysql> SELECT NOW();
- +---------------------+
- | NOW() |
- +---------------------+
- | 2006-02-10 23:59:01 |
- +---------------------+
- 1 row in set (0.04 sec)
- mysql > CREATE EVENT e_totals
- -> ON SCHEDULE AT ‘2006-02-10 23:59:00‘
- -> DO INSERT INTO test.totals VALUES (NOW());
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql > SHOW WARNINGS\G
- *************************** 1. row ***************************
- Level: Note
- Code: 1588
- Message: Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was dropped immediately after creation.
- delimiter //
- CREATE EVENT e
- ON SCHEDULE
- EVERY 5 SECOND
- DO
- BEGIN
- DECLARE v INTEGER;
- DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
- SET v = 0;
- WHILE v < 5 DO
- INSERT INTO t1 VALUES (0);
- UPDATE t2 SET s1 = s1 + 1;
- SET v = v + 1;
- END WHILE;
- END //
- delimiter ;
- CREATE EVENT e_call_myproc
- ON SCHEDULE
- AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
- DO CALL myproc(5, 27);
- ALTER
- [DEFINER = { user | CURRENT_USER }]
- EVENT event_name
- [ON SCHEDULE schedule]
- [ON COMPLETION [NOT] PRESERVE]
- [RENAME TO new_event_name]
- [ENABLE | DISABLE | DISABLE ON SLAVE]
- [COMMENT ‘comment‘]
- [DO event_body]
- ALTER EVENT OLDDB.MYEVENT RENAME TO NEWDB.MYEVENT;
- DROP EVENT [IF EXISTS] event_name
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。