Configure Grafana Alloy
In this milestone, you’ll install Grafana Alloy and add the Database Observability configuration blocks for MySQL. Alloy collects both metrics and structured logs from your database and forwards them to Grafana Cloud.
To configure Grafana Alloy to connect your MySQL database to Grafana Cloud, complete the following steps:
Install Grafana Alloy 1.14.0 or later. Refer to the Alloy installation documentation for your platform.
Run Alloy with the public preview stability flag:
alloy run config.alloy --stability.level=public-previewCreate the Data Source Name secret file. Replace the placeholders with your values:
echo 'db-o11y:<DB_PASSWORD>@tcp(<DB_HOST>:<DB_PORT>)/' > /var/lib/alloy/mysql_secretReplace:
- DB_PASSWORD — Password for the
db-o11yuser you created - DB_HOST — Hostname or IP of your MySQL server
- DB_PORT — MySQL port (default: 3306)
- DB_PASSWORD — Password for the
Add the core Database Observability blocks to your Alloy configuration file. These configure the MySQL exporter for metrics and the Database Observability component for query-level logs:
local.file "mysql_secret" { filename = "/var/lib/alloy/mysql_secret" is_secret = true } prometheus.exporter.mysql "mysql" { data_source_name = local.file.mysql_secret.content enable_collectors = ["perf_schema.eventsstatements"] perf_schema.eventsstatements { text_limit = 0 } } database_observability.mysql "mysql" { data_source_name = local.file.mysql_secret.content forward_to = [loki.relabel.database_observability_mysql.receiver] targets = prometheus.exporter.mysql.mysql.targets }Add the relabeling rules and Prometheus scrape configuration:
loki.relabel "database_observability_mysql" { forward_to = [loki.write.logs_service.receiver] rule { target_label = "instance" replacement = "<DB_HOST>:<DB_PORT>" } } discovery.relabel "database_observability_mysql" { targets = database_observability.mysql.mysql.targets rule { target_label = "job" replacement = "integrations/db-o11y" } rule { target_label = "instance" replacement = "<DB_HOST>:<DB_PORT>" } } prometheus.scrape "database_observability_mysql" { targets = discovery.relabel.database_observability_mysql.output forward_to = [prometheus.remote_write.metrics_service.receiver] }In the relabeling blocks you just added, replace the
DB_HOSTandDB_PORTplaceholder values with your MySQL host and port.Add the Prometheus remote write and Loki write blocks. Find your Grafana Cloud credentials in your stack’s configuration page:
prometheus.remote_write "metrics_service" { endpoint { url = "https://prometheus-<region>.grafana.net/api/prom/push" basic_auth { username = "<METRICS_USER>" password = "<METRICS_API_KEY>" } } } loki.write "logs_service" { endpoint { url = "https://logs-<region>.grafana.net/loki/api/v1/push" basic_auth { username = "<LOGS_USER>" password = "<LOGS_API_KEY>" } } }Replace the region, usernames, and API keys with your Grafana Cloud stack values.
You’ve configured Grafana Alloy with all the Database Observability components for MySQL.
In the next milestone, you’ll verify that telemetry is flowing to Grafana Cloud.