Procedure of the MySQL event Scheduler

Source: Internet
Author: User
The following article mainly introduces the MySQL event scheduler (eventsched). The event scheduler we tested this time was executed in the MySQL environment, and it adds another related function, it can be used as a new scheduled task scheduler. This replaces some timer functions that can only be completed by the operating system task scheduler. I. Overview

The following article mainly introduces the MySQL Event Scheduler. The Event Scheduler we tested this time is performed in the MySQL 5.1 environment, and it adds another related function, it can be used as a new scheduled task scheduler. This replaces some timer functions that can only be completed by the operating system task scheduler. I. Overview

The following article mainly introduces the MySQL Event Scheduler. The Event Scheduler we tested this time is performed in the MySQL 5.1 environment, and it adds another related function, it can be used as a new scheduled task scheduler.

This replaces some timer functions that can only be completed by the operating system task scheduler.
I. Overview

The event scheduler is another feature added in MySQL 5.1. It can be used as a scheduled task scheduler to replace some timer tasks that can only be completed by the operating system task scheduler>. For example, crontabe in Linux can be executed only once per minute, while the MySQL event scheduler can execute one task per second, this is very practical in some environments that require higher real-time performance.

The event scheduler is scheduled to trigger execution, which can also be called a "temporary trigger ". The trigger only executes some statements for the events generated by a table, while the event scheduler executes some statements at a certain interval.

Events are managed by a specific thread, that is, the so-called "event scheduler ". After the MySQL event scheduler is enabled, the SUPER account executes show processlist to view the thread. You can dynamically control whether the event scheduler is enabled by setting the global variable event_scheduler.

 
 
  1. (root:localhost:)test> SET GLOBAL event_scheduler = ON;
  2. (root:localhost:)test> show processlist\G

4. row

 
 
  1. Id: 46147
  2. User: event_scheduler
  3. Host: localhost
  4. db: NULL
  5. Command: Daemon
  6. Time: 1
  7. State: Waiting on empty queue
  8. Info: NULL


As shown above, the thread owner is event_scheduler.

II. Application Cases

Implement the MySQL event scheduler. In this case, the storage process is called every second to determine whether the SLAVE is running normally. If the SLAVE is disabled and zero errors are ignored, then restart SLAVE.

First, create a stored procedure

 
 
  1. delimiter //
  2. create procedure `Slave_Monitor`()
  3. begin
  4. SELECT VARIABLE_VALUE INTO @SLAVE_STATUS
  5. FROM information_schema.GLOBAL_STATUS
  6. WHERE VARIABLE_NAME='SLAVE_RUNNING';
  7. IF ('ON' != @SLAVE_STATUS) THEN
  8. SET GLOBAL SQL_SLAVE_SKIP_COUNTER=0;
  9. SLAVE START;
  10. END IF;
  11. end; //
  12. delimiter ;

Because the statement like show slave status cannot be called in the stored procedure, the exact copy error message and Error Code cannot be obtained, and the various conditions of SLAVE stop cannot be further processed.

Next, create a task.

 
 
  1. CREATE EVENT IF NOT EXISTS `Slave_Monitor`
  2. ON SCHEDULE EVERY 5 SECOND
  3. ON COMPLETION PRESERVE
  4. DO
  5. CALL Slave_Monitor();

A task is created and executed every five seconds. After the task is completed, the task is retained instead of deleted. Of course, the task in this example will not end unless it is manually disabled.

If you want to temporarily close a task during running, execute the alter event statement:

 
 
  1. (root:localhost:)test> alter event `Slave_Monitor` ON
  2. COMPLETION PRESERVE DISABLE;
  3. (root:localhost:)test> alter event `Slave_Monitor` ON
  4. COMPLETION PRESERVE ENABLE;

The above content is an introduction to the MySQL event scheduler. I hope you will get some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.