2013年5月13日星期一

使用MySQL的事件調度

Original post: http://anothermysqldba.blogspot.com/2013/05/using-mysql-event-scheduler.html

MySQL的事件調度器是非常有效的調度不同的東西,因為你需要他們。 
下面是一個簡單的例子,如何使用事件和如何使用事件調度不同的跨主奴隸。 

首先檢查你的事件調度器的狀態: 




> show variables like '%event%';
+---------------------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------------------+-------+
| event_scheduler | OFF |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
+---------------------------------------------------+-------+


要打開它你可以使用下面的命令,或者你也可以將它設置在你的my.cnf文件。 

set GLOBAL event_scheduler=ON; 


這個演示,我將創建一個數據庫 


create database events_test;
use events_test;


在主對於測試中,我將使用這個簡單的表在數據庫events_test。 


CREATE TABLE `foobar` (
`time_recorded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
); 


我喜歡我的事件運行存儲過程,因為我可以輕鬆測試程序推入一個事件之前。 

delimiter //
CREATE PROCEDURE mastereventtest()
BEGIN
insert into foobar values (NOW());
END//
delimiter ; 


現在,我將有此事件每分鐘運行一次就這個演示 



CREATE EVENT mastereventtest
ON SCHEDULE EVERY 1 MINUTE
COMMENT 'testing master events'
DO
call mastereventtest();




確認,這是在系統中。 



> show create event mastereventtest\G
*************************** 1. row ***************************
Event: mastereventtest
sql_mode: NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
time_zone: SYSTEM
Create Event: CREATE DEFINER=`root`@`localhost` EVENT `mastereventtest` ON SCHEDULE EVERY 1 MINUTE STARTS '2013-05-12 21:25:22' ON COMPLETION NOT PRESERVE ENABLE COMMENT 'testing master events' DO call mastereventtest()
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci





> show events \G
*************************** 1. row ***************************
Db: events_test
Name: mastereventtest
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: MINUTE
Starts: 2013-05-12 21:25:22
Ends: NULL
Status: ENABLED
Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci



我也將創建一個事件上的奴隸,因為我打算這樣做,我需要禁用的事件,我想。 因此,我將改變事件mastereventtest中,從屬服務器上。 


ALTER EVENT mastereventtest disable on slave; 



我會再次創造一個不同的表上的奴隸只下events_test的數據庫複製 


CREATE TABLE `foo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
); 


再次,我想創建一個程序來使用我的活動。 這不是必需的,但只是使得它更容易測試。 

delimiter //
CREATE PROCEDURE slaveeventtest()
BEGIN
insert into foo values ();
END//
delimiter ; 


這將是運行從屬的事件 


CREATE EVENT slaveeventtest
ON SCHEDULE EVERY 2 MINUTE
COMMENT 'testing master events'
DO
call slaveeventtest(); 





確認,也使得進入系統 



> show create event slaveeventtest\G
*************************** 1. row ***************************
Event: slaveeventtest
sql_mode: NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
time_zone: SYSTEM
Create Event: CREATE DEFINER=`root`@`localhost` EVENT `slaveeventtest` ON SCHEDULE EVERY 2 MINUTE STARTS '2013-05-12 21:14:08' ON COMPLETION NOT PRESERVE ENABLE COMMENT 'testing master events' DO call slaveeventtest()
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci 





> show events\G

Db: events_test
Name: slaveeventtest
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 2
Interval field: MINUTE
Starts: 2013-05-12 21:14:08
Ends: NULL
Status: ENABLED
Originator: 3
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci




事件信息,還可以查詢的信息架構 




> select * from information_schema.EVENTS\G




要調試事件檢查的最佳位置是錯誤日誌。 

請記住,當您檢查事件上的奴隸,你會看到這兩個事件 



> > show events \G
*************************** 1. row ***************************
Db: events_test
Name: mastereventtest
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: MINUTE
Starts: 2013-05-12 21:25:22
Ends: NULL
Status: SLAVESIDE_DISABLED
Originator: 3
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Db: events_test
Name: slaveeventtest
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 2
Interval field: MINUTE
Starts: 2013-05-12 21:14:08
Ends: NULL
Status: ENABLED
Originator: 3
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
2 rows in set (0.01 sec) 






現在沒有工作? 

如果執行SHOW PROCESSLIST,你將能夠看到當前事件的線程狀態 ,您可以在這裡閱讀更多有關國家的結果: 


你應該看到:等待下一次激活 
我也有在表中的結果: 



root@localhost [events_test]> select * from foobar;
+---------------------+
| time_recorded |
+---------------------+
| 2013-05-12 21:25:22 |
| 2013-05-12 21:26:22 |
| 2013-05-12 21:27:22 |
| 2013-05-12 21:28:22 |
| 2013-05-12 21:29:22 |
| 2013-05-12 21:30:22 |
| 2013-05-12 21:31:22 |
| 2013-05-12 21:32:22 |
+---------------------+
8 rows in set (0.00 sec)

root@localhost [events_test]> select * from foo;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+----+
8 rows in set (0.00 sec) 








要刪除或禁用活動: 

Drop事件IF EXISTS mastereventtest的; 
ALTER EVENT mastereventtest,禁用;