在主從架構中,在master建立一個event,如下:
- mysql> show create event `insert`\G;
- *************************** 1. row ***************************
- Event: insert
- sql_mode:
- time_zone: SYSTEM
- Create Event: CREATE DEFINER=`root`@`localhost` EVENT `insert`
- ON SCHEDULE EVERY 1 MINUTE STARTS '2012-11-20 16:10:09'
- ON COMPLETION PRESERVE ENABLE DO BEGIN
- insert into t3(name) values('aa');
- END
- character_set_client: utf8
- collation_connection: utf8_general_ci
- Database Collation: utf8_general_ci
- 1 row in set (0.02 sec)
-
- ERROR:
- No query specified
slave同步過去,結果是這樣的,注意紅色字型:
- mysql> show create event `insert`\G;
- *************************** 1. row ***************************
- Event: insert
- sql_mode:
- time_zone: SYSTEM
- Create Event: CREATE DEFINER=`root`@`localhost` EVENT `insert`
- ON SCHEDULE EVERY 1 MINUTE STARTS '2012-11-20 16:10:09'
- ON COMPLETION PRESERVE DISABLE ON SLAVE DO BEGIN
- insert into t3(name) values('aa');
- END
- character_set_client: utf8
- collation_connection: utf8_general_ci
- Database Collation: utf8_general_ci
- 1 row in set (0.02 sec)
-
- ERROR:
- No query specified
再回過頭來,看下事件狀態,注意紅色字型:
在master上
- mysql> show events;
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
- | Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
- | test | insert | root@localhost | SYSTEM | RECURRING | NULL | 1 | MINUTE | 2012-11-20 16:10:09 | NULL | ENABLED | 25 | utf8 | utf8_general_ci | utf8_general_ci |
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
- 1 row in set (0.11 sec)
在slave上
- mysql> show events;
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+--------------------+------------+----------------------+----------------------+--------------------+
- | Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+--------------------+------------+----------------------+----------------------+--------------------+
- | test | insert | root@localhost | SYSTEM | RECURRING | NULL | 1 | MINUTE | 2012-11-20 16:10:09 | NULL | SLAVESIDE_DISABLED | 25 | utf8 | utf8_general_ci | utf8_general_ci |
- +------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+--------------------+------------+----------------------+----------------------+--------------------+
- 1 row in set (0.10 sec)
也就是說,事件只能在master觸發,slave上不會觸發,否則如果slave上觸發了,同步複製就會壞掉。
當主從故障切換之後,VIP漂移到了以前的slave上,此時slave成了新的master。
但這時,事件的狀態還是維持SLAVESIDE_DISABLED,並不是也改成了ENABLED,這樣就會造成切換以後,事件無法執行。
所以,需要人工重新開啟事件狀態。
- mysql> alter event `insert` enable;
- Query OK, 0 rows affected (0.05 sec)
參考手冊:
650) this.width=650;" border="0" alt="" src="http://www.bkjia.com/uploads/allimg/131229/1Z42I4O-0.jpg" />
本文出自 “賀春暘的技術專欄” 部落格,請務必保留此出處http://hcymysql.blog.51cto.com/5223301/1067081