Xem thêm

A Comprehensive Guide to MySQL Events and Event Scheduler

MySQL Events are a powerful tool for managing databases and automating repetitive tasks. In this guide, we will explore what MySQL Events are and how to set them up and use them effectively. Whether you...

MySQL Events are a powerful tool for managing databases and automating repetitive tasks. In this guide, we will explore what MySQL Events are and how to set them up and use them effectively. Whether you are an experienced developer or just starting with MySQL, this guide will provide you with everything you need to know to get started.

What is MySQL Event Scheduler?

The MySQL Event Scheduler is a special thread that runs in the background and manages the execution of scheduled events. It acts as MySQL's alternative to Cron jobs and offers several benefits:

  • It is written directly on the MySQL Server.
  • It is platform and application independent.
  • It can be used for regular database updates or cleanup tasks.
  • It reduces the number of query compilations.

To use the Event Scheduler, you need a system running MySQL on a database server and a MySQL user account with root privileges. Once you have these prerequisites set up, you can proceed to configure the Event Scheduler.

MySQL Event Scheduler Configuration

The state of the MySQL Event Scheduler can be configured to turn it on, off, or disable it. To check the current state, you can run the following command in the MySQL shell:

SHOW processlist;

Check event scheduler state in MySQL shell.

The "event_scheduler" variable in the command's output indicates the state of the Event Scheduler. The possible states are:

  • ON: The Event Scheduler thread is running and executing all scheduled events. This is the default state.
  • OFF: The Event Scheduler thread is not running, and scheduled events are not executed.
  • DISABLED: The Event Scheduler is not operational, and the thread does not run.

To turn the Event Scheduler on or off, you can use the following commands:

SET GLOBAL event_scheduler = ON; SET GLOBAL event_scheduler = OFF;

The value "ON" can also be replaced with "1", and the value "OFF" can be replaced with "0". Once you have configured the Event Scheduler, you can proceed to create and schedule events.

MySQL Event Scheduling

An event in MySQL is a database object that contains SQL statements to be executed at a specified time or at regular intervals. Events are similar to temporal triggers, but they should not be confused with MySQL Triggers, which are executed in response to specific events. To create a new event, you can use the following syntax:

CREATE EVENT [IF NOT EXISTS] event_name   ON SCHEDULE schedule   DO event_body

The "IF NOT EXISTS" statement ensures that the event name is unique in the database. You can specify a unique event name and provide the schedule for executing the event. The event body can contain stored procedures, single queries, or compound statements.

Events can be scheduled as one-time events or recurring events. One-time events are executed only once and then automatically deleted. Recurring events happen repeatedly at a specified time. To schedule a one-time event, you can use the "AT" keyword followed by a timestamp, as shown below:

AT timestamp + [INTERVAL interval]

The timestamp must be a future DATETIME or TIMESTAMP value. You can also specify an interval to start the event later. For recurring events, you can use the "EVERY" keyword along with the "STARTS" and "ENDS" keywords to define the execution interval, as shown below:

EVERY interval STARTS timestamp [+ INTERVAL] ENDS timestamp [+ INTERVAL]

To view all the events stored in the database, you can use the following command:

SHOW EVENTS FROM database_name;

See all scheduled events in a database.

This command will display a list of all the events stored in the specified database. You can also use the "ALTER EVENT" statement to change an existing event or the "DROP EVENT" statement to delete an event.

MySQL Event Limitations

While MySQL Events offer great flexibility and automation capabilities, there are some limitations to consider:

  • Events cannot return a result set, and the output is directed to "dev/null".
  • Event names are not case sensitive, and two events cannot have the same name with different case.
  • Events cannot be scheduled beyond January 19th, 2038.
  • Events cannot be created, dropped, or altered by another stored program, trigger, or event.
  • Events cannot create, drop, or alter stored programs or triggers.
  • The maximum delay for executing an event is two seconds.
  • An event always runs with definer privileges, meaning it executes as the user who created it.
  • Events do not change the count of a statement's execution.
  • There are certain limitations on interval resolutions and recursive calls.


MySQL Events and the Event Scheduler provide a powerful way to automate tasks and efficiently manage databases. By leveraging the flexibility of SQL statements and schedules, you can streamline your database operations and focus on more critical aspects of your projects. So go ahead, experiment with MySQL Events, and unlock the full potential of your MySQL database.