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

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

问题描述

我想清除我的SQL数据库从所有年龄超过1周,我想每晚做。所以,我要设置一个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中的查询如下:

The query in PHP is as follows:

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

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

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