Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cronjob or MySQL event?

I have to update my MySQL database every hour, and I was wondering what the advantages/disadvantages of using a cronjob VS a MySQL event? For example, which is faster? Which is safer? Thanks!

like image 214
Michael Eilers Smith Avatar asked Feb 11 '13 04:02

Michael Eilers Smith


People also ask

What is MySQL event scheduler?

What is MySql Event Scheduler? MySQL Events are tasks that execute on a specified time and can be scheduled hence also knowns as Scheduled events. Scheduler events are set off one or more than one SQL statement that executes on single or multiple times at specified intervals.

Does MySQL have a job scheduler?

MySQL events are similar to a cronjob on Linux or a task scheduler on Windows. MySQL Event Scheduler manages the schedule and execution of Events. MySQL Events can be very useful in many cases such as optimizing database tables, cleaning up logs, archiving data, or generating complex reports during off-peak time.

How do I know if MySQL event is running?

SELECT * FROM INFORMATION_SCHEMA. events; You can also use SHOW CREATE EVENT yourevent to see what it does.

Do I need to set up a cron job for MySQL?

Some database tasks require/prefer mysql to be off-line (eg full backup), so you've got to use cron for those - it's a bad idea to have some tasks done with cron and some done with mysql; you'll be unsure where to look You can chain up other events that should follow if you've got a shell script

What is an example of a MySQL event?

For example, you can create an event that optimizes all tables in the database that runs at 1:00 AM every Sunday. MySQL Events are also known as “temporal triggers” because they are triggered by time, not by DML events like normal triggers. MySQL events are similar to a cronjob on Linux or a task scheduler on Windows.

What is cronjob and why does it run as a daemon?

However, the abbreviation also alludes to Chronos, the ancient Greek god of time. In operating systems such as Linux or macOS, however, the command runs as a cron daemon (also called a cron system). So, there you have it. The allusion to the Greek god of time is, by the way, an excellent mnemonic to recall the purpose of a CronJob.

What is the MySQL event scheduler?

With MySQL 5.1 the guys at MySQL introduced a new cool feature: The MySQL Event Scheduler ! With the Event Scheduler you can schedule tasks that you want to perform on your database. This is great for web developers who can’t create cron jobs on their webspace, because their host won’t let them!


2 Answers

I would always go a cron job, because:

  • That's where sysadmins will expect it to be (this point is not to be underestimated)
  • crontab is bullet-proof, time-tested, extremely widely used and understood
  • You can freely direct/analyse error/success messages where you want
  • Some database tasks require/prefer mysql to be off-line (eg full backup), so you've got to use cron for those - it's a bad idea to have some tasks done with cron and some done with mysql; you'll be unsure where to look
  • You can chain up other events that should follow if you've got a shell script

And finally, just because you can do something, doesn't mean it's a good idea. Mysql is good at data stuff. Don't use it for "shell" stuff.

like image 138
Bohemian Avatar answered Sep 21 '22 05:09

Bohemian


MySQL Event Scheduler – A good replacement for cron.

We all know about cron, an easy way to schedule certain processes, like truncating your log tables in your MySQL database every week.

With MySQL 5.1 the guys at MySQL introduced a new cool feature: The MySQL Event Scheduler !

With the Event Scheduler you can schedule tasks that you want to perform on your database. This is great for web developers who can’t create cron jobs on their webspace, because their host won’t let them! It really is a great replacement for cron!

A few examples:

you want to truncate your application log table every week, this is how your event schedule should look like:

CREATE EVENT PurgeLogTable ON SCHEDULE EVERY 1 WEEK DO BEGIN DELETE FROM `logs` WHERE `LogTime` <= DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1 WEEK); INSERT INTO `audit` (`AuditDate`, `Message`) VALUES(NOW(), "Log table purged succesfully!"); END 
like image 37
user2001117 Avatar answered Sep 22 '22 05:09

user2001117