MySQL jobs are automated tasks created with CREATE EVENT that run on a defined schedule.
Events run inside MySQL, so they use the same connection pool, respect transactions, and stay in version-controlled SQL scripts. No external server access is required, keeping deployments portable.
Run SET GLOBAL event_scheduler = ON;
(needs SUPER or SET_USER_ID privilege). To persist across restarts, add event_scheduler=ON
in my.cnf
.
Use CREATE EVENT
with a schedule (AT, EVERY, or a combination). You can define a one-off or recurring job, its start time, interval, status, and body.
Create an event that runs each night at 02:00 and deletes orders older than a year: see the example below for full code.
Use ALTER EVENT
to change the schedule, body, or status. Disable with ALTER EVENT ... DISABLE
; drop completely with DROP EVENT
.
Prefix event names with the schema (e.g., analytics.cleanup_old_orders
), keep bodies idempotent, log rows affected, and test schedules on a staging database.
See the list below for the two most frequent errors and how to fix them.
Yes. Events are written to the binary log and executed on replicas if log_bin
is enabled and the replica’s event scheduler is on.
Absolutely. Use ON SCHEDULE EVERY 1 MINUTE
. Keep the body lightweight to avoid overlapping executions.
Query INFORMATION_SCHEMA.EVENTS
or run SHOW EVENTS FROM db_name;