MySQL Database set timed tasks

Source: Internet
Author: User

Recently on hand to do an auction of e-commerce projects.

In the middle you need to set the auction state of the point to in progress.

our solution is to Mysql Timer task, here is a simple summary.

1. Scope of Use

not all of them. MySQL version is supported,MySQL 5.1 only after the new features that start to support

2. syntax related to MySQL

# Query MySQL Scheduled task support is turned on

Show variables like '%sche% ';

# Turn on MySQL Scheduled task support

SET GLOBAL event_scheduler = on;

SET @ @global. Event_scheduler = on;

SET GLOBAL event_scheduler = 1;

SET @ @global. Event_scheduler = 1;

# Turn off MySQL Scheduled Tasks support

SET GLOBAL event_scheduler = OFF;

SET @ @global. Event_scheduler = OFF;

SET GLOBAL event_scheduler = 0;

SET @ @global. Event_scheduler = 0;

# If a scheduled task exists , Delete the scheduled task

DROP EVENT IF EXISTS Testinsert;

# Create a scheduled task

CREATE EVENT IF not EXISTS Testinsert

On SCHEDULE every 3 SECOND

On completion PRESERVE

Do INSERT into AA (' name ', sex) VALUES (' AA ', 2), (' BB ', 1);

# Open a scheduled task

ALTER EVENT Testinsert ENABLE;

# Close Scheduled Tasks

ALTER EVENT Testinsert DISABLE;

# Query MySQL Scheduled task support is turned on

Show variables like '%sche% ';

# Turn on MySQL Scheduled task support

SET GLOBAL event_scheduler = on;

SET @ @global. Event_scheduler = on;

SET GLOBAL event_scheduler = 1;

SET @ @global. Event_scheduler = 1;

# Turn off MySQL Scheduled Tasks support

SET GLOBAL event_scheduler = OFF;

SET @ @global. Event_scheduler = OFF;

SET GLOBAL event_scheduler = 0;

SET @ @global. Event_scheduler = 0;

# If a scheduled task exists , Delete the scheduled task

DROP EVENT IF EXISTS Testinsert;

# Create a scheduled task

CREATE EVENT IF not EXISTS Testinsert

On SCHEDULE every 3 SECOND

On completion PRESERVE

Do INSERT into AA (' name ', sex) VALUES (' AA ', 2), (' BB ', 1);

# Open a scheduled task

ALTER EVENT Testinsert ENABLE;

# Close Scheduled Tasks

ALTER EVENT Testinsert DISABLE;

Finally, give a complete test case.

Mysqltimers are provided by the systemEvent, create the table below:
CREATE TABLE MyTable (
ID int auto_increment NOT NULL,
Name varchar (+) NOT null default ' ',
Introduce text NOT NULL,
Createtime timestamp NOT NULL,
Constraint Pk_mytable primary key (ID)
)


Create stored procedures, where the stored procedures are primarily provided toMysqlthe timerEventto invoke to execute:
CREATE PROCEDURE Mypro ()
BEGIN
Insert INTO MyTable (name,introduce,createtime) VALUES (' 1111 ', ' Inner Mongolia ', now ());
End
here is just a simple write-up, just to illustrate the example.


immediately after creatingMysqlthe timerEvent:
Create event if not exists eventjob
On schedule every 1 second
On completion PRESERVE
Do call Mypro ();
This is set to execute once per second


At this point all the preparations have been finished and done,MysqlTo make use of the timer must be done to prepare the work, is toMySQLtimer is turned on:
SET GLOBAL event_scheduler = 1; --Start Timer
SET GLOBAL event_scheduler = 0; --Stop Timer


The event is also opened immediately:
ALTER EVENT eventjob on completion PRESERVE ENABLE; --Turn on Events
ALTER EVENT eventjob on completion PRESERVE DISABLE; --Close Event


SHOW VARIABLES like '%sche% '; --View timer Status


at this point, you go to the table inside the databaseMyTablelook inside, the system will be every second to insert a piece of data, hehe, the task is completed.
SELECT * FROM MyTable

MySQL Database set timed tasks

Related Article

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.