All Products
Search
Document Center

ApsaraDB RDS:Create a scheduled task in ApsaraDB RDS for MySQL

Last Updated:Dec 01, 2025

To execute scheduled or periodic tasks on an ApsaraDB RDS for MySQL instance, such as periodically sending statistical reports, synchronizing data, or deleting expired data, you can use the event scheduler feature to execute events defined in databases.

Requirement

You have created an ApsaraDB RDS for MySQL account with the EVENT permission on the target database, such as a privileged account, a standard account (read/write), or a standard account (DML only).

Step 1: Enable Event Scheduler

First, ensure that Event Scheduler is enabled before you create a scheduled task.

  1. Log on to the ApsaraDB RDS console and go to the Instances page. In the top navigation bar, select the region in which the RDS instance resides. Then, find the RDS instance and click the instance ID.

  2. In the navigation pane on the left, click Parameters.

  3. On the Modifiable Parameters page, search for event_scheduler, and make sure that the Running Value is set to ON.

  4. If you modified the parameter value, click Apply Changes, and in the dialog box that appears, select Take Effect Immediately, and click OK.

Step 2: Create a scheduled task

Create a scheduled task in the DMS console

  1. Click Log On to Database in the upper-right corner of the page to go to the quick logon page of DMS. Log on to the RDS MySQL instance using your RDS MySQL account. For more information, see Log on to an ApsaraDB RDS database using DMS.

    Note

    When you log on to the database instance, select Control Mode as Flexible Management. For more information about DMS control modes, see Control modes.

  2. In the SQLConsole window, execute the following SQL statement to create a test table named students.

    Note

    If you use a table that already exists in the database, you can skip this step and use the corresponding SQL statement in the Event statement field.

    CREATE TABLE students (
      `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
      `name` varchar(64) NOT NULL COMMENT 'name',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  3. In the visual operation area of the SQL window, click Programmable Object Permissions, right-click Event, and select New (Event).

    image.png

  4. On the New (Event) tab, configure the event parameters.

    In this example, Event name is set to test01, Scheduling Method is set to Cycle Time, Interval is set to 10 minutes, and Event statement is set to insert data into the test table students. For more information about event parameters, see the Event parameter description table.

    Example of an event statement:

    begin
    INSERT INTO students(name) VALUES('zhangsan');
    end

    Event parameter description

    Classification

    Parameter

    Description

    Event Basic information

    Event name

    The name of the event.

    Status

    The status of the event. Valid values:

    • Enabled: The event is executable.

    • Disable: The event is not executable.

    • Disable from DB: The event can be executed only on the primary database.

    Due deletion

    • Specifies whether to delete the event after the event is executed at a fixed point in time.

    • Specifies whether to delete the event after the event is executed at specified intervals for the specified period of time.

    Note

    The annotation of the event.

    Execution time definition

    Scheduling Method

    • Fixed Time: The event is executed once at a fixed point in time. If you select this option, you must set Fixed Time.

    • Cycle time: The event is executed at specified intervals. If you select this option, you must set Interval, Start time, and End time.

      Note
      • Interval: consists of the number of time units and the time unit, which indicates the interval at which the event is executed.

      • Start time: the time when the system starts to execute the recurring event. If you do not configure this parameter, the system immediately executes the event.

      • End time: the time when the system stops executing the recurring event. If you do not configure this parameter, the system infinitely executes the event.

    Event Statement

    None

    The SQL statement that needs to be executed in the event.

    Replace /**event body**/ with the specific SQL statement.

  5. Click Save. In the Submit dialog box that appears, confirm that the SQL statement is correct and click Execute.

    You can view the new scheduled task under Programmable Object Permissions > Event. You can also execute SHOW EVENTS; in the SQL window to view detailed information about the scheduled task.

    Note

    When you query using SHOW EVENTS;, if the status of the event is ENABLED, the event is enabled.

Create a scheduled task using SQL statements

Syntax

CREATE
    [DEFINER = user]
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [ON COMPLETION [NOT] PRESERVE]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'string']
    DO event_body;

schedule:
    {
        AT timestamp [+ INTERVAL interval] ...
      | EVERY interval
          [STARTS timestamp [+ INTERVAL interval] ...]
          [ENDS timestamp [+ INTERVAL interval] ...]
    }

interval:
    quantity {
        YEAR
      | QUARTER
      | MONTH
      | DAY
      | HOUR
      | MINUTE
      | WEEK
      | SECOND
      | YEAR_MONTH
      | DAY_HOUR
      | DAY_MINUTE
      | DAY_SECOND
      | HOUR_MINUTE
      | HOUR_SECOND
      | MINUTE_SECOND
    }

Example

For example, to delete data that is 30 days old from the user_order table at 02:00 every day, where the create_time field of the user_order table records the time when each row of data is written.

DELIMITER $$
CREATE EVENT IF NOT EXISTS delete_old_user_orders
ON SCHEDULE
    EVERY 1 DAY
    STARTS TIMESTAMP(CURRENT_DATE, '02:00:00') + INTERVAL 1 DAY -- Start at 02:00 the next day
COMMENT --Delete order data that is 30 days old every day
DO
BEGIN
    -- Delete logic (create_time is the time field)
    DELETE FROM user_order 
    WHERE create_time < DATE_SUB(NOW(), INTERVAL 30 DAY);
    
    -- Optional: Record operation logs (You need to create the log table first)
    -- INSERT INTO task_log (message) VALUES ('Deleted old user_orders');
END$$
DELIMITER ;

Step 3: View the execution results of the scheduled task

When viewing the execution results of an event, you can check the execution of the SQL statements in the event.

  • If the SQL statement is successfully executed, you can check related tables to verify the result.

  • If the SQL statement fails to execute, you can view error logs to obtain details.

You can enable SQL Explorer to view the execution results of scheduled tasks through log records.

Note

You can execute the SQL statement SELECT * FROM information_schema.events; to confirm existing scheduled tasks and their related information, such as the creation time, last modification time, last execution time, and whether they are enabled.

Step 4: Delete a scheduled task

Delete a scheduled task in the DMS console

Note

If you set Due deletion and End time when you created a recurring event, the recurring event is automatically deleted after it expires. You do not need to manually delete it.

  1. Under Programmable Object Permissions > Event, right-click the target event and select Delete (event).

  2. In the dialog box that appears, click Execute.

Note

You can also execute the DROP EVENT <event name>; command in the SQL window to delete the corresponding event.

Delete a scheduled task using SQL statements

Execute the DROP EVENT <event name>; command to delete the corresponding event.

References

What is Data Management (DMS)

FAQ

Q: Are the statements executed by scheduled tasks in ApsaraDB RDS for MySQL recorded in SQL audit logs?

A: Yes, they are recorded in SQL Explorer logs. Because scheduled tasks are executed internally by the database, the client IP address is displayed as %.

Note

A client IP address displayed as % does not necessarily indicate a record of a scheduled task execution. It may also come from SQL statements within a stored procedure.

Q: Does the system check whether related table objects exist when you create a scheduled task in ApsaraDB RDS for MySQL?

A: No, the system does not check. If the table is not found during execution or the SQL statement fails to execute for other reasons, you can view error logs and slow logs to obtain details.

Q: How do I check scheduled tasks in ApsaraDB RDS for MySQL and their last execution time?

A: You can execute the following SQL statement to query the information_schema.events table to confirm existing scheduled tasks and their related information, such as the creation time, last modification time, last execution time, and whether they are enabled.

SELECT * FROM information_schema.events;