Automatically executes one-off or recurring SQL statements at defined times via the MariaDB Event Scheduler.
Automated events eliminate cron jobs, keep logic inside the database, and guarantee that recurring maintenance, reporting, and data-movement tasks run even if application servers are down.
Session level: SET @@event_scheduler = ON;
Global level (persists restart): SET GLOBAL event_scheduler = ON;
or add event_scheduler=ON
in my.cnf
.
Use CREATE EVENT
to define a name, schedule, status, and the SQL to run. Schedules accept AT TIMESTAMP
for one-off runs or EVERY interval [STARTS ts] [ENDS ts]
for recurring runs.
Create an event that runs every night at 00:05 and inserts yesterday’s totals into a daily_sales
table.
CREATE EVENT IF NOT EXISTS ev_nightly_sales
ON SCHEDULE EVERY 1 DAY STARTS CURRENT_DATE + INTERVAL 5 MINUTE
ON COMPLETION PRESERVE ENABLE
DO
INSERT INTO daily_sales(report_date, total_amount)
SELECT CURRENT_DATE - INTERVAL 1 DAY,
SUM(total_amount)
FROM Orders
WHERE order_date = CURRENT_DATE - INTERVAL 1 DAY;
Use ALTER EVENT ev_name ON SCHEDULE ...
to change timing or ALTER EVENT ev_name DISABLE
to pause execution. Dropping an event permanently: DROP EVENT IF EXISTS ev_name;
.
The EVENT
privilege is required to create, alter, or drop events. The SUPER
privilege is needed to toggle the global scheduler setting.
Query information_schema.EVENTS
for schedule metadata and information_schema.EVENT_STATISTICS
for run counts and last execution times.
Namespace event names by project, wrap complex logic in stored procedures, keep events idempotent, and log results into audit tables for easy troubleshooting.
• Scheduler disabled: confirm SELECT @@event_scheduler
returns ON
.
• Using CURRENT_TIMESTAMP
inside ON SCHEDULE AT
: evaluate to a fixed time first; otherwise the event never runs.
The event definition belongs to the current database when created. Switch databases with USE dbname;
before CREATE EVENT
.
Yes. The scheduler spawns a new thread per event when its time arrives. Use locking or idempotent logic to avoid race conditions.
mysqldump --events
exports them. Always include events in schema version control alongside tables and procedures.