Documentationbreadcrumb arrow breadcrumb arrow Grafana Alloybreadcrumb arrow Collect and forward databreadcrumb arrow Collect MySQL database metrics and logs
Open source
Last reviewed: July 30, 2026

Collect MySQL database metrics and logs

You can configure Alloy to collect database observability data from MySQL servers including metrics, logs, and performance schema statistics. Forward this data to Grafana Cloud, Prometheus, Loki, or any compatible observability backend.

You can accomplish the following with Alloy:

  • Collect query performance data, explain plans, and lock information from MySQL.
  • Monitor schema and table statistics.
  • Forward database observability logs to Loki.
  • Scrape MySQL metrics with Prometheus.

Components used in this topic

Before you begin

  • Ensure you have a running MySQL server with access credentials.
  • Enable the MySQL performance_schema on your MySQL server. Refer to the MySQL performance_schema documentation for setup instructions.
  • Have write access to a Prometheus-compatible endpoint or Grafana Cloud for storing metrics.
  • Have write access to a Loki-compatible endpoint or Grafana Cloud for storing logs.
  • Be familiar with the concept of Components in Alloy.

Collect MySQL database observability data

The database_observability.mysql component connects to a MySQL database and collects performance schema data. This data includes query details, execution plans, and lock information forwarded as logs to Loki. The component also exports targets that can be scraped with Prometheus to collect MySQL metrics.

To collect MySQL observability data and forward it to Grafana Cloud, complete the following steps:

  1. Add a database_observability.mysql component to your configuration file.

    Alloy
    database_observability.mysql "<LABEL>" {
      data_source_name = "<MYSQL_DSN>"
      forward_to       = [loki.relabel.<LABEL>.receiver]
      targets          = prometheus.exporter.mysql.<LABEL>.targets
    
      enable_collectors = ["query_samples", "explain_plans"]
    
      cloud_provider {
        aws {
          arn = "<AWS_RDS_ARN>"
        }
      }
    }

    Replace the following:

    • <LABEL>: The Alloy component label, such as prod_mysql.
    • <MYSQL_DSN>: The MySQL Data Source Name, such as user:pass@tcp(mysql:3306)/. Refer to the DSN documentation for the full DSN format.
    • <AWS_RDS_ARN>: The ARN of your AWS RDS database instance, such as arn:aws:rds:us-east-1:123456789:db/prod-mysql. If using Azure or GCP, use the corresponding cloud provider block instead.

    For more information, refer to the database_observability.mysql documentation.

  2. Add a prometheus.exporter.mysql component to export MySQL metrics.

    Alloy
    prometheus.exporter.mysql "<LABEL>" {
      data_source_name  = "<MYSQL_DSN>"
      enable_collectors = ["perf_schema.eventsstatements"]
    }

    Replace the following:

    • <LABEL>: Must match the label used for the database_observability.mysql component.
    • <MYSQL_DSN>: The same MySQL Data Source Name as the database_observability.mysql component.

    For more information, refer to the prometheus.exporter.mysql documentation.

  3. Add a loki.relabel component to standardize labels on database logs.

    Alloy
    loki.relabel "<LABEL>" {
      forward_to = [loki.write.logs_service.receiver]
    
      rule {
        target_label = "job"
        replacement  = "integrations/db-o11y"
      }
    
      rule {
        target_label = "instance"
        replacement  = "<LABEL>"
      }
    }

    Replace the following:

    • <LABEL>: Must match the label used for the database_observability.mysql component.

    For more information, refer to the loki.relabel documentation.

  4. Add a discovery.relabel component to standardize labels on Prometheus targets.

    Alloy
    discovery.relabel "<LABEL>" {
      targets = database_observability.mysql.<LABEL>.targets
    
      rule {
        target_label = "job"
        replacement  = "integrations/db-o11y"
      }
    
      rule {
        target_label = "instance"
        replacement  = "<LABEL>"
      }
    }

    Replace the following:

    • <LABEL>: Must match the label used for the database_observability.mysql component.

    For more information, refer to the discovery.relabel documentation.

  5. Add a prometheus.scrape component to scrape the relabeled targets from discovery.relabel, which are sourced from the database_observability.mysql component’s exported targets.

    Alloy
    prometheus.scrape "<LABEL>" {
      targets    = discovery.relabel.<LABEL>.output
      job_name   = "integrations/db-o11y"
      forward_to = [prometheus.remote_write.metrics_service.receiver]
    }

    Replace the following:

    • <LABEL>: Must match the label used for the database_observability.mysql component.

    For more information, refer to the prometheus.scrape documentation.

  6. Add a prometheus.remote_write component to send metrics to Grafana Cloud or Prometheus.

    Alloy
    prometheus.remote_write "metrics_service" {
      endpoint {
        url = "<GRAFANA_CLOUD_HOSTED_METRICS_URL>"
    
        basic_auth {
          username = "<GRAFANA_CLOUD_HOSTED_METRICS_ID>"
          password = "<GRAFANA_CLOUD_RW_API_KEY>"
        }
      }
    }

    Replace the following:

    • <GRAFANA_CLOUD_HOSTED_METRICS_URL>: The URL for your Grafana Cloud hosted metrics endpoint.
    • <GRAFANA_CLOUD_HOSTED_METRICS_ID>: The user ID for your Grafana Cloud hosted metrics.
    • <GRAFANA_CLOUD_RW_API_KEY>: Your Grafana Cloud API key.

    For more information, refer to the prometheus.remote_write documentation.

  7. Add a loki.write component to send logs to Grafana Cloud or Loki.

    Alloy
    loki.write "logs_service" {
      endpoint {
        url = "<GRAFANA_CLOUD_HOSTED_LOGS_URL>"
    
        basic_auth {
          username = "<GRAFANA_CLOUD_HOSTED_LOGS_ID>"
          password = "<GRAFANA_CLOUD_RW_API_KEY>"
        }
      }
    }

    Replace the following:

    • <GRAFANA_CLOUD_HOSTED_LOGS_URL>: The URL for your Grafana Cloud hosted logs endpoint.
    • <GRAFANA_CLOUD_HOSTED_LOGS_ID>: The user ID for your Grafana Cloud hosted logs.
    • <GRAFANA_CLOUD_RW_API_KEY>: Your Grafana Cloud API key.

    For more information, refer to the loki.write documentation.

Complete configuration

The following is a complete configuration that collects MySQL database observability data and forwards it to Grafana Cloud.

Alloy
database_observability.mysql "example" {
  data_source_name = "<MYSQL_DSN>"
  forward_to       = [loki.relabel.example.receiver]
  targets          = prometheus.exporter.mysql.example.targets

  enable_collectors = ["query_samples", "explain_plans"]

  cloud_provider {
    aws {
      arn = "<AWS_RDS_ARN>"
    }
  }
}

prometheus.exporter.mysql "example" {
  data_source_name  = "<MYSQL_DSN>"
  enable_collectors = ["perf_schema.eventsstatements"]
}

loki.relabel "example" {
  forward_to = [loki.write.logs_service.receiver]

  rule {
    target_label = "job"
    replacement  = "integrations/db-o11y"
  }

  rule {
    target_label = "instance"
    replacement  = "example"
  }
}

discovery.relabel "example" {
  targets = database_observability.mysql.example.targets

  rule {
    target_label = "job"
    replacement  = "integrations/db-o11y"
  }

  rule {
    target_label = "instance"
    replacement  = "example"
  }
}

prometheus.scrape "example" {
  targets    = discovery.relabel.example.output
  job_name   = "integrations/db-o11y"
  forward_to = [prometheus.remote_write.metrics_service.receiver]
}

prometheus.remote_write "metrics_service" {
  endpoint {
    url = "<GRAFANA_CLOUD_HOSTED_METRICS_URL>"

    basic_auth {
      username = "<GRAFANA_CLOUD_HOSTED_METRICS_ID>"
      password = "<GRAFANA_CLOUD_RW_API_KEY>"
    }
  }
}

loki.write "logs_service" {
  endpoint {
    url = "<GRAFANA_CLOUD_HOSTED_LOGS_URL>"

    basic_auth {
      username = "<GRAFANA_CLOUD_HOSTED_LOGS_ID>"
      password = "<GRAFANA_CLOUD_RW_API_KEY>"
    }
  }
}

Replace the following:

  • <MYSQL_DSN>: The MySQL Data Source Name for your MySQL server.
  • <AWS_RDS_ARN>: The ARN of your AWS RDS database instance.
  • <GRAFANA_CLOUD_HOSTED_METRICS_URL>: The URL for your Grafana Cloud hosted metrics endpoint.
  • <GRAFANA_CLOUD_HOSTED_METRICS_ID>: The user ID for your Grafana Cloud hosted metrics.
  • <GRAFANA_CLOUD_HOSTED_LOGS_URL>: The URL for your Grafana Cloud hosted logs endpoint.
  • <GRAFANA_CLOUD_HOSTED_LOGS_ID>: The user ID for your Grafana Cloud hosted logs.
  • <GRAFANA_CLOUD_RW_API_KEY>: Your Grafana Cloud API key.