MySQLd Exporter

grafana-agent 内置集成了 mysqld_exporter, 来收集MySQL Server的metrics指标。

目前一个grafana-agent实例,只能配置和采集一个MySQL server的metrics,因此如果您想要配置采集多个MySQL server的指标,那么需要启动多个grafana-agent实例,并使用 relabel_configs 机制来给不同的MySQL server的metrics数据做区分。

配置并启用mysqld_exporter

下面是开启了mysqld_exporter的配置文件示例:

  1. mysqld_exporter:
  2. enabled: true
  3. data_source_name: root@(server-a:3306)/
  4. relabel_configs:
  5. - source_labels: [__address__]
  6. target_label: instance
  7. replacement: server-a

为了安全起见,推荐您为grafana-agent mysqld_exporter 配置一个单独的数据库账号,并授予合适的权限,需要的权限配置详情可以参考 MySQL Expoter 官方文档.

采集的关键指标列表

  1. mysql_global_status_uptime: The number of seconds that the server has been up.(Gauge)
  2. mysql_global_status_uptime_since_flush_status: The number of seconds since the most recent FLUSH STATUS statement.(Gauge)
  3. mysql_global_status_queries: The number of statements executed by the server. This variable includes statements executed within stored programs, unlike the Questions variable. It does not count COM_PING or COM_STATISTICS commands.(Counter)
  4. mysql_global_status_threads_connected: The number of currently open connections.(Counter)
  5. mysql_global_status_connections: The number of connection attempts (successful or not) to the MySQL server.(Gauge)
  6. mysql_global_status_max_used_connections: The maximum number of connections that have been in use simultaneously since the server started.(Gauge)
  7. mysql_global_status_threads_running: The number of threads that are not sleeping.(Gauge)
  8. mysql_global_status_questions: The number of statements executed by the server. This includes only statements sent to the server by clients and not statements executed within stored programs, unlike the Queries variable. This variable does not count COM_PING, COM_STATISTICS, COM_STMT_PREPARE, COM_STMT_CLOSE, or COM_STMT_RESET commands.(Counter)
  9. mysql_global_status_threads_cached: The number of threads in the thread cache.(Counter)
  10. mysql_global_status_threads_created: The number of threads created to handle connections. If Threads_created is big, you may want to increase the thread_cache_size value. The cache miss rate can be calculated as Threads_created/Connections.(Counter)
  11. mysql_global_status_created_tmp_tables: The number of internal temporary tables created by the server while executing statements.(Counter)
  12. mysql_global_status_created_tmp_disk_tables: The number of internal on-disk temporary tables created by the server while executing statements. You can compare the number of internal on-disk temporary tables created to the total number of internal temporary tables created by comparing Created_tmp_disk_tables and Created_tmp_tables values.(Counter)
  13. mysql_global_status_created_tmp_files: How many temporary files mysqld has created.(Counter)
  14. mysql_global_status_select_full_join: The number of joins that perform table scans because they do not use indexes. If this value is not 0, you should carefully check the indexes of your tables.(Counter)
  15. mysql_global_status_select_full_range_join: The number of joins that used a range search on a reference table.(Counter)
  16. mysql_global_status_select_range: The number of joins that used ranges on the first table. This is normally not a critical issue even if the value is quite large.(Counter)
  17. mysql_global_status_select_range_check: The number of joins without keys that check for key usage after each row. If this is not 0, you should carefully check the indexes of your tables.(Counter)
  18. mysql_global_status_select_scan: The number of joins that did a full scan of the first table.(Counter)
  19. mysql_global_status_sort_rows: The number of sorted rows.(Counter)
  20. mysql_global_status_sort_range: The number of sorts that were done using ranges.(Counter)
  21. mysql_global_status_sort_merge_passes: The number of merge passes that the sort algorithm has had to do. If this value is large, you should consider increasing the value of the sort_buffer_size system variable.(Counter)
  22. mysql_global_status_sort_scan: The number of sorts that were done by scanning the table.(Counter)
  23. mysql_global_status_slow_queries: The number of queries that have taken more than long_query_time seconds. This counter increments regardless of whether the slow query log is enabled.(Counter)
  24. mysql_global_status_aborted_connects: The number of failed attempts to connect to the MySQL server.(Counter)
  25. mysql_global_status_aborted_clients: The number of connections that were aborted because the client died without closing the connection properly.(Counter)
  26. mysql_global_status_table_locks_immediate: The number of times that a request for a table lock could be granted immediately. Locks Immediate rising and falling is normal activity.(Counter)
  27. mysql_global_status_table_locks_waited: The number of times that a request for a table lock could not be granted immediately and a wait was needed. If this is high and you have performance problems, you should first optimize your queries, and then either split your table or tables or use replication.(Counter)
  28. mysql_global_status_bytes_received: The number of bytes received from all clients.(Counter)
  29. mysql_global_status_bytes_sent: The number of bytes sent to all clients.(Counter)
  30. mysql_global_status_innodb_page_size: InnoDB page size (default 16KB). Many values are counted in pages; the page size enables them to be easily converted to bytes.(Gauge)
  31. mysql_global_status_buffer_pool_pages: The number of pages in the InnoDB buffer pool.(Gauge)
  32. mysql_global_status_commands_total: The number of times each xxx statement has been executed.(Counter)
  33. mysql_global_status_handlers_total: Handler statistics are internal statistics on how MySQL is selecting, updating, inserting, and modifying rows, tables, and indexes. This is in fact the layer between the Storage Engine and MySQL.(Counter)
  34. mysql_global_status_opened_files: The number of files that have been opened with my_open() (a mysys library function). Parts of the server that open files without using this function do not increment the count.(Counter)
  35. mysql_global_status_open_tables: The number of tables that are open.(Gauge)
  36. mysql_global_status_opened_tables: The number of tables that have been opened. If Opened_tables is big, your table_open_cache value is probably too small.(Counter)
  37. mysql_global_status_table_open_cache_hits: The number of hits for open tables cache lookups.(Counter)
  38. mysql_global_status_table_open_cache_misses: The number of misses for open tables cache lookups.(Counter)
  39. mysql_global_status_table_open_cache_overflows: The number of overflows for the open tables cache.(Counter)
  40. mysql_global_status_innodb_num_open_files: The number of files InnoDB currently holds open.(Gauge)
  41. mysql_global_variables_thread_cache_size: How many threads the server should cache for reuse.(Gauge)
  42. mysql_global_variables_max_connections: The maximum permitted number of simultaneous client connections.(Gauge)
  43. mysql_global_variables_innodb_buffer_pool_size: The size in bytes of the buffer pool, the memory area where InnoDB caches table and index data. The default value is 134217728 bytes (128MB).(Gauge)
  44. mysql_global_variables_innodb_log_buffer_size: The size in bytes of the buffer that InnoDB uses to write to the log files on disk.(Gauge)
  45. mysql_global_variables_key_buffer_size: Index blocks for MyISAM tables are buffered and are shared by all threads.(Gauge)
  46. mysql_global_variables_query_cache_size: The amount of memory allocated for caching query results.(Gauge)
  47. mysql_global_variables_table_open_cache: The number of open tables for all threads.(Gauge)
  48. mysql_global_variables_open_files_limit: The number of file descriptors available to mysqld from the operating system.(Gauge)

mysqld-exporter-config详细配置项说明

  1. # Enables the mysqld_exporter integration, allowing the Agent to collect
  2. # metrics from a MySQL server.
  3. [enabled: <boolean> | default = false]
  4. # Sets an explicit value for the instance label when the integration is
  5. # self-scraped. Overrides inferred values.
  6. #
  7. # The default value for this integration is a truncated version of the
  8. # connection DSN, containing only the server and db name. (Credentials
  9. # are not included.)
  10. [instance: <string>]
  11. # Automatically collect metrics from this integration. If disabled,
  12. # the mysqld_exporter integration will be run but not scraped and thus not
  13. # remote-written. Metrics for the integration will be exposed at
  14. # /integrations/mysqld_exporter/metrics and can be scraped by an external
  15. # process.
  16. [scrape_integration: <boolean> | default = <integrations_config.scrape_integrations>]
  17. # How often should the metrics be collected? Defaults to
  18. # prometheus.global.scrape_interval.
  19. [scrape_interval: <duration> | default = <global_config.scrape_interval>]
  20. # The timeout before considering the scrape a failure. Defaults to
  21. # prometheus.global.scrape_timeout.
  22. [scrape_timeout: <duration> | default = <global_config.scrape_timeout>]
  23. # Allows for relabeling labels on the target.
  24. relabel_configs:
  25. [- <relabel_config> ... ]
  26. # Relabel metrics coming from the integration, allowing to drop series
  27. # from the integration that you don't care about.
  28. metric_relabel_configs:
  29. [ - <relabel_config> ... ]
  30. # How frequent to truncate the WAL for this integration.
  31. [wal_truncate_frequency: <duration> | default = "60m"]
  32. # Data Source Name specifies the MySQL server to connect to. This is REQUIRED
  33. # but may also be specified by the MYSQLD_EXPORTER_DATA_SOURCE_NAME
  34. # environment variable. If neither are set, the integration will fail to
  35. # start.
  36. #
  37. # The format of this is specified here: https://github.com/go-sql-driver/mysql#dsn-data-source-name
  38. #
  39. # A working example value for a server with no required password
  40. # authentication is: "root@(localhost:3306)/"
  41. data_source_name: <string>
  42. # A list of collector names to enable on top of the default set.
  43. enable_collectors:
  44. [ - <string> ]
  45. # A list of collector names to disable from the default set.
  46. disable_collectors:
  47. [ - <string> ]
  48. # A list of collectors to run. Fully overrides the default set.
  49. set_collectors:
  50. [ - <string> ]
  51. # Set a lock_wait_timeout on the connection to avoid long metadata locking.
  52. [lock_wait_timeout: <int> | default = 2]
  53. # Add a low_slow_filter to avoid slow query logging of scrapes. NOT supported
  54. # by Oracle MySQL.
  55. [log_slow_filter: <bool> | default = false]
  56. ## Collector-specific options
  57. # Minimum time a thread must be in each state to be counted.
  58. [info_schema_processlist_min_time: <int> | default = 0]
  59. # Enable collecting the number of processes by user.
  60. [info_schema_processlist_processes_by_user: <bool> | default = true]
  61. # Enable collecting the number of processes by host.
  62. [info_schema_processlist_processes_by_host: <bool> | default = true]
  63. # The list of databases to collect table stats for. * for all
  64. [info_schema_tables_databases: <string> | default = "*"]
  65. # Limit the number of events statements digests by response time.
  66. [perf_schema_eventsstatements_limit: <int> | default = 250]
  67. # Limit how old the 'last_seen' events statements can be, in seconds.
  68. [perf_schema_eventsstatements_time_limit: <int> | default = 86400]
  69. # Maximum length of the normalized statement text.
  70. [perf_schema_eventsstatements_digtext_text_limit: <int> | default = 120]
  71. # Regex file_name filter for performance_schema.file_summary_by_instance
  72. [perf_schema_file_instances_filter: <string> | default = ".*"]
  73. # Remove path prefix in performance_schema.file_summary_by_instance
  74. [perf_schema_file_instances_remove_prefix: <string> | default = "/var/lib/mysql"]
  75. # Database from where to collect heartbeat data.
  76. [heartbeat_database: <string> | default = "heartbeat"]
  77. # Table from where to collect heartbeat data.
  78. [heartbeat_table: <string> | default = "heartbeat"]
  79. # Use UTC for timestamps of the current server (`pt-heartbeat` is called with `--utc`)
  80. [heartbeat_utc: <bool> | default = false]
  81. # Enable collecting user privileges from mysql.user
  82. [mysql_user_privileges: <bool> | default = false]