docs/sources/datasources/mysql/alerting/index.md
You can use Grafana Alerting with MySQL to create alerts based on your MySQL data. This allows you to monitor metrics, detect anomalies, and receive notifications when specific conditions are met.
For general information about Grafana Alerting, refer to Grafana Alerting.
Before creating alerts with MySQL, ensure you have:
MySQL alerting works with time series queries that return numeric data over time. Table formatted queries are not supported in alert rule conditions.
To create a valid alert query:
time column that returns a SQL datetime or UNIX epoch timestampFor more information on writing time series queries, refer to MySQL query editor.
| Query format | Alerting support | Notes |
|---|---|---|
| Time series | Yes | Required for alerting |
| Table | No | Convert to time series format for alerts |
To create an alert rule using MySQL:
$__time() or $__timeGroup() macro$__timeFilter() to filter data by the dashboard time rangeFor detailed instructions, refer to Create a Grafana-managed alert rule.
The following examples show common alerting scenarios with MySQL.
Monitor the number of errors over time:
SELECT
$__timeGroup(created_at, '1m') AS time,
COUNT(*) AS error_count
FROM error_logs
WHERE $__timeFilter(created_at)
AND level = 'error'
GROUP BY time
ORDER BY time
Condition: When error_count is above 100.
Monitor API response times:
SELECT
$__timeGroup(request_time, '5m') AS time,
AVG(response_time_ms) AS avg_response_time
FROM api_requests
WHERE $__timeFilter(request_time)
GROUP BY time
ORDER BY time
Condition: When avg_response_time is above 500 (milliseconds).
Detect drops in order activity:
SELECT
$__timeGroup(order_date, '1h') AS time,
COUNT(*) AS order_count
FROM orders
WHERE $__timeFilter(order_date)
GROUP BY time
ORDER BY time
Condition: When order_count is below 10.
Monitor database storage metrics:
SELECT
$__timeGroup(recorded_at, '5m') AS time,
AVG(disk_used_percent) AS disk_usage
FROM system_metrics
WHERE $__timeFilter(recorded_at)
AND metric_type = 'disk'
GROUP BY time
ORDER BY time
Condition: When disk_usage is above 85.
When using MySQL with Grafana Alerting, be aware of the following limitations:
Alert queries cannot contain template variables. Grafana evaluates alert rules on the backend without dashboard context, so variables like $hostname or $environment won't be resolved.
If your dashboard query uses template variables, create a separate query for alerting with hard coded values.
Queries using the Table format cannot be used for alerting. Set the query format to Time series and ensure your query returns a time column.
Complex queries with large datasets may timeout during alert evaluation. Optimize queries for alerting by:
WHERE clauses to limit dataFollow these best practices when creating MySQL alerts:
$__timeFilter() macro to limit data to the evaluation window.WHERE clauses and GROUP BY.