Menu
Grafana Cloud

MySQL integration for Grafana Cloud

MySQL is a managed, open source relational database that is a widely used for both small and large applications.

This integration includes 15 useful alerts and 2 pre-built dashboards to help monitor and visualize MySQL metrics and logs.

Before you begin

A running MySQL instance, user and password are required for the integration.

Supported versions:

  • MySQL >= 5.6
  • MariaDB >= 10.2

We recommend that you configure a separate user for the Agent, and give it only the strictly mandatory security privileges necessary for monitoring your node, as per the official documentation.

Install MySQL integration for Grafana Cloud

  1. In your Grafana Cloud stack, click Connections in the left-hand menu.
  2. Find MySQL and click its tile to open the integration.
  3. Review the prerequisites in the Configuration Details tab and set up Grafana Agent to send MySQL metrics and logs to your Grafana Cloud instance.
  4. Click Install to add this integration’s pre-built dashboards and alerts to your Grafana Cloud instance, and you can start monitoring your MySQL setup.

Configuration snippets for Grafana Alloy

Simple mode

These snippets are configured to scrape a single MySQL node with Grafana Alloy running locally.

Copy and Paste the following snippets into your Grafana Alloy configuration file.

Replace data_source_name value to your MySQL DSN connection details. It must follow the URI patterns defined in go-sql-driver README, which is [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN].

If your MySQL server requires authentication, check the integration documentation to understand how to setup a secret file.

Integrations snippets

alloy
prometheus.exporter.mysql "integrations_mysqld_exporter" {
  data_source_name = "root:password@(localhost:3306)/"
}

discovery.relabel "integrations_mysqld_exporter" {
  targets = prometheus.exporter.mysql.integrations_mysqld_exporter.targets

  rule {
    target_label = "job"
    replacement  = "integrations/mysql"
  }

  rule {
    target_label = "instance"
    replacement  = constants.hostname
  }
}

prometheus.scrape "integrations_mysqld_exporter" {
  targets    = discovery.relabel.integrations_mysqld_exporter.output
  forward_to = [prometheus.remote_write.metrics_service.receiver]
  job_name   = "integrations/mysqld_exporter"
}

Logs snippets

all

alloy
local.file_match "logs_integrations_mysql" {
  path_targets = [{
    __address__ = "localhost",
    __path__    = "/var/log/mysql/*.log",
    instance    = constants.hostname,
    job         = "integrations/mysql",
  }]
}

loki.process "logs_integrations_mysql" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  stage.regex {
    expression = "(?P<timestamp>.+) (?P<thread>[\\d]+) \\[(?P<label>.+?)\\]( \\[(?P<err_code>.+?)\\] \\[(?P<subsystem>.+?)\\])? (?P<msg>.+)"
  }

  stage.labels {
    values = {
      err_code  = null,
      level     = "label",
      subsystem = null,
    }
  }

  stage.drop {
    drop_counter_reason = "drop empty lines"
    expression          = "^ *$"
  }
}

loki.source.file "logs_integrations_mysql" {
  targets    = local.file_match.logs_integrations_mysql.targets
  forward_to = [loki.process.logs_integrations_mysql.receiver]
}

Advanced mode

The following snippets provide examples to guide you through the configuration process.

To instruct Grafana Alloy to scrape your MySQL server, manually copy and append the snippets to your alloy configuration file, then follow subsequent instructions.

Advanced integrations snippets

alloy
local.file "mysql_secret" {
  filename = "/var/lib/alloy/mysql-secret"
  is_secret = true
}

prometheus.exporter.mysql "integrations_mysqld_exporter" {
  data_source_name = local.file.mysql_secret.content
}

discovery.relabel "integrations_mysqld_exporter" {
  targets = prometheus.exporter.mysql.integrations_mysqld_exporter.targets

  rule {
    target_label = "job"
    replacement  = "integrations/mysql"
  }

  rule {
    target_label = "instance"
    replacement  = constants.hostname
  }
}

prometheus.scrape "integrations_mysqld_exporter" {
  targets    = discovery.relabel.integrations_mysqld_exporter.output
  forward_to = [prometheus.remote_write.metrics_service.receiver]
  job_name   = "integrations/mysqld_exporter"
}

This integration uses the prometheus.exporter.mysql component to collect metrics from a MySQL instance.

For better security, your DSN containing authentication details will be stored in a file, and loaded by the Grafana Agent as a secret. The snippets are prepared to read from /var/lib/alloy/mysql-secret. It must follow the URI patterns defined in go-sql-driver README, which is [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN].

You can create this file using the following command; echo -n 'user:password@(hostname:3306)/' > /var/lib/alloy/mysql-secret

For the full array of options included within this exporter, please check the component documentation.

This exporter must be linked with a discovery.relabel component to apply the necessary relabelings. For each MySQL instance to be monitored you must create a pair of these components.

The snippets set the instance label to your Grafana Alloy server hostname using the constants.hostname variable. If you are running Grafana Alloy outside of your MySQL server host, change the variable to a value that uniquely identifies it.

You can then scrape them by including each discovery.relabel under targets within the prometheus.scrape component.

Advanced logs snippets

darwin

alloy
local.file_match "logs_integrations_mysql" {
  path_targets = [{
    __address__ = "localhost",
    __path__    = "/var/log/mysql/*.log",
    instance    = constants.hostname,
    job         = "integrations/mysql",
  }]
}

loki.process "logs_integrations_mysql" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  stage.regex {
    expression = "(?P<timestamp>.+) (?P<thread>[\\d]+) \\[(?P<label>.+?)\\]( \\[(?P<err_code>.+?)\\] \\[(?P<subsystem>.+?)\\])? (?P<msg>.+)"
  }

  stage.labels {
    values = {
      err_code  = null,
      level     = "label",
      subsystem = null,
    }
  }

  stage.drop {
    drop_counter_reason = "drop empty lines"
    expression          = "^ *$"
  }
}

loki.source.file "logs_integrations_mysql" {
  targets    = local.file_match.logs_integrations_mysql.targets
  forward_to = [loki.process.logs_integrations_mysql.receiver]
}

To collect your MySQL Server logs, you must use a local.file_match component to tag the file to be scraped, a loki.source.file component to prepare it for Loki ingestion and a loki.process component to process your logs with adequate labels and drop empty lines.

You can check the full array of options in each component documentation.

linux

alloy
local.file_match "logs_integrations_mysql" {
  path_targets = [{
    __address__ = "localhost",
    __path__    = "/var/log/mysql/*.log",
    instance    = constants.hostname,
    job         = "integrations/mysql",
  }]
}

loki.process "logs_integrations_mysql" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  stage.regex {
    expression = "(?P<timestamp>.+) (?P<thread>[\\d]+) \\[(?P<label>.+?)\\]( \\[(?P<err_code>.+?)\\] \\[(?P<subsystem>.+?)\\])? (?P<msg>.+)"
  }

  stage.labels {
    values = {
      err_code  = null,
      level     = "label",
      subsystem = null,
    }
  }

  stage.drop {
    drop_counter_reason = "drop empty lines"
    expression          = "^ *$"
  }
}

loki.source.file "logs_integrations_mysql" {
  targets    = local.file_match.logs_integrations_mysql.targets
  forward_to = [loki.process.logs_integrations_mysql.receiver]
}

To collect your MySQL Server logs, you must use a local.file_match component to tag the file to be scraped, a loki.source.file component to prepare it for Loki ingestion and a loki.process component to process your logs with adequate labels and drop empty lines.

You can check the full array of options in each component documentation.

windows

alloy
local.file_match "logs_integrations_mysql" {
  path_targets = [{
    __address__ = "localhost",
    __path__    = "/var/log/mysql/*.log",
    instance    = constants.hostname,
    job         = "integrations/mysql",
  }]
}

loki.process "logs_integrations_mysql" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  stage.regex {
    expression = "(?P<timestamp>.+) (?P<thread>[\\d]+) \\[(?P<label>.+?)\\]( \\[(?P<err_code>.+?)\\] \\[(?P<subsystem>.+?)\\])? (?P<msg>.+)"
  }

  stage.labels {
    values = {
      err_code  = null,
      level     = "label",
      subsystem = null,
    }
  }

  stage.drop {
    drop_counter_reason = "drop empty lines"
    expression          = "^ *$"
  }
}

loki.source.file "logs_integrations_mysql" {
  targets    = local.file_match.logs_integrations_mysql.targets
  forward_to = [loki.process.logs_integrations_mysql.receiver]
}

To collect your MySQL Server logs, you must use a local.file_match component to tag the file to be scraped, a loki.source.file component to prepare it for Loki ingestion and a loki.process component to process your logs with adequate labels and drop empty lines.

You can check the full array of options in each component documentation.

Grafana Agent static configuration (deprecated)

The following section shows configuration for running Grafana Agent in static mode which is deprecated. You should use Grafana Alloy for all new deployments.

Dashboards

The MySQL integration installs the following dashboards in your Grafana Cloud instance to help monitor your system.

  • MySQL
  • MySQL logs

MySQL overview

MySQL overview

MySQL logs

MySQL logs

Alerts

The MySQL integration includes the following useful alerts:

AlertDescription
MySQLDownCritical: MySQL not up.
MySQLRestartedCritical: MySQL restarted.
MySQLSlowQueriesWarning: MySQL slow queries.
MySQLTuneThreadCacheSizeWarning: MySQL thread cache needs tuning.
MySQLInsufficientTempTableSizeWarning: MySQL temp table size insufficient.
MySQLInsufficientBinLogCacheSizeWarning: MySQL binary log cache size insufficient.
MySQLInsufficientBinLogStmtCacheSizeWarning: MySQL binary log statement cache size insufficient.
MySQLGaleraNotReadyWarning: Galera cluster node not ready.
MySQLGaleraOutOfSyncWarning: Galera cluster node out of sync.
MySQLGaleraDonorFallingBehindWarning: XtraDB cluster donor node falling behind.
MySQLReplicationNotRunningCritical: Replication is not running.
MySQLReplicationLagCritical: MySQL slave replication is lagging.
MySQLHeartbeatLagCritical: MySQL heartbeat is lagging.
MySQLInnoDBLogWaitsWarning: MySQL innodb log writes stalling.
MySQLSlaveBehindMasterWarning: MySQL innodb slave behind master.

Metrics

The most important metrics provided by the MySQL integration, which are used on the pre-built dashboards and Prometheus alerts, are as follows:

  • asserts:mixin_workload_job
  • instance:mysql_heartbeat_lag_seconds
  • instance:mysql_slave_lag_seconds
  • mysql_global_status_aborted_clients
  • mysql_global_status_aborted_connects
  • mysql_global_status_binlog_cache_disk_use
  • mysql_global_status_binlog_cache_use
  • mysql_global_status_binlog_stmt_cache_disk_use
  • mysql_global_status_binlog_stmt_cache_use
  • mysql_global_status_buffer_pool_pages
  • mysql_global_status_bytes_received
  • mysql_global_status_bytes_sent
  • mysql_global_status_commands_total
  • mysql_global_status_connections
  • mysql_global_status_created_tmp_disk_tables
  • mysql_global_status_created_tmp_files
  • mysql_global_status_created_tmp_tables
  • mysql_global_status_handlers_total
  • mysql_global_status_innodb_buffer_pool_bytes_data
  • mysql_global_status_innodb_data_read
  • mysql_global_status_innodb_data_written
  • mysql_global_status_innodb_log_waits
  • mysql_global_status_innodb_mem_adaptive_hash
  • mysql_global_status_innodb_mem_dictionary
  • mysql_global_status_innodb_num_open_files
  • mysql_global_status_innodb_page_size
  • mysql_global_status_innodb_pages_written
  • mysql_global_status_key_blocks_unused
  • mysql_global_status_key_blocks_used
  • mysql_global_status_max_used_connections
  • mysql_global_status_open_files
  • mysql_global_status_open_table_definitions
  • mysql_global_status_open_tables
  • mysql_global_status_opened_files
  • mysql_global_status_opened_table_definitions
  • mysql_global_status_opened_tables
  • mysql_global_status_qcache_free_memory
  • mysql_global_status_qcache_hits
  • mysql_global_status_qcache_inserts
  • mysql_global_status_qcache_lowmem_prunes
  • mysql_global_status_qcache_not_cached
  • mysql_global_status_qcache_queries_in_cache
  • mysql_global_status_queries
  • mysql_global_status_questions
  • mysql_global_status_select_full_join
  • mysql_global_status_select_full_range_join
  • mysql_global_status_select_range
  • mysql_global_status_select_range_check
  • mysql_global_status_select_scan
  • mysql_global_status_slow_queries
  • mysql_global_status_sort_merge_passes
  • mysql_global_status_sort_range
  • mysql_global_status_sort_rows
  • mysql_global_status_sort_scan
  • mysql_global_status_table_locks_immediate
  • mysql_global_status_table_locks_waited
  • mysql_global_status_table_open_cache_hits
  • mysql_global_status_table_open_cache_misses
  • mysql_global_status_table_open_cache_overflows
  • mysql_global_status_threads_cached
  • mysql_global_status_threads_connected
  • mysql_global_status_threads_created
  • mysql_global_status_threads_running
  • mysql_global_status_uptime
  • mysql_global_status_wsrep_local_recv_queue
  • mysql_global_status_wsrep_local_state
  • mysql_global_status_wsrep_ready
  • mysql_global_variables_innodb_additional_mem_pool_size
  • mysql_global_variables_innodb_buffer_pool_size
  • mysql_global_variables_innodb_log_buffer_size
  • mysql_global_variables_key_buffer_size
  • mysql_global_variables_max_connections
  • mysql_global_variables_open_files_limit
  • mysql_global_variables_query_cache_size
  • mysql_global_variables_table_definition_cache
  • mysql_global_variables_table_open_cache
  • mysql_global_variables_thread_cache_size
  • mysql_global_variables_tokudb_cache_size
  • mysql_global_variables_wsrep_desync
  • mysql_heartbeat_now_timestamp_seconds
  • mysql_heartbeat_stored_timestamp_seconds
  • mysql_info_schema_processlist_threads
  • mysql_slave_status_seconds_behind_master
  • mysql_slave_status_slave_io_running
  • mysql_slave_status_slave_sql_running
  • mysql_slave_status_sql_delay
  • mysql_up
  • up

Changelog

md
# 1.1.1 - July 2024

* Adds asserts specific variables to dashboards

# 1.1.0 - June 2024

* Add asserts support
* Update mixin with additional alerts

# 1.0.2 - January 2024

* Allow selection of "all" clusters on MySQL overview dashboard, allowing the display of metrics which do not have the cluster tag.

# 1.0.1 - August 2023

* Add regex filter for logs datasource

# 1.0.0 - August 2023

* Add Kubernetes instructions
* Switch panels to timeseries type
* Fix stat panels to use 'last' aggegation instead of 'mean'
* Chain job and instance variables

# 0.1.1 - July 2023

* New Filter Metrics option for configuring the Grafana Agent, which saves on metrics cost by dropping any metric not used by this integration. Beware that anything custom built using metrics that are not on the snippet will stop working.
* New hostname relabel option, which applies the instance name you write on the text box to the Grafana Agent configuration snippets, making it easier and less error prone to configure this mandatory label.

# 0.1.0 - July 2023

* Updated logs dashboard
* Updated alerts annotations

# 0.0.5 - November 2022

* Add integration status panel

# 0.0.4 - April 2022

* Updated documentation and agent config snippets

# 0.0.3 - February 2022

* Added logs support from Loki datasource

# 0.0.2 - October 2021

* Update all rate queries to use `$__rate_interval`

# 0.0.1 - October 2020

* Initial release

Cost

By connecting your MySQL instance to Grafana Cloud, you might incur charges. To view information on the number of active series that your Grafana Cloud account uses for metrics included in each Cloud tier, see Active series and dpm usage and Cloud tier pricing.