安装Mysql - Prometheus监控

参考原文:Prometheus+Grafana监控MySQL - 知乎 (zhihu.com)

安装

  1. 下载文件:wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

  2. 解压:tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz

  3. 创建Mysql用户名用于监控数据

    1
    2
    CREATE USER 'mysql_node_exporter'@'localhost' IDENTIFIED BY '123456789';
    grant select,replication client, process on *.* to 'mysql_node_exporter'@'localhost';
  4. 创建配置文件:

    1
    2
    3
    4
    vim /opt/sofeware/mysql_node_exporter/.my.cnf
    [client]
    user=mysql_node_exporter
    password=123456789
  5. 创建Service:vim /etc/systemd/system/mysql_node_exporter.service

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=mysql_node_exporter Monitoring System
    Documentation=mysql_node_exporter Monitoring System

    [Service]
    Type=simple
    ExecStart=/opt/sofeware/mysql_node_exporter/mysqld_exporter --config.my-cnf=/opt/sofeware/mysql_node_exporter/.my.cnf
    Restart=on-failure
    ExecStop=/bin/kill -INT $MAINPID

    [Install]
    WantedBy=multi-user.target
  6. 设置启动项

    1
    2
    3
    4
    systemctl daemon-reload
    systemctl enable mysql_node_exporter.service
    systemctl start mysql_node_exporter.service
    systemctl status mysql_node_exporter.service
  7. 查看端口是否启动:netstat -lnptu | grep 9104

    1
    2
    3
    4
    byz-db@database-server:~$ netstat -lnptu | grep 9104
    (Not all processes could be identified, non-owned process info
    will not be shown, you would have to be root to see it all.)
    tcp6 0 0 :::9104
  8. 测试:curl localhost:9104/metrics

成功::注意放行防火墙端口!!

配置Prometheus

修改Prometheus配置文件:vim /opt/sofeware/prometheus/prometheus.yml

1
2
3
4
scrape_configs:
  - job_name: 'mysql-vitral'
    static_configs:
    - targets: ['192.168.0.7:9104']
  1. 重启服务:systemctl restart prometheus.service

配置Grafana

  1. 安装模板:MySQL Overview | Grafana Labs
  2. 导入模板:

点击Import即可! 之后查看效果: