安装

  1. # wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
  2. # tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt
  3. # mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter

创建mysql只读账户

  1. mysql> create user 'mysqld_exporter'@'%' identified by '1qaz@WSX';
  2. GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'%' WITH MAX_USER_CONNECTIONS 3;
  3. flush privileges;

创建mysqld_exporter配置文件

  1. # cat <<EOF>> /opt/mysqld-exporter/my.cnf
  2. [client]
  3. user=mysqld_exporter
  4. password=1qaz@WSX
  5. port=3306
  6. host=127.0.0.1
  7. EOF

创建systemd启动文件

  1. cat <<EOF>> /usr/lib/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. User=root
  9. ExecStart=/opt/mysqld_exporter/mysqld_exporter \
  10. --config.my-cnf "/opt/mysqld_exporter/my.cnf" \
  11. --collect.global_status \
  12. --collect.info_schema.innodb_metrics \
  13. --collect.auto_increment.columns \
  14. --collect.info_schema.processlist \
  15. --collect.binlog_size \
  16. --collect.info_schema.tablestats \
  17. --collect.global_variables \
  18. --collect.info_schema.query_response_time \
  19. --collect.info_schema.userstats \
  20. --collect.info_schema.tables \
  21. --collect.perf_schema.tablelocks \
  22. --collect.perf_schema.file_events \
  23. --collect.perf_schema.eventswaits \
  24. --collect.perf_schema.indexiowaits \
  25. --collect.perf_schema.tableiowaits \
  26. --collect.slave_status \
  27. --web.listen-address=0.0.0.0:9104
  28. ExecReload=/bin/kill -HUP $MAINPID
  29. Restart=on-failure
  30. [Install]
  31. WantedBy=multi-user.target
  32. EOF

启动mysqld-exporter

  1. systemctl enable mysqld-exporter
  2. systemctl start mysqld-exporter

配置prometheus抓取数据

这里使用的prometheus-operator部署的,所以配置serviceMonitor进行抓取,如下:

  1. ---
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: mysqld-exporter
  6. namespace: monitoring
  7. labels:
  8. k8s-app: mysqld-exporter
  9. spec:
  10. type: ClusterIP
  11. clusterIP: None
  12. ports:
  13. - name: http
  14. port: 9104
  15. protocol: TCP
  16. ---
  17. apiVersion: v1
  18. kind: Endpoints
  19. metadata:
  20. name: mysqld-exporter
  21. namespace: monitoring
  22. labels:
  23. k8s-app: mysqld-exporter
  24. subsets:
  25. - addresses:
  26. - ip: 172.16.0.185
  27. ports:
  28. - name: http
  29. port: 9104
  30. protocol: TCP
  31. ---
  32. apiVersion: monitoring.coreos.com/v1
  33. kind: ServiceMonitor
  34. metadata:
  35. name: mysqld-exporter
  36. namespace: monitoring
  37. labels:
  38. k8s-app: mysqld-exporter
  39. chart: prometheus-operator-8.5.0
  40. heritage: Helm
  41. release: prometheus
  42. spec:
  43. jobLabel: k8s-app
  44. endpoints:
  45. - port: http
  46. interval: 30s
  47. scheme: http
  48. selector:
  49. matchLabels:
  50. k8s-app: mysqld-exporter
  51. namespaceSelector:
  52. matchNames:
  53. - monitoring

添加grafana面板

导入7362即可。
**

配置告警规则

  1. groups:
  2. - name: MySQLStatsAlert
  3. rules:
  4. - alert: MySQL is down
  5. expr: mysql_up == 0
  6. for: 1m
  7. labels:
  8. severity: critical
  9. annotations:
  10. summary: "Instance {{ $labels.instance }} MySQL is down"
  11. description: "MySQL database is down. This requires immediate action!"
  12. - alert: Mysql_High_QPS
  13. expr: rate(mysql_global_status_questions[5m]) > 500
  14. for: 2m
  15. labels:
  16. severity: warning
  17. annotations:
  18. summary: "{{$labels.instance}}: Mysql_High_QPS detected"
  19. description: "{{$labels.instance}}: Mysql opreation is more than 500 per second ,(current value is: {{ $value }})"
  20. - alert: Mysql_Too_Many_Connections
  21. expr: rate(mysql_global_status_threads_connected[5m]) > 200
  22. for: 2m
  23. labels:
  24. severity: warning
  25. annotations:
  26. summary: "{{$labels.instance}}: Mysql Too Many Connections detected"
  27. description: "{{$labels.instance}}: Mysql Connections is more than 100 per second ,(current value is: {{ $value }})"
  28. - alert: Mysql_Too_Many_slow_queries
  29. expr: rate(mysql_global_status_slow_queries[5m]) > 3
  30. for: 2m
  31. labels:
  32. severity: warning
  33. annotations:
  34. summary: "{{$labels.instance}}: Mysql_Too_Many_slow_queries detected"
  35. description: "{{$labels.instance}}: Mysql slow_queries is more than 3 per second ,(current value is: {{ $value }})"
  36. - alert: SQL thread stopped
  37. expr: mysql_slave_status_slave_sql_running != 1
  38. for: 1m
  39. labels:
  40. severity: critical
  41. annotations:
  42. summary: "Instance {{ $labels.instance }} Sync Binlog is enabled"
  43. description: "SQL thread has stopped. This is usually because it cannot apply a SQL statement received from the master."
  44. - alert: Slave lagging behind Master
  45. expr: rate(mysql_slave_status_seconds_behind_master[5m]) >30
  46. for: 1m
  47. labels:
  48. severity: warning
  49. annotations:
  50. summary: "Instance {{ $labels.instance }} Slave lagging behind Master"
  51. description: "Slave is lagging behind Master. Please check if Slave threads are running and if there are some performance issues!"