- After connecting and logging in to MySQL, first check whether MySQL has enabled the event function:
命令:show variables like '%sc%';
It is found that event_sheduler is OFF;
2. Open event_scheuler:
- Temporarily open (after the mysql service is restarted, it will fail)
- SET GLOBAL event_scheduler = ON;
SET GLOBAL event_scheduler = 1; — 0 means off
permanently on
Add the following to the [mysqld] section in my.cnf, and then restart mysql (mysql restart command: service mysqld restart)
event_scheduler=ON
3. Create an event. Here is an example to periodically delete data that is expired for 2 minutes in the wififlows table every 5 seconds :
create event e_delete_wififlows on schedule every 5 second do delete from wififlows where timestamp < (CURRENT_TIMESTAMP() + INTERVAL -2 MINUTE);
If this event exists beforehand, it can be deleted with the following command:
drop event if exists e_delete_wififlows;
4. Start the event:
alter event e_del_wififlows on completion preserve enable;
5. Close the event:
alter event e_del_wififlowa on completion preserve disable;