How to Set a Timer in MySQL
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
This guide covers how to automate SQL commands in MySQL. You’ll usually want to do this to schedule routine tasks like data backups, cleanup and reporting during off-peak hours.
MySQL’s event scheduler is a built-in feature for executing tasks at scheduled times. It’s similar to cron jobs in Unix/Linux systems.
Before creating events, ensure the event scheduler is enabled:
SET GLOBAL event_scheduler = ON;
Alternatively, check its status:
SHOW VARIABLES LIKE 'event_scheduler';
You can create an event with the following syntax:
CREATE EVENT my_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE my_table SET my_column = my_value;
This event updates my_table one hour from the current time.
For more complex scheduling, you can use a couple clauses in the ON SCHEDULE section.
To repeat an event, use the EVERY clause:
CREATE EVENT my_repeating_event
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
INSERT INTO my_table (my_column) VALUES (my_value);
This will insert a new record into my_table every day.
Control when an event starts and optionally when it ends:
CREATE EVENT my_timed_event
ON SCHEDULE EVERY 1 WEEK
STARTS '2023-01-01 00:00:00'
ENDS '2023-12-31 23:59:59'
DO
CALL my_procedure();
This calls my_procedure weekly throughout 2023.
List all events:
SHOW EVENTS;
Modify an event’s definition:
ALTER EVENT my_event
ON SCHEDULE EVERY 2 DAYS;
Remove an event when it’s no longer needed:
DROP EVENT IF EXISTS my_event;
Basedash is built as an AI-native BI platform, so teams can go from ad hoc SQL to trusted answers and dashboards quickly, without the overhead of traditional BI setup.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.