I have an event that deletes record on a table that are a month old. But it does not seem to execute.
DELIMITER $
CREATE EVENT delete_wykofile_back
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 YEAR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM factory.wyko_file_backup WHERE date_inserted < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END$
DELIMITER ;
What I've done so far:
I have followed and set global event_scheduler to ON based from this question
I've also check this question, but to no avail.
There's also no answer from this question.
The account im using is a superuser based from this.
SHOW EVENTS;
SHOW PROCESSLIST;
I am using MySQL Workbench 6.2. I am running the event with 1 minute interval for testing.
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
needs to be the current date or a past date. Right now its set up to start on 12/4/2015.
I'm having my application checking the database for expired members.
I'm using:UPDATE members SET enabled=0 WHERE now() >= time
That runs every 30 seconds. It was first running at 10 seconds but I didn't know if that was an issue so I made it 30 seconds instead. That's the only thing I have running and my timestamp goes from the time I set and sets it to the time the SQL command is executed. Is there something I'm doing wrong?
Isn't this the expected result? You check the timestamp compared to now() (now() is always greater than time ) and you always update the field. The timestamp is updated also that' s why you get the execution time.
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.
I dont understand why my sql return this error when i try to create event..
error: # MySQL returned an empty result (ie no rows).
Code:
USE my_database;
CREATE EVENT IF NOT EXISTS `remove_inactives`
ON SCHEDULE EVERY 15 MINUTE
ON COMPLETION PRESERVE
ENABLE
DO
DELETE FROM `inactive`
WHERE `inactive` < DATE_SUB(NOW(), INTERVAL 15 MINUTE)
;
The problem is likely due to the fact that your where clause is not referring to any columns in your inactive table and therefore
`inactive` < DATE_SUB(NOW(), INTERVAL 15 MINUTE
produces no results.
I am not sure what your column name is, but for sake of argument let's assume it is last_mod_time, in which case this SQL statement might work:
DELETE FROM `inactive`
WHERE `inactive`.last_mod_time < DATE_SUB(NOW(), INTERVAL 15 MINUTE)
If you find that your events are still not being processed, use SHOW PROCESSLIST\G to see if event_scheduler is running.
If not, then use the following to start it for the currently-running mysqld session:
mysql> SET GLOBAL event_scheduler = ON;
To make the setting permanent so the scheduler starts when mysqld starts set the appropriate server system variable
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());