mysql_exporter安装教程

一、下载安装包

https://github.com/prometheus/prometheus/releases/

二、编写启动脚本

vim /lib/systemd/system/mysql_exporter.service

[Unit]
Description=mysqld_exporte
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/mysqld_exporter
#Environment=DATA_SOURCE_NAME=exporter:password123@tcp(192.168.4.88:3306)/
ExecStart=/usr/bin/sh -c '/opt/mysqld_exporter/mysqld_exporter --web.listen-address=0.0.0.0:9104 \
  --config.my-cnf=./my.cnf \
  --collect.slave_status \
  --collect.slave_hosts \
  --collect.info_schema.processlist \
  --collect.info_schema.innodb_metrics \
  --collect.info_schema.innodb_tablespaces \
  --collect.info_schema.innodb_cmp \
  --collect.info_schema.innodb_cmpmem \
  --log.level=info >./mysqld_exporter.log 2>&1'
Restart=on-failure

[Install]
WantedBy=multi-user.targe

PS: 安装路径和启动监听端口<9104>按需修改<外网部署注意安全风险>,如果启用Environment参数连接MySQL则--config.my-cnf参数无效,二选一连接方式

vim ./my.cnf   # MySQL server的连接信息
[client]
host=192.168.4.88
port=3306
user=mysql_exporter
password=password12345

三、创建MySQL用户,如果localhost改成%请注意外网安全风险

mysql > CREATE USER 'mysql_exporter'@'localhost' IDENTIFIED BY 'password123';
Query OK, 0 rows affected (0.11 sec)
MariaDB [(none)]> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'localhost';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

四、启动服务

systemctl start mysql_exporter    # 启动服务
systemctl enable mysql_exporter      # 开机自启动

PS:如果再次修改启动脚本需要执行: systemctl daemon-reload

五、接入prometheus在配置文件末尾加入以下配置

vim prometheus.yml
  - job_name: mysql
    metrics_path: /metrics
    scheme: http
    static_configs:
    - targets:
      - "ip:9104"

刷新prometheus配置: curl -X POST http://ip:9090/-/reload
打开prometheus页面显示如下表示接入成功: http://ip:9090/targets

六、grafana配置

打开导入页面: http://ip:port/dashboard/import
本文举例使用https://grafana.com/grafana/dashboards/7362 MySQL Overview 模板用于展示

讨论数量: 0

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!