Known as one of the most popular open source database, MySQL is widely used in various scenarios, Alibaba Cloud provides high available ApsaraDB RDS for MySQL with enhanced MySQL service reduced enterprise’s database expenses, and helped enterprises utilize technology to fight against coronavirus.
MySQL Event Scheduler Sample Demo
As we all know, the MySQL event Scheduler is a special feature in MySQL 5.1 that can be used as a timed Task scheduler to replace some of the timing functions that were previously only available with the operating system Task Scheduler. The following articles will provide you with more comprehensive knowledge.
First. Overview
The event Scheduler is another feature added in MySQL 5.1 that can be used as a timed Task scheduler to replace some of the time > Energy that was previously available only with the operating system Task Scheduler. For example, the Crontabe in Linux can only be executed once per minute, while the MySQL event Scheduler implements one task per second, which is useful in some environments where real-time > High-performance is needed.
The event Scheduler is timed to trigger execution, and can also be called a "temporary trigger" at this point in time. A trigger simply executes some statements for the events produced by a table, while the event scheduler executes some statements at a certain (interval) time. Events are managed by a particular thread, the so-called "event Scheduler". When event Scheduler is enabled, accounts with SUPER privileges can see this thread by executing SHOW processlist. You can dynamically control whether the MySQL event scheduler is enabled by setting the value of the global variable Event_scheduler.
- (root:localhost:) test> SET GLOBAL event_scheduler = on ;
- (root:localhost:) test> Show Processlist\g
Row
- id:46147
- User:event_scheduler
- Host:localhost
- Db:null
- Command:daemon
- Time:1
- State:waiting on Empty queue
- Info:null
As above, the owner of the thread is Event_scheduler.
Second, the MySQL event Scheduler application case
In this case, the event scheduler is used to invoke a stored procedure every second to determine if the SLAVE is functioning properly, if SLAVE is turned off, 0 errors are ignored, and SLAVE is restarted.
Create a stored procedure first
- Delimiter//
- CREATE PROCEDURE ' Slave_monitor ' ()
- Begin
- SELECT Variable_value into @SLAVE_STATUS
- From INFORMATION_SCHEMA. Global_status
- WHERE variable_name=' slave_running ';
- IF (' on '! = @SLAVE_STATUS) Then
- SET GLOBAL sql_slave_skip_counter=0;
- SLAVE START;
- END IF;
- End //
- delimiter;
Because statements like SHOW SLAVE STATUS cannot be called in a stored procedure, exact replication error messages and error codes cannot be obtained and further processing of SLAVE stops is not possible.
Next, create the task
- CREATE EVENT IF not EXISTS ' Slave_monitor '
- On SCHEDULE every 5 SECOND
- On completion PRESERVE
- Do
- Call Slave_monitor ();
A task is created, executed every 5 seconds, and the task is retained after the task is completed, rather than deleted. Of course, the task in this example will not end unless it is manually disabled.
If you want to temporarily close a task while running, execute the ALTER EVENT statement:
- (root:localhost:) test> Alter event ' Slave_monitor ' on
- Completion PRESERVE DISABLE;
- (root:localhost:) test> Alter event ' Slave_monitor ' on
- Completion PRESERVE ENABLE;
The above related content is to the MySQL event Scheduler (Scheduler) Introduction, hope you can gain
Original blog:http://www.codesky.net/article/201006/147600.html
mysql-status waiting on empty queue extension