1.1 下载mysqld_exporter

  1. [root@mysql-38 src]# cd /usr/local/src/
  2. [root@mysql-38 src]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
  3. [root@mysql-38 src]# ls
  4. mysqld_exporter-0.12.1.linux-amd64.tar.gz

1.2 解压exporter

  1. [root@mysql-38 src]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
  2. [root@mysql-38 src]# mv mysqld_exporter-0.12.1.linux-amd64 ../
  3. [root@mysql-38 src]# cd ..
  4. [root@mysql-38 local]# mv mysqld_exporter-0.12.1.linux-amd64/ mysqld_exporter

1.3 创建mysql用户,并授予权限

因为mysql_exporter需要连接到mysql,所以需要创建mysql用户监控mysql数据库,并赋予权限

  1. # mysql -uroot -p123456
  2. > create user 'exporter'@'%' identified by '123456';
  3. > GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'exporter'@'%';
  4. > GRANT SELECT ON performance_schema.* TO 'exporter'@'%';
  5. > ALTER USER exporter@'%' IDENTIFIED WITH mysql_native_password BY '123456';
  6. > flush privileges;

1.4 创建.my.cnf

  1. [root@mysql-38 mysqld_exporter]# vim .my.cnf
  2. [client]
  3. user=exporter #创建的mysql用户
  4. password=123456 #用户密码

1.5 把exporter设置为系统程序

  1. [root@mysql-38 local]# vim /etc/systemd/system/mysqld_exporter.service
  2. [Unit]
  3. Description=mysqld_exporter
  4. Documentation=https://prometheus.io/
  5. After=network.target
  6. [Service]
  7. Type=simple
  8. ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  12. [root@mysql-38 local]# systemctl daemon-reload
  13. [root@mysql-38 local]# systemctl enable mysqld_exporter
  14. [root@mysql-38 local]# systemctl start mysqld_exporter
  15. [root@mysql-38 mysqld_exporter]# ss -lntp|grep 9104
  16. LISTEN 0 128 :::9104 :::* users:(("mysqld_exporter",pid=28564,fd=3))

1.6 配置prometheus

  1. [root@prometheus prometheus]# cd /usr/local/prometheus/
  2. [root@prometheus prometheus]# vim prometheus.yml
  3. # my global config
  4. global:
  5. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  6. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  7. # scrape_timeout is set to the global default (10s).
  8. # Alertmanager configuration
  9. alerting:
  10. alertmanagers:
  11. - static_configs:
  12. - targets:
  13. # - alertmanager:9093
  14. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  15. rule_files:
  16. # - "first_rules.yml"
  17. # - "second_rules.yml"
  18. # A scrape configuration containing exactly one endpoint to scrape:
  19. # Here it's Prometheus itself.
  20. scrape_configs:
  21. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  22. - job_name: 'prometheus'
  23. # metrics_path defaults to '/metrics'
  24. # scheme defaults to 'http'.
  25. static_configs:
  26. - targets: ['localhost:9090']
  27. - job_name: 'Linux'
  28. static_configs:
  29. - targets: ['10.4.7.58:9100']
  30. #添加下面三行
  31. - job_name: 'Mysql'
  32. static_configs:
  33. - targets: ['10.4.7.38:9104']
  34. [root@prometheus prometheus]# systemctl restart prometheus

1.7 Grafana监控mysql

image.png
image.png
image.png

最后的结果图:
image.png