【MySQL】MySQL Exporter

Posted by 西维蜀黍 on 2022-04-01, Last Modified on 2023-05-02

Download

$ curl -LO https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

$ tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz

Running

Running using an environment variable:

export DATA_SOURCE_NAME='user:password@(hostname:3306)/' ./mysqld_exporter <flags>

开机运行

$ sudo vim /home/sw/start_mysqld_exporter.sh
#!/bin/bash

export DATA_SOURCE_NAME='exporter:zVP#6*n16vla@(localhost:3306)/';
/home/sw/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter

$ sudo vim /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/bash /home/sw/start_mysqld_exporter.sh

[Install]
WantedBy=multi-user.target


$ sudo systemctl enable mysqld_exporter.service; sudo systemctl start mysqld_exporter.service; sudo systemctl status mysqld_exporter.service

Required Grants

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
flush privileges;

# 如果要修改密码
ALTER USER 'exporter'@'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;

Test

MySQL Exporter publishes MySQL metrics in Prometheus format on port 9104. You can test this using curl. You will need to open a new SSH session or background the MySQL Exporter process to use curl.

$ curl http://localhost:9104/metrics
. . .
process_resident_memory_bytes 1.2079104e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.61377616023e+09
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1.16355072e+08
# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.
# TYPE process_virtual_memory_max_bytes gauge
process_virtual_memory_max_bytes -1
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 1
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

If you see the above output, you’re ready to begin scraping MySQL metrics using Prometheus.

Scraping MySQL Exporter using Prometheus

If you don’t have a prometheus.yml configuration file, create a simple one using your favorite text editor. Open your preferred text editor and paste in the following Prometheus configuration:

global:
  scrape_interval: 15s

scrape_configs:
- job_name: mysql
  static_configs:
  - targets: ['mysql_exporter_machine_IP_address:9104']

Configuring Grafana dashboards

ou can import the dashboard directly using the following dashboard ID: 14057.

Reference