---
title: "Configure Grafana Alloy | Grafana Labs"
description: "Install Grafana Alloy and add the Database Observability configuration blocks for MySQL telemetry collection."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# 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.

01. To configure Grafana Alloy to connect your MySQL database to Grafana Cloud, complete the following steps:
02. Install Grafana Alloy 1.14.0 or later. Refer to the [Alloy installation documentation](/docs/alloy/latest/get-started/install/) for your platform.
03. Run Alloy with the public preview stability flag:
    
    Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
    
    ```bash
    alloy run config.alloy --stability.level=public-preview
    ```
04. Create the Data Source Name secret file. Replace the placeholders with your values:
    
    Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
    
    ```bash
    echo 'db-o11y:<DB_PASSWORD>@tcp(<DB_HOST>:<DB_PORT>)/' > /var/lib/alloy/mysql_secret
    ```
    
    Replace:
    
    - **DB\_PASSWORD** — Password for the `db-o11y` user you created
    - **DB\_HOST** — Hostname or IP of your MySQL server
    - **DB\_PORT** — MySQL port (default: 3306)
05. 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:
    
    Alloy ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
    
    ```alloy
    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
    }
    ```
06. Add the relabeling rules and Prometheus scrape configuration:
    
    Alloy ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
    
    ```alloy
    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]
    }
    ```
07. In the relabeling blocks you just added, replace the `DB_HOST` and `DB_PORT` placeholder values with your MySQL host and port.
08. Add the Prometheus remote write and Loki write blocks. Find your Grafana Cloud credentials in your stack’s configuration page:
    
    Alloy ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
    
    ```alloy
    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>"
        }
      }
    }
    ```
09. Replace the region, usernames, and API keys with your Grafana Cloud stack values.
10. 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.
