MySQL定时任务event_MySQL
bitsCN.com
MySQL定时任务event
由于一些业务需求,我们可能需要定时清除数据库一些废弃的数据,可以使用mysql的存储过程和事件来完成。
下面例子定时清除日志表log中指定天数前的数据
1、创建日志表log
CREATE TABLE IF NOT EXISTS `log` ( `log_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '记录id', `user_id` int(11) DEFAULT NULL COMMENT '用户id', `op` varchar(128) NOT NULL COMMENT '操作类型', `model` varchar(32) DEFAULT NULL COMMENT '操作模块', `activity_time` int(10) NOT NULL DEFAULT '0' COMMENT '操作时间', `data` text COMMENT '数据', PRIMARY KEY (`log_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='用户日志表' AUTO_INCREMENT=1 ;
2、创建事件e_del_logs
CREATE EVENT `e_del_logs` ON SCHEDULE EVERY 1 DAY STARTS '2013-07-30 17:33:43' ON COMPLETION NOT PRESERVE ENABLE DO call p_del_logs(90);
//上面代码表示从2013-07-30 17:33:43起每一天执行一次p_del_logs这个存储过程,并带上参数
3、建立存储过程
p_del_logsDELIMITER $$---- 存储过程--CREATE PROCEDURE `p_del_logs`(IN `date_inter` int)BEGIN delete from log where (to_days(now()) - to_days(FROM_UNIXTIME(activity_time)))>=date_inter;END$$DELIMITER ;
//按事件传过来的参数90,删除操作时间90天之前的数据
这样mysql就会定制每天去执行这个任务了。
查看当前是否已开启事件计划(调度器)有3种方法:
1) SHOW VARIABLES LIKE 'event_scheduler';2) SELECT @@event_scheduler;3) SHOW PROCESSLIST;
(二) 开启事件计划(调度器)开关有4种方法:
1) SET GLOBAL event_scheduler = 1;2) SET @@global.event_scheduler = 1;3) SET GLOBAL event_scheduler = ON;4) SET @@global.event_scheduler = ON;
键值1或者ON表示开启;0或者OFF表示关闭;
(三) 事件开启与关闭:
开启某事件:ALTER EVENT e_del_logs ON COMPLETION PRESERVE ENABLE;关闭某事件:ALTER EVENT e_del_logs ON COMPLETION PRESERVE DISABLE;
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

PHP...
