将 mySQL 查询作为 cron 作业运行? [英] Run a mySQL query as a cron job?

查看:39
本文介绍了将 mySQL 查询作为 cron 作业运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从超过 1 周的所有数据中清除我的 SQL 数据库,并且我想每晚执行一次.所以,我要建立一个 cron 工作.不用每次都手动输入密码,如何查询mySQL?

I would like to purge my SQL database from all entires older than 1 week, and I'd like to do it nightly. So, I'm going to set up a cron job. How do I query mySQL without having to enter my password manually every time?

PHP中的查询如下:

mysql_query("DELETE FROM tbl_message WHERE DATEDIFF( NOW( ) ,  timestamp ) >=7");

有没有办法将它作为 shell 脚本运行?如果没有,有没有办法让cron运行一个php文件?

Is there a way to run this as a shell script? If not, is there a way of making cron run a php file?

推荐答案

尝试创建如下所示的 shell 脚本:

Try creating a shell script like the one below:

#!/bin/bash

mysql --user=[username] --password=[password] --database=[db name] --execute="DELETE FROM tbl_message WHERE DATEDIFF( NOW( ) ,  timestamp ) >=7"

然后您可以将其添加到 cron

You can then add this to the cron

这篇关于将 mySQL 查询作为 cron 作业运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆