每小时清空一个MySQL表 [英] Empty a MySQL table hourly
问题描述
我有一个用PHP Ajax和MySQL编写的聊天脚本.它将消息记录在表"usermsg"上.
I have a chat script written on PHP Ajax and MySQL. It keeps the message logged on a table "usermsg".
我的问题是我只有一个100mb的MySQL数据库,并且我无法保留所有消息,因为我有55个用户,并且数据库配额不足以存储所有消息,当聊天记录满时,我的聊天会自动停止. 因此,每当我觉得它快满了时,我都会从PhpMyAdmin执行truncat. 我想做的是以某种方式每小时执行一次truncat,以便每小时可以清除一次我的表.
My problem is that I have only a MySQL db of 100mb and I can't keep all the messages, as I have 55 users and my db quota is not enough to stock all and when it's full my chat stops automatically. So I perform truncat from PhpMyAdmin each time I feel that it is almost full. What I want to do is somehow perform truncat hourly so it can clear my table every hour.
有人可以提供有关PHP脚本的帮助吗?谢谢.
Can somebody help with a PHP script or something to do that? Thanks.
推荐答案
使用cronjobs每小时执行一次php或使用mysql事件来完成此任务
Use cronjobs to execute the php for an hourly or use mysql events to achieve this task
方法1:
创建一个php并为其执行cron作业
create a php and do cron jobs for it
<?php
// write database connection code
mysqli_query("TRUNCATE TABLE tablename");
?>
并在cron中调用此php
and call this php in cron
php /path/to php file
方法2:
创建mysql EVENT
.授予对表
create mysql EVENT
. give privilege to accesss event for table
SET GLOBAL EVENT_SCHEDULER = ON;
CREATE EVENT e_truncate
ON SCHEDULE
EVERY 1 HOUR
DO
TRUNCATE TABLE tablename
这篇关于每小时清空一个MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!