How to delete expired data with cron job - mysql

I want to auto delete my data on mysql database which has expired date with cron job every 5 minutes.
For example I have a table (TableName) with one of column (ExpDate) as "2015-04-10 21:30:00", it's timestamp format type and UTC time zone.
I have tried creat a cron job command like this:
*/5 * * * * delete * from TableName where ExpDate > NOW();
But it didn't work, I've searched around in SO and elsewhere but have not found a direct answer to this. So, I created this question and hope someone can help me. I am a beginner in cron job, I would be very grateful if you could help me.

Cron doesn't know about SQL commands. Cron knows about running executable commands. This leads us to the answer - you need to either directly call the MySQL command line client in the crontab (probably a bad idea), or you need to write a script that does what you want, and then get cron to execute it.
A sample script would be:
USERNAME=[username]
PASSWORD=[password]
DATABASE=[database]
MYSQL=/usr/bin/mysql
$MYSQL -u $USERNAME -p$PASSWORD $DATABASE -e "DELETE from ....."
You would put this script somewhere safe, because it contains your password. Let's say you called it purge_expired.sh, you would need to make it executable like this chmod +x purge_expired.sh, and then tell cron to execute that script instead of trying to make it run the SQL commands itself.
Somehow I missed the memo where MySQL got its own event scheduler, you might want to use that instead.
An example specific to you would be something like:
CREATE EVENT DeleteExpired
ON SCHEDULE EVERY 5 MINUTE
DO
DELETE FROM TableName WHERE ExpDate < NOW();
You also need to make sure the event_scheduler variable is set to on in order for this to run.

Related

How to keep track of last successful run in bash

I am running an ETL script that loads data from mysql into teradata. The script aims to select all rows later than the timestamp of the last successful run of the bash script. Since I do not have write access to the mysql database, I need to store the last run timestamp with the bash script. Is there an easy way to store the timestamp of a successful run? I was thinking I could have a file that I would touch at the end of the script and then check its mtime, or just parse out the timestamp from a log file. What are some better strategies to do this?
Within your script, use set -e1 so that the script exits immediately if any command within the script fails. Then, at the end, log successful completion with a unix timestamp date +%s.
You can then use SELECT FROM_UNIXTIME(<YOUR TIMESTAMP>, <YOUR MYSQL DATE FORMAT>)2 to pull rows that are newer than the last successful completion.
One big caveat: I would not rely solely on timestamps to approach this problem. I would pull from MySQL with some time overlap and check primary keys for each insert into teradata to avoid inserting duplicates. To follow this approach, just subtract 1800 from <YOUR TIMESTAMP> to ensure a 30 minute overlap.

Execute script when date matches mysql cell

I have a debian machine with a mysql server.
On mysql I have a table that contains a number of rows with a datetime field.
How can I execute a php script when the date and time of the machine match with those specified in any mysql record?
a) Mysql triggers ?
b) Deamon that runs in background and checks time every n seconds ?
c) Cron?
Let me know!
Polling the MySQL will work but surely is not efficient, especially when there are a lot of "triggers".
So use a cronjob. Note: MySQL-Queries cannot schedule or cancel cronjobs. So the (i guess) PHP-Script updating that date field also has to schedule the cronjobs. Also, every time the column changes you will have to cancel the previously scheduled cronjob and schedule a new one.
Since you can't run shell command from within Mysql database the MySql Triggers are no good. The MySQL Scheduler is therefore useless too. So you need an external script to help.
I'd suggest to create a (php) script and add it to the crontab to run every minute. Its task would be to check for matching dates in the database and run whatever command you need.
I think that the easiest solution is to use , if possible , the at command.
Basically after inserting the data into mysql do the following:
1)Create (using PHP) a BASH script that runs the php file
script_1.sh should look like this:
!#/bin/bash
php /path/to/file.php
and make it executable with :
exec("chmod +x script_1.sh");
2)create a second BASH script that executes the first at the desired time:
script_2.sh should look like this:
!#/bin/bash
at H:M Y:M:D < script_1.sh
Make script_2 executable and run it with the exec command.

How can I automate a SQL query via PHPMyadmin?

Because of a buggy wordpress plugin I am using, I have to manually run the following query many times during a single day:
update table_name set column_name="";
It simply drops the contents of the given column name.
So how can I make my server do it automatically every 30 minutes for instance, through PHPMyadmin?
I am not a PHP nor a SQL SAVVY. So please take that into consideration :)
I am on Hostmonster.com , and they have MySQL client version: 5.1.60 and phpMyAdmin v. 3.4.9
If are using UNIX and like you can put the following into your crontab:
30 * * * * "/<path_to_mysql>/mysql -u <username> -p<password> -e "update table set column=\"\""
This will do every 30 minutes what you need to do.
For Windows you can create a Batch file that does the same and execute it using Windows Scheduler
While it is a bit of work to do this every N minutes, it is quite easy to do it statistically every N requests. Put
<?php
define('MYPLUGIN_CLEANUP_PROBABILITY',5);
function myplugin_cleanup_db($postid) {
global $wpdb;
if (rand(0,99)<MYPLUGIN_CLEANUP_PROBABILITY))
$wpdb->query('update table_name set column_name=""');
}
add_action('shutdown', 'myplugin_cleanup_db');
?>
into a .php file and put it into your plugins directory after adapting your cleanup probability. Then activate this plugin and off you go.
To my knowledge, PHPMyAdmin can not automate it directly.
You have a few options:
Create a cron job to run the SQL statement (too advanced?)
Utilize WP Cron to schedule an event to run the SQL statement through WordPress. (possibly easier)
Fix the plugin (my vote, but could be difficult)

Can a MySQL query be run every second?

I like to update my table every second. I know this is possible in AJAX but is this possible in MySQL query? I found MySQL event, can I get solution from this?
" i wanna check condition **if date_time > now() ** then update status as 1 . is this possible"
it does not seems like you need special status to be setup...
this condition can be checked when data is pulled (if need to be marked execute UPDATE and SELECT when pull),
also it can be done as cron job every minute (not sure can be done every second), however if it very related with user being on page - ajax could be the way to do it and downgrade performance at this same time
It is possible to write an sql query that will set an update status equal to 1 when the date value for that record is out of date, however you will still need a scheduled task to run this sql query from time to time.
If you are able to run code on your server, then you should write up a script that periodically runs your query against the database.
so cron is a scheduler in linux to run anything periodically.
So let's say your script that contains "**if date_time > now() ** then update status as 1" is called updateIfOld.php
then you should make crontab runs "php updateIfOld.php" every second.
Here are the manuals to use crontab: http://www.manpagez.com/man/5/crontab/Ta3HK4KosQPd8aT8BQ&usg=AFQjCNErp1Hz19N7xJwVY1wisQNxmtgpQQ&sig2=D4NQu19AJnBZil9J54V8ww
If you could actually tell a bit more about this situation and why u need this to be done, it will help us to give a better solutions.
With assumptions on what you are trying to achieve , we can give the best.
Anyway sending too many ajax and updating query every second is not an good option.
Here is an idea,
if you can store the expiry time for each row and you can set them set status to 1 where your condition matches.
Anyway i think there must be some reason to change the status to 1 (may be for making them not to display/consider).. If we know the exact reason, i think i can give a better solution..

MySQL - Disabling the query output and showing only the total execution time taken

I have a stored procedure which I want to test for speed in a production environment. So I created a new stored procedure which calls this for a 100 times, each time with different parameters.
My question is: how can I disable the output that the MySQL command line prints as I am sure that this adds to the total time.
So, to recap, from the MySQL command line I would like to perform something like the following:
MySQL> call cbtest;
and I want it to display just the total time taken to run the 100 test sp calls (wrapped in cbtest) rather than show me the results returned for each call.
Thanks in advance,
Tim
How something like on the command line:
$ time echo "call cbtest" | mysql -uuser -ppassword database
Keep in mind that MySQL will probably cache your stored proc, so this might not give you a real good feel of performance.