A simple method to regularly delete expired data records in MySQL

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, first check whether the event function is enabled in MySQL:

Command: show variables like '%sc%';

It is found that event_sheduler is OFF;

2. Open event_scheuler:

Temporarily enabled (invalid after restarting MySQL service)

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

Permanently open

Add the following content 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 delete data in the wififlows table that is 2 minutes old and expired every 5 seconds:

create event e_delete_wififlows on schedule every 5 seconds do delete from wififlows where timestamp < (CURRENT_TIMESTAMP() + INTERVAL -2 MINUTE);


If this event already exists, you can delete it using the following command:

drop event if exists e_delete_wififlows;


Then use show events; to view the existing events

4. Open the event:

alter event e_del_wififlows on completion preserve enable;


5. Close event:

alter event e_del_wififlowa on completion preserve disable;


The above simple method of regularly deleting expired data records in MySQL is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of several practical solutions for quickly deleting large amounts of data (tens of millions) in MySQL
  • MySQL's method of dealing with duplicate data (preventing and deleting)
  • MySQL database operations (create, select, delete)
  • MySQL uses mysqldump+binlog to completely restore the deleted database principle analysis
  • Analysis of common basic operations of MySQL database [create, view, modify and delete database]
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • A quick solution to accidentally delete MySQL data (MySQL Flashback Tool)
  • MySQL Binlog Data Recovery: Detailed Explanation of Accidentally Deleting a Database
  • How to quickly delete all tables in MySQL without deleting the database
  • Two ways to delete a MySQL database
  • Why the table file size remains unchanged after deleting data in MySQL

<<:  Basic usage and examples of yum (recommended)

>>:  Regarding the problem of using webpack instructions in vscode showing "Because running scripts is prohibited in this system" (perfect solution)

Recommend

How to install and persist the postgresql database in docker

Skip the Docker installation steps 1. Pull the po...

MySQL table field time setting default value

Application Scenario In the data table, the appli...

How to build a drag and drop plugin using vue custom directives

We all know the drag-and-drop feature of HTML5, w...

How to hide the version number in Nginx

Nginx hides version number In a production enviro...

Detailed explanation of the integer data type tinyint in MySQL

Table of contents 1.1Tinyint Type Description 1.2...

Next.js Getting Started Tutorial

Table of contents Introduction Create a Next.js p...

The difference between delete, truncate, and drop and how to choose

Preface Last week, a colleague asked me: "Br...

Realize super cool water light effect based on canvas

This article example shares with you the specific...

Summary of js execution context and scope

Table of contents Preface text 1. Concepts relate...

Summary of Linux command methods to view used commands

There are many commands used in the system, so ho...

Explain the difference between iframe and frame in HTML with examples

I don't know if you have used the frameset at...

Summary of common commands in Dockerfile

Syntax composition: 1 Annotation information 2 Co...

How to build YUM in Centos7 environment

1. Enter the configuration file of the yum source...

Do you know the meaning of special symbols in URL?

1.# # represents a location in a web page. The ch...