Get the App
SLTechnology News&Howtos  ›  Database  › 

A simple method for mysql to delete expired data records at regular intervals

Shulou Source: shulou.com Published: 2022-06-01 03:17:50 11月14日 Update

1. After connecting and logging in to MySQL, check whether MySQL has enabled the event feature:

Command: show variables like'% sc%'

It was found that event_sheduler was closed by OFF.

two。 Open event_scheuler:

Temporarily start (expire after the mysql service restarts)

SET GLOBAL event_scheduler = ON; SET GLOBAL event_scheduler = 1;-0 means off

Permanently open

Add the following to the [mysqld] section of my.cnf, and then restart mysql (mysql restart command: service mysqld restart)

Event_scheduler=ON

3. Create an event. Here is an example of regularly deleting data in the wififlows table that has expired for 2 minutes 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 in advance, you can delete it using the following command:

Drop event if exists e_delete_wififlows

Then use show events; to view the events that exist

4. Enable event:

Alter event e_del_wififlows on completion preserve enable

5. Shutdown event:

Alter event e_del_wififlowa on completion preserve disable

The above mysql regularly delete expired data records of the simple way is the editor to share with you all the content, I hope to give you a reference, but also hope that you support more.

Tags: Event command data content method representative function that is part permanent reference support service login Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Docker OPPO Reno macOS