MySQL Event Scheduler

Check Events

show events from database_name;

To show more detail about a specific event...

show create event event_name\G

To show last run time etc...

SELECT *

  FROM INFORMATION_SCHEMA.events;

Create Events

Example - Run myprocedure every minute for 1 hour...

CREATE EVENT event_name 

ON SCHEDULE EVERY 1 MINUTE 

STARTS CURRENT_TIMESTAMP 

ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR 

DO

 call myprocedure(); 

Example - Run myprocedure every day from 20-Jan-2020 to 20-Jan-2030 at 5am...

CREATE EVENT event_name 

ON SCHEDULE EVERY 1 DAY 

STARTS '2020-01-20 05:00:00' 

ENDS '2030-01-20 05:00:00' 

DO 

  call myprocedure();

Example - Run myprocedure once starting after 1 minute...

CREATE EVENT event_name

ON SCHEDULE AT CURRENT_TIMESTAMP + 1 MINUTE

DO

   call myprocedure();

ALTER EVENT event_name ENABLE;

Alter Events

ALTER EVENT event_name

ON SCHEDULE AT CURRENT_TIMESTAMP + 1 MINUTE

DO

   call myprocedure();

ALTER EVENT event_name ENABLE;

Enable Events

If an Event shows as DISABLED you can ENABLE it using this SQL...

ALTER EVENT event_name ENABLE;

Disable Events

If an Event shows as ENABLED you can DISABLE (i.e. prevent it from running) it using this SQL...

ALTER EVENT event_name DISABLE;

Drop Events

DROP EVENT IF EXIST event_name;

Enable Event Scheduler

If there is no event_scheduler process...

SHOW PROCESSLIST;

or...

SELECT id,

       user,

       host,

       db,

       command,

       time,

       state,

       info

  FROM information_schema.processlist

 WHERE user = 'event_scheduler'; 

...then you can start it using...

SET GLOBAL event_scheduler = ON;

Disable Event Scheduler

You can disable the event scheduler using...

SET GLOBAL event_scheduler = OFF;

Cancel Running Event

Bibliography