I have so far been unsuccessful in getting any Scheduled Events to fire.
MySQL 5.7.14
Global variable event_scheduler is ON
Processlist shows event_scheduler with a status of "Waiting on empty queue"
This is a Master Server with server_id=2 and is replicated to Slaves.
The following query is used to create a scheduled event to clear out the sessions table (this is verbatim). The query itself works.
CREATE EVENT cleanup_session_data
ON SCHEDULE EVERY 1 DAY
ON COMPLETION PRESERVE
DISABLE ON SLAVE
DO
DELETE FROM session_data WHERE created_at < DATE_SUB(NOW(), INTERVAL 1 DAY);
However the Scheduled event simply does not fire.
Turns out the answer was to remove the
DISABLE ON SLAVE
Either it is my own misunderstanding or this does not work as expected. By not including the the EVENT status is "ENABLED" whereas with it included it is "SLAVESIDE_DISABLED"
The EVENTS now fire as expected. They are also not replicated to the SLAVE.
Related
I Have an OTP table and I want to delete data that is older than 5 minutes automatically
Then how could I make trigger or procedure for that?
You must use according Event Scheduler procedure.
CREATE EVENT remove_old_rows
ON SCHEDULE
EVERY 10 SECOND
COMMENT 'Delete the rows that are older than 5 minutes from OTP table.'
DO
DELETE
FROM OTP_database.OTP_table
WHERE created_at < CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Do not forget to enable Event Scheduler.
Don't do it! Just create view to get the most recent data:
create view v_otp as
select otp.*
from otp
where otp.created_at >= now() - interval 5 minute;
Anyone who uses the view only sees the most recent data.
Then you can leisurely delete old data during a period when the database is not busy.
An added benefit is that this is always accurate. If an event or job gets delayed, then your users might see old data. Further, this does not involve complicated locking and transaction semantics when the server is busy.
How to check event scheduler status in MySQL database?
I want to know the running status of my event whether it is running or not?
how to check event scheduler status
It can be ON or OFF (TRUE/FALSE, 1/0) and can be viewed by
SELECT ##GLOBAL.event_scheduler;
-- or
SELECT ##event_scheduler;
for example.
i want to know the running status of my event whether it is running or not
This is absolutely another task. Event scheduler != Event.
Look at
SHOW PROCESSLIST;
for your event executing thread row is present or not.
I am trying to use mysql event schedule in my application, I have not use it before so i have some confusions.
I want to know if my computer is off on the schedule date, then schedule will continue on next day, after starting my computer?
Like:
my schduled is for beginning at every month (no predefined time set)
if in the above date my computer/Server is off,
will mysql continue scheduled event in next day after turning on my computer/server?
If no, then please suggest a solution.
Hmmmm, have you looked at something like this?
MySQL: Using the Event Scheduler
... or:
How to create MySQL Events
... or even: [MySQL :: MySQL 5.1 Reference Manual: 19.4.1. Event Scheduler Overview](19.4.1. Event Scheduler Overview)?
Also please keep in mind that SQL DBMS servers are written with the rather strong presumption that they will be kept up and operating 24 hours per day with only brief periods of downtime for maintenance or repairs. There is generally very little consideration for operation on machines which are shutdown at night and while not in use.
If you simply store a table of dates and events then your can simply query that table for events which have passed or are upcoming within any range you like ... and you can run the program(s) containing those queries (and performing any appropriate activities based on the results) whenever you start you computer and periodically while it's up and running.
These links refer to a feature of MySQL which is designed to have the server internally execute certain commands (MySQL internal commands, such as re-indexing, creating/updating views, cleaning tables of data which "expires" and so on. I don't know if a MySQL server would attempt to execute all events which have passed during downtime, though it should only be a little bit of work to follow the tutorial, schedule some event for some time (say 15 minutes after the time you expect to hit [Enter]) ... then shutdown your computer (or even just the MySQL server) and go off to lunch. Then come back, start it up and see what happens.
The scheduled event could be something absurdly simple, like inserting the "current" time into some table you set up.
How to create trigger that fires automatically when server date will change.
What do you mean by 'date change'. Do you mean an event that should be triggered every midinight? Use event scheduler for that.
If you mean 'whenever someone manually changes server data' - there is no reliable way to catch that in MySQL. You could have an event in event scheduler monitoring the date for changes, but I think it would be wasteful.
Set cron job.
Example for midnight every 1st and 15th of the month.
0 0 1,15 * * /scripts/phpscript.php
I've googled around and searched the MYSQL docs ad nauseam and couldn't find a succinct way of automating deletion of records that exceeded a given timeframe. I've been able to get a query in 5.1 to cast a value of TIMESTAMP to DATETIME within a DIFF function with the current time to see if it meets the criteria of expiration. I've read that 5.1 now has the capability of running scheduled tasks but not much in the way of configuring it. I'm not using triggers for this.
In the MySQL docs for 5.1, it refers to creating an event:
'CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO sql_statement;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
I currently use Toad (which has been a Godsend). My query affectively removes any records that are more than 30 minutes old. I just need to find how this event gets invoked...
Thanks!
You are talking about using the MySQL Scheduler. Once you create that event, MySQL will call it automatically at whatever interval you configure it with. If you are having trouble getting it set up, post the query and error your are getting.
Write a query and have it ran on a job every so often. Say, check for the expired rows every 30 minutes or so.
If it doesn't have to be exact, and you're just housekeeping, you can tie the process to another one. If you can afford the time.
If you have an old invoice file, purge it when month-end is run (possibly a lot of records, but it's a batch process anyway). Purge old inventory items when you add new ones (less frequent, but fewer records possibly). Keeping an access log table? Purge it when the most recent record in it falls on a different day than today. (for low traffic logfiles) And so on.