mysql event runs but doesnt execute the query - mysql

I used to have an archiving event that used to run once everyday without a problem but then recently when I checked, I found out that my event still runs but the query wasn't executed. So I dropped the old event and recreated it and only changed the name and the execution time but now it says there's something wrong with the syntax when I use a mysql syntax checker but it runs using php the only problem is it executes the 2nd query which is delete instead of the 1st first query which is insert. The event looks like this:
CREATE DEFINER=`ricoj`#`%` EVENT `log_archiving` ON SCHEDULE EVERY 1 DAY STARTS
'2016-06-29 12:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
insert into archives
select * from app_log where date_sub(curdate( ) , interval 75 DAY) >= created_datetime;
delete from app_log where date_sub(curdate( ) , interval 75 DAY) >= created_datetime;
END

Related

MySQL Recurring event not executing

I have a recurring event set to execute each 1 minute in MySQL:
BEGIN
DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 1 MINUTE);
END
For some reason, my session entries, such as one with the date parameter of
2019-01-19 18:28:24
are not being deleted, despite the fact that SELECT NOW(); currently returns
2019-01-19 18:44:15
So this is more than a minute after the creation date and proves that timezones aren't the problem. Is my event simply failing to execute or is there a problem with the event itself?
EDIT: Please note that the event is set to execute each minute AND delete session entries that are more than a minute old, not just one or the other. The recurring interval isn't shown above, but I've confirmed that it is 1 minute.
I had to run
SET GLOBAL event_scheduler = on;
in order to fix the issue. I had rebooted my XAMPP local server since the last time I tested this, which is why it suddenly stopped working.

How can I cyclically remove entries from my database?

I have a table used to store some entries with a date, which I put in a "datetime" typed field.
I'd like to automatically move those entries at that time to another table.
The result would be having a table with all the passed entries, and one with the future ones.
I was thinking about using a stored procedure, but I wouldn't know how to trigger them when I want.
Or maybe I need a standalone application always running on my server.
Or maybe I'm just not thinking to the right, banal solution...
MySQL offers events, which are like stored procedures but also run at a particular time or on a particular schedule.
The basic sequence of queries to do what you want is this.
START TRANSACTION;
SET #time := NOW() - INTERVAL 24 HOUR;
INSERT INTO archive_log
SELECT *
FROM live_log
WHERE timestamp <= #time;
DELETE FROM live_log WHERE timestamp <= #time;
COMMIT;
You'll run this as often as you need to. Each time you run it you'll move old records from the live_log table to the archive_log table. The phrase - INTERVAL 24 HOUR governs how old the records must be to move them.
You can run this as a MySQL event. For this to work event scheduling must be on in your MySQL server. Read this. https://dev.mysql.com/doc/refman/5.6/en/events-configuration.html
Here's how you declare this event.
CREATE EVENT `archiver`
ON SCHEDULE EVERY 4 HOUR
STARTS '2015-05-23 15:11:23'
ON COMPLETION PRESERVE DO
BEGIN
START TRANSACTION;
SET #time := NOW() - INTERVAL 24 HOUR;
INSERT INTO archive_log
SELECT *
FROM live_log
WHERE timestamp <= #time;
DELETE FROM live_log WHERE timestamp <= #time;
COMMIT;
END
Notice that the query is embedded in the CREATE EVENT code. Debug the query first, before you put it into the event, because debugging events is hard.

phpMyAdmin event error #1542 interval too big

I have an MySQL event that deletes any rows older than 1 month. I now want to change that so it deletes rows older than 6 months (it cleans up a database for messages sent in a game).
My (edited) MySQL event is:
DELETE FROM messages WHERE date < (NOW() - INTERVAL 6 MONTH)
The error I get is:
The following query has failed: "CREATE DEFINER=`admin`#`localhost` EVENT `delete msgs` ON SCHEDULE EVERY 0 DAY_HOUR STARTS '2013-07-19 18:00:00' ON COMPLETION PRESERVE ENABLE DO DELETE FROM messages WHERE date < (NOW() - INTERVAL 6 MONTH)"
MySQL said: #1542 - INTERVAL is either not positive or too big
The event functions fine but I can't even save it as it is now. If I open the event and immediately save I get the same error.
Juergen d basically answered my question in a comment. The interval was set to "EVERY 0 DAY_HOUR" while in phpmyadmin it was set to "EVERY '0_10' DAY_HOUR". I was able to save it before with the '0_10' but apparently that doesn't work anymore, or maybe never worked (even though the event did it's thing).
I changed that to "EVERY 10 HOUR" and now it saves fine.

How to delete a MySQL record after a certain time

I want to delete some messages from my MySQL database after 7 days.
My message table rows have this format:
id | message | date
The date is a timestamp in the normal format; 2012-12-29 17:14:53
I was thinking that an MySQL event would be the way to go instead of a cron job.
I have what I guess is a simple question to an experienced SQL person, how do I code the delete messages portion in brackets below?
An example would be appreciated, Thanks.
DELIMITER $$
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO
BEGIN
DELETE messages WHERE date >= (the current date - 7 days);
END;
$$;
You can try using this condition:
WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY)
So that the whole SQL script looks like this:
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE messages WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY);
END;
However, on your place I would solve the given problem with a simple cron script. The reasons to do this is simple: it's easier to maintain the code, no ugly SQL workarounds, integrates smoothly with your system.
This should do the trick.
DELETE FROM messages WHERE date < (CURDATE() - INTERVAL 7 DAY);
For those out there who are on a shared hosting, like 1and1's, and can't create events, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want

MySQL Event Scheduler on a specific time everyday

Here's my query
CREATE EVENT
RESET ON SCHEDULE AT TIMESTAMP DO
UPDATE `ndic`.`students`
SET `status` = '0';
How can I update status to "0" at 1 pm every day.
What can I use instead of TIMESTAMP?
This might be too late for your work, but here is how I did it. I want something run everyday at 1AM - I believe this is similar to what you are doing. Here is how I did it:
CREATE EVENT event_name
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
# Your awesome query
The documentation on CREATE EVENT is quite good, but it takes a while to get it right.
You have two problems, first, making the event recur, second, making it run at 13:00 daily.
This example creates a recurring event.
CREATE EVENT e_hourly
ON SCHEDULE
EVERY 1 HOUR
COMMENT 'Clears out sessions table each hour.'
DO
DELETE FROM site_activity.sessions;
When in the command-line MySQL client, you can:
SHOW EVENTS;
This lists each event with its metadata, like if it should run once only, or be recurring.
The second problem: pointing the recurring event to a specific schedule item.
By trying out different kinds of expression, we can come up with something like:
CREATE EVENT IF NOT EXISTS `session_cleaner_event`
ON SCHEDULE
EVERY '1 13' DAY_HOUR
COMMENT 'Clean up sessions at 13:00 daily!'
DO
DELETE FROM site_activity.sessions;
Update, long over-due: The interval with the unit "DAY_HOUR" takes an expression that looks like 'day-number hour-number'. I have updated the oft-accepted solution to match that. Thanks, commenters for clarifying!
Try this
CREATE EVENT event1
ON SCHEDULE EVERY '1' DAY
STARTS '2012-04-17 13:00:00' -- should be in the future
DO
-- your statements
END
DROP EVENT IF EXISTS xxxEVENTxxx;
CREATE EVENT xxxEVENTxxx
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
--process;
¡IMPORTANT!->
SET GLOBAL event_scheduler = ON;
My use case is similar, except that I want a log cleanup event to run at 2am every night. As I said in the comment above, the DAY_HOUR doesn't work for me. In my case I don't really mind potentially missing the first day (and, given it is to run at 2am then 2am tomorrow is almost always the next 2am) so I use:
CREATE EVENT applog_clean_event
ON SCHEDULE
EVERY 1 DAY
STARTS str_to_date( date_format(now(), '%Y%m%d 0200'), '%Y%m%d %H%i' ) + INTERVAL 1 DAY
COMMENT 'Test'
DO
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL recurring Event',NOW());