How to schedule a stored procedure in MySQL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL does not use an operating-system cron service to run stored procedures on a schedule. Instead, the usual solution is the MySQL Event Scheduler, which lets the server execute SQL statements at a specific time or interval, including CALL statements that invoke stored procedures.
Create the Procedure First
Before anything can be scheduled, the stored procedure itself must exist and run correctly when called manually. For example, suppose you want to archive old orders once per day:
Calling it manually first matters because the event scheduler will not fix SQL logic errors for you. If the procedure fails when called directly, it will fail the same way when triggered by an event.
Enable the MySQL Event Scheduler
The event scheduler must be enabled before scheduled events will run:
If the value is ON, the scheduler is active. If it is OFF, events exist in the schema but do not execute. In many installations, you also want the setting in the MySQL configuration file so it survives a restart.
Schedule the Procedure With a MySQL Event
Once the scheduler is enabled, create an event that calls the procedure at the desired time:
This tells MySQL to start one hour from now and then repeat every day. The event body can contain a simple CALL, which keeps the scheduling logic separate from the business logic stored in the procedure.
You can inspect the event later with:
Control Timing and Lifecycle Carefully
MySQL events support one-time schedules as well as repeating schedules. For a one-time execution, you can use:
You can also preserve a one-time event after it runs if you want to inspect it later:
For recurring tasks, the most important questions are:
- what time zone the server uses
- which account is the event definer
- whether the event body is idempotent if it runs more than once
Database automation becomes risky when those assumptions are left implicit.
Updating or Removing a Scheduled Event
You do not need to drop and recreate the procedure if only the schedule changes. You can alter or remove the event directly:
This separation is useful in production because it lets you tune the schedule without editing the stored procedure logic.
Common Pitfalls
- Forgetting to enable
event_scheduler. The event can be created successfully and still never run. - Scheduling the procedure before testing
CALL procedure_name()manually. - Ignoring the server time zone. The event fires according to MySQL server time unless you design around that explicitly.
- Using a definer account that later loses privileges. An event may stop working if the required permissions disappear.
- Writing a procedure that is not safe to rerun. Retries or overlapping schedules can create duplicate work unless the procedure is designed carefully.
Summary
- Use the MySQL Event Scheduler to run stored procedures on a schedule.
- Create and test the procedure first, then wrap it with an event that calls
CALL procedure_name(). - Make sure
event_scheduleris enabled or the event will never execute. - Use
SHOW EVENTSandSHOW CREATE EVENTto inspect and verify the schedule. - Treat time zone, privileges, and idempotency as part of the design, not as afterthoughts.

