MYSQL, Event not running - mysql

I am trying to run the MYSQL event at a particular time, and to be repeated after 24 hours:
ALTER EVENT myeventToronto1
ON SCHEDULE EVERY 24 HOUR
STARTS '2013-08-04 02:02:00'
COMMENT 'A sample comment.'
DO
INSERT into `smallworksdb`.`eventtesttable`(ID,NAME) Values(1,'someValue');
When I save the above event, the table is not getting populated at a particular time specified in the event, so I am assuming that the event does not get run.
What am I missing here.
Thanx

Why do I have this string feeling that id is declared as a primary key (or at least unique)? And you are trying to insert the same value every day. The problem would be an error generated by SQL, not that the job is not running.
Typically a field like id would be an auto_increment value that would be set automatically on insert, and not given a particular value.
This is just speculation. There are other possibilities like the scheduler is turned off or the computer reboots at exactly that time every day?

Related

Does mysql can give each row a specified lifetime to be deleted automatically?

I'd like to know if mysql(or mariadb) offers a function for the expiration that a row can be removed automatically in the DB without using any extra scheduler program nor using any SQL like DELETE.
This should happen or define when you create a table so that once INSERT occurs it starts to manage it.
There are many related questions here:
MySQL how to make value expire?
Remove Mysql row after specified time
MySQL give rows a lifetime
However, I couldn't find the answer. I am not curious about using WHERE nor DELETE.
Is it even possible?
Yes for same you can create an event by this way
CREATE EVENT lifetime ON SCHEDULE
EVERY 1 DAY STARTS '14:05:44' ENDS '14:05:46'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
// put your delete query here with where clause by calculate your exp date
END

MySql event date limitation?

am trying to generate an event that delete a table in a DB
my idea is to make that to run every (saying hypotetically) 10 min for ever ( no end date)
but I get an error as following:
The starts and ends parameters of the create event command are timestamps. Currently the upper limit of timestamp in mysql is 2038-01-19 03:14:07.999999'. You can't specify any ends after this point.
If you want the event to run "forever", then do not specify any ends (although I would be surprised if your application would run unchanged beyond 2038...)

How to reset a column value in MySQL after it's been set to a certain value after 1 hour?

Hi I am quite new to MySQL events and triggers, I have a table called userstatus(userid, timestamp,count, flag) which is used to store data about the user if he's blocked or not. When count reaches 4, the flag becomes 0(blocked). Can you please help me on what I can do to automatically reset the flag to 1 after it remains blocked for an hour.
I have tried the following code:
create event testevent
on schedule
every 1 minute
starts CURRENT_TIMESTAMP
do
update demo.userstatus set flag=1
where timestampdiff(hour,timestamp,CURRENT_TIMESTAMP)>1
This seems to work but is not efficient. Any help would be appreciated. Thanks!
It would be a lot more efficient if you added a datetime field to the usersratus table that contains the date and time until a user is blocked, or use the existing timestamp field that stores when the user was blocked rather than having just a flag.
During login or any activity you deem to fit, you just check if the current time is on or earlier than the block time (or on or before timestamp + 1 hour). If yes, then the user is blocked and you can prevent the activity. If not, then the user is not blocked and can allow the activity. You can even restore the counter at the first successful activity to 1.
This way you avoid the need to periodically check the database and clear the status flag.

delete rows from a table using MySQL Scheduler

I have a MySQL table called: "regkeys". This table has two columns: "keyCat" and "keyNum". In my web-app I have a keygen module that adds a key(1134fb) and a category(mds or dts, etc.) into this table.
What I want to accomplish is this: after issuing (adding into the db) a key, I'd like to create a MySQL event to delete the keys that stay/stayed in my table longer than 2 days (or so), this way expiring the keys (for example: - from the time it was created, counting 2 days, each key has to be deleted, having certain keys getting deleted sooner or later than others, depending on when they were created). I looked at the MySQL's API, but I need some help with the logic. I do not know how to tell the event to delete only the keys that were stored in for 2 days (or so). I was hoping somebody could give me a quick example or direct me to a clear tutorial.
Thank you very much in advance.
Edited: I think I found this other question that helps a bit with my problem. I think I was going about it the wrong way (key based). Since every key and key category get inserted into a row, the scheduler should deal with rows instead of keys.
This is the solution:
Enabled the event_scheduler in the db like this: SET GLOBAL event_scheduler = ON;
Added a timestamp column in my table using MySQL default values
I created an event, CREATE EVENT <name>
Called the event to run on a schedule like so: ON SCHEDULE EVERY 20 SECOND
Add the SQL query to the event: DO DELETE FROM <table_name> WHERE <time_stamp_column> < NOW() - INTERVAL 5 MINUTE.

How record insert time in mysql database

I want to remove a table row from my table new_data once the row is 45 mins old and then input it in another table called old_data.
The only way i can think for this to work, it to query the database lets say every min and remove any row thats (current_time - time inserted) > 45 mins.
Is there any other way of doing this? if not how could i set up a table to record inserted_time?
edit added
How could i write this statement to retrieve the correct data into the old_data table
SELECT * FROM new_spots WHERE (NOW()-created_at)>45mins
and then insert the above into the old_data table
you can specify value of time column upon insertion:
INSERT INTO x (created_at) VALUES (NOW());
additionally you can setup VIEW to show you only recent entries.
you are asking for some kind of auto expiration feature, it is not built into mysql. Memcached provides this feature. So it might be cleaner to achieve your goal as:
when you insert data into your system, you do:
insert your data into memcached with 45 minutes expiration time -- after 45 minutes, the data automatically disappear from memcached.
insert the data into the old_data table with a created_at column -- in case you need to rebuild your memcached when your memcached have to restart or other issue.
So everytime you just need to get the new data from the memcached -- as a side effect, it is faster than get the data from mysql :).
#keymone showed you how to capture the insert time. Then, periodically (every minute seems excessive - every 5 mins? 15 mins?) go through and build a list that meets the criteria, and for each entry, insert into your second table and delete from your first table.
I don't think there is an automatic way to do this. Here are some alternative ideas.
Use CRON
I have a similar scenario where we need to aggregate data from one table into another. A simple command line tool running via CRON suffices. We receive a few messages a second into our Web server and each results in a database insert. So volumes aren't huge but they are reasonably similar to your scenario
We use the NOW() function to record the insert time and after the records are 1hr old, we process them. It isn't exactly an hour but it is good enough. You can see the created_on field below.
CREATE TABLE glossaries (
id int(11) NOT NULL auto_increment,
# Our stuff ...
created_on datetime default NULL,
KEY owner_id (owner_id),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Use CRON and a Trigger
Alternatively you could use a database trigger to kick off the processing. You would still need something scheduled to cause the trigger to fire but you would get max performance/
Chris