在前文中我们介绍了 Prometheus 的基础知识,并演示了如何监控一台 Linux 服务器。这次我们来聊聊如何将 Prometheus 应用到几个常见的业务组件上,实现更细粒度的监控。
监控 MySQL 服务器
Prometheus 通过 Exporter 收集远程数据,对于 MySQL,我们需要用到 mysqld_exporter。
1. 部署架构与安装
首先下载并解压 exporter 二进制文件:
$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
$ tar xzvf mysqld_exporter-0.10.0.linux-amd64.tar.gz
$ mv mysqld_exporter-0.10.0.linux-amd64 /usr/local/prometheus/mysqld_exporter
2. 创建监控用户
Exporter 需要连接数据库获取指标,因此要创建一个专用账号并授权:
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' IDENTIFIED BY '000000';
mysql> GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
mysql> FLUSH PRIVILEGES;
3. 配置文件与服务启动
默认情况下,exporter 会读取 ~/.my.cnf。为了方便管理,我们将其放在安装目录下:
$ vim /usr/local/prometheus/mysqld_exporter/.my.cnf
[client]
user=mysqld_exporter
password=000000
接着配置 Systemd 服务,确保进程随系统启动并自动重启:
[Unit]
Description=mysql_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/mysqld_exporter/mysqld_exporter -config.my-cnf="/usr/local/prometheus/mysqld_exporter/.my.cnf"
=-failure
=multi-user.target

