1、生成密钥

root@localhost:# htpasswd -nBC 10 "" | tr -d ':\n'; echo
New password:  #输入密钥
Re-type new password:  #在输入一次密钥
$2y$10$h0mrhNIKF2f2yrtol3rcEOYtUuDa2X.GEfViuSjIl4lO.fVXlwOkG

2、创建basic_auth_users文件

root@ceshi8server:~# cat basic_auth.yaml 
basic_auth_users:
  admin: $2y$10$h0mrhNIKF2f2yrtol3rcEOYtUuDa2X.GEfViuSjIl4lO.fVXlwOkG

3、创建docker-compose配置文件

services:
  mysqld_exporter:
    image: prom/mysqld-exporter:v0.16.0
    container_name: mysqld_exporter
    hostname: mysqld_exporter
    restart: unless-stopped
    environment:
      MYSQLD_EXPORTER_PASSWORD: Seca@2024...
      MYSQLD_EXPORTER_USER: root
      MYSQLD_EXPORTER_ADDRESS: "10.84.3.47:3306"
    volumes:
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
      - ./basic_auth.yaml:/etc/basic_auth.yaml:ro
    entrypoint:
      - /bin/mysqld_exporter
      - --collect.info_schema.innodb_metrics
      - --collect.info_schema.tables
      - --collect.info_schema.processlist
      - --collect.info_schema.tables.databases=*
      - --mysqld.username=root
      - --collect.perf_schema.eventsstatements.digest_text_limit=120
      - --collect.perf_schema.file_events
      - --collect.global_status
      - --collect.global_variables
      - --web.config.file=/etc/basic_auth.yaml
    expose:
      - 9104
    network_mode: "host"

MYSQLD_EXPORTER_PASSWORD: MySQL 数据库的密码,供 mysqld_exporter 使用连接到 MySQL 实例。

MYSQLD_EXPORTER_USER: 指定 MySQL 用户名,供 mysqld_exporter 用来连接 MySQL 数据库。
MYSQLD_EXPORTER_ADDRESS: 指定 MySQL 实例的地址,mysqld_exporter 将连接此地址来收集指标。
--collect.info_schema.innodb_metrics: 启用从 INFORMATION_SCHEMA.INNODB_METRICS 表中采集 InnoDB 存储引擎的相关指标包括但不限于 缓冲池大小、页面读取/写入、事务操作等信息。

--collect.info_schema.tables: 启用从 INFORMATION_SCHEMA.TABLES 表中采集每个表的大小信息,包括表的大小、行数等。

--collect.info_schema.processlist: 启用从 INFORMATION_SCHEMA.PROCESSLIST 表中采集当前正在执行的 会话的状态、执行的查询、连接状态等信息。

--collect.info_schema.tables.databases=*: 启用对所有数据库中的表信息的采集。

--mysqld.username=root : mysqld_exporter 连接 MySQL 时使用的用户名。

--collect.perf_schema.eventsstatements.digest_text_limit=120: 限制 events_statements 中 SQL 语句的字符数(如 SQL 语句的内容)为 120 字符,限制了导出器收集的 SQL 语句的字符长度。这样可以防止长 SQL 语句占用过多空间。

--collect.perf_schema.file_events: 启用从 performance_schema.file_events 表中采集文件 I/O 操作的相关指标该表记录了与文件操作(如读取、写入、创建、删除文件等)相关的信息。

--collect.global_status: 启用从 SHOW GLOBAL STATUS 命令获取的全局状态变量包括但不限于连接数、查询数、表扫描数、缓存命中率等。

--collect.global_variables: 启用从 SHOW GLOBAL VARIABLES 命令获取的全局配置变量包括但不限于max_connections、innodb_buffer_pool_size、query_cache_size等。

--web.config.file=/etc/basic_auth.yaml: 指定用于 Web 服务的基本认证配置文件路径。

4、创建容器并测试访问metrics

root@ceshi8server:~# docker-compose up -d
[+] Running 1/1
 ✔ Container mysqld_exporter  Started                                                                                                                                                                                                   0.0s 
root@ceshi8server:~# docker-compose logs -f
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:239 msg="Starting mysqld_exporter" version="(version=0.16.0, branch=HEAD, revision=c366424252a3140838a344a6cb0d40d44f1be91d)"
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:240 msg="Build context" build_context="(go=go1.23.3, platform=linux/amd64, user=root@ad5ebc2e1a44, date=20241108-16:00:22, tags=unknown)"
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=global_variables
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=slave_status
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.processlist
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.tables
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.innodb_metrics
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=global_status
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=perf_schema.file_events
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.innodb_cmp
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.innodb_cmpmem
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=mysqld_exporter.go:252 msg="Scraper enabled" scraper=info_schema.query_response_time
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=tls_config.go:347 msg="Listening on" address=[::]:9104
mysqld_exporter  | time=2025-01-16T10:20:51.485+08:00 level=INFO source=tls_config.go:386 msg="TLS is disabled." http2=false address=[::]:9104


root@ceshi8server:~# curl -u admin:Sec@2024... 10.84.3.47:9104/metrics | tail -n 20
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2196k    0 2196k    0     0  6034k      0 --:--:-- --:--:-- --:--:-- 6050k
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 2.72384e+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.73699405119e+09
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1.272434688e+09
# 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.8446744073709552e+19
# 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"} 14
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

Promtheus端配置

1、新增job

  - job_name: 'mysql_exporter'
    basic_auth:
      username:  admin
      password: Sec@2024...
    file_sd_configs:
    - files:
      - /apps/prometheus/Discovery/mysql_exporter.yml
      refresh_interval: 5s

2、创建自动发现文件

#cat /apps/prometheus/Discovery/mysql_exporter.yml
- targets:
  - 10.84.3.47:9104
  labels:
    app: MySQLv5.7.44
    role: master
- targets:
  - 10.84.3.46:9104
  labels:
    app: MySQLv5.7.44
    role: slave

3、重新加载配置使其生效

curl -X POST "http://10.84.3.241:9090/-/reload"

4、检查Prometheus target 状态,验证能否正常pull数据

检查Prometheus target 状态
root@Monitoring-Center:/apps/prometheus# curl -s -u admin:Sec@2024... http://10.84.3.241:9090/api/v1/targets | jq '.data.activeTargets[].health'
"up"
"up"

验证能否正常pull数据
root@Monitoring-Center:/apps/prometheus# curl -s -u admin:Sec@2024... 'http://10.84.3.241:9090/api/v1/query?query=mysql_global_status_threads_connected' | jq .
{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "__name__": "mysql_global_status_threads_connected",
          "app": "MySQLv5.7.44",
          "instance": "10.84.3.46:9104",
          "job": "mysql_exporter",
          "role": "slave"
        },
        "value": [
          1736995335.354,
          "1"
        ]
      },
      {
        "metric": {
          "__name__": "mysql_global_status_threads_connected",
          "app": "MySQLv5.7.44",
          "instance": "10.84.3.47:9104",
          "job": "mysql_exporter",
          "role": "master"
        },
        "value": [
          1736995335.354,
          "111"
        ]
      }
    ]
  }
}
最后修改:2025 年 01 月 16 日
如果觉得我的文章对你有用,请随意赞赏