Use CREATE EVENT and SELECT ... INTO OUTFILE to schedule and export recurring reports without external schedulers.
Automating reports removes manual query runs, produces consistent CSVs, and frees developers to focus on feature work. MariaDB’s built-in event scheduler executes SQL blocks on a calendar, so no cron or external ETL is required.
First set the global variable, then persist it in the my.cnf file.
SET GLOBAL event_scheduler = ON; -- immediate
# my.cnf
[mysqld]
event_scheduler=ON
Create an event that runs once per day at 02:00 and exports a CSV to the server’s secure mysql-files
directory.
CREATE EVENT report_daily_sales
ON SCHEDULE EVERY 1 DAY
STARTS '2024-01-01 02:00:00'
DO
SELECT o.id,
o.order_date,
o.total_amount,
c.name AS customer_name,
c.email
INTO OUTFILE CONCAT('/var/lib/mysql-files/daily_sales_',
DATE_FORMAT(CURDATE(), '%Y_%m_%d'), '.csv')
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
WHERE o.order_date = CURDATE();
Events can call stored procedures that email or write alert rows.
CREATE PROCEDURE low_stock_alert()
BEGIN
INSERT INTO Alerts(product_id, stock_level, created_at)
SELECT id, stock, NOW()
FROM Products
WHERE stock < 10;
END;
CREATE EVENT weekly_stock_check
ON SCHEDULE EVERY 1 WEEK STARTS '2024-01-05 06:00:00'
DO CALL low_stock_alert();
Write events as idempotent queries; use CONCAT()
with dates to avoid file overwrites. Store output in a directory defined by secure_file_priv
. Log each run in an audit table for troubleshooting.
ALTER EVENT report_daily_sales ENABLE; -- or DISABLE
DROP EVENT IF EXISTS report_daily_sales;
The user creating the event needs EVENT
, FILE
, and table-level privileges. Grant them explicitly to avoid runtime failures.
Scheduler off: If events don’t fire, check SHOW PROCESSLIST
for event_scheduler
. Ensure event_scheduler=ON
globally and in config.
Wrong OUTFILE path: MariaDB writes only inside secure_file_priv
. Move your directory inside that path or change the variable in the server config.
Combine events with Galaxy Collections to version and share report SQL, then let Galaxy’s AI copilot optimize queries as data models change.
Yes—wrap each report query in a stored procedure and call them sequentially inside a single event body.
Temporarily change ON SCHEDULE AT NOW()
or just execute the event body manually to validate output.
Events are stored in the data dictionary and persist, but the scheduler must be ON after each restart.
Use ALTER EVENT event_name ON SCHEDULE EVERY 1 MONTH STARTS '2024-02-01 01:00:00';
MariaDB cannot email natively. Write rows to an Alerts table and let an external process pick them up, or use a UDF that calls an SMTP library.
Run reports during off-peak hours and limit result size. Consider SELECT ... INTO OUTFILE to bypass the network layer and reduce locking.