MySQL
stored procedure
scheduling
database automation
SQL tasks

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:

sql
1DELIMITER //
2
3CREATE PROCEDURE archive_old_orders()
4BEGIN
5    INSERT INTO archived_orders (id, customer_id, created_at, total_amount)
6    SELECT id, customer_id, created_at, total_amount
7    FROM orders
8    WHERE created_at < NOW() - INTERVAL 30 DAY;
9
10    DELETE FROM orders
11    WHERE created_at < NOW() - INTERVAL 30 DAY;
12END //
13
14DELIMITER ;
15
16CALL archive_old_orders();

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:

sql
SET GLOBAL event_scheduler = ON;
SHOW VARIABLES LIKE 'event_scheduler';

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:

sql
1CREATE EVENT daily_archive_old_orders
2ON SCHEDULE EVERY 1 DAY
3STARTS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
4DO
5    CALL archive_old_orders();

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:

sql
SHOW EVENTS;
SHOW CREATE EVENT daily_archive_old_orders;

Control Timing and Lifecycle Carefully

MySQL events support one-time schedules as well as repeating schedules. For a one-time execution, you can use:

sql
1CREATE EVENT one_time_cleanup
2ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
3DO
4    CALL archive_old_orders();

You can also preserve a one-time event after it runs if you want to inspect it later:

sql
1CREATE EVENT one_time_cleanup
2ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
3ON COMPLETION PRESERVE
4DO
5    CALL archive_old_orders();

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:

sql
1ALTER EVENT daily_archive_old_orders
2ON SCHEDULE EVERY 12 HOUR;
3
4DROP EVENT daily_archive_old_orders;

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_scheduler is enabled or the event will never execute.
  • Use SHOW EVENTS and SHOW CREATE EVENT to inspect and verify the schedule.
  • Treat time zone, privileges, and idempotency as part of the design, not as afterthoughts.

Course illustration
Course illustration

All Rights Reserved.