mysql_exporter是用来收集MysQL或者Mariadb数据库相关指标的,mysql_exporter需要连接到数据库并有相关权限。既可以用二进制安装部署,也可以通过容器形式部署,但为了数据收集的准确性,推荐二进制安装。
**安装包下载地址:**https://github.com/prometheus/mysqld_exporter/releases
同样物理机上下载,然后上传到服务器,本次安装使用的版本为:mysqld_exporter-0.14.0.linux-amd64
cd /prometheus
#解压
tar -xvzf mysqld_exporter-0.14.0.linux-amd64.tar.gz
mv mysqld_exporter-0.14.0.linux-amd64 mysqld_exporter
cd mysqld_exporter
.my.cnf
,填入如下内容后保存:wq
[client]
user = exporter
password = <your password>
host = 11x.xx.xx.7
port = <your port>
vim .my.cnf
cat .my.cnf
CREATE USER 'exporter'@'<安装Prometheus的主机IP>' IDENTIFIED BY '<your password>';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'<安装Prometheus的主机IP>';
flush privileges;
mysqld_exporter.service
配置文件cd /usr/lib/systemd/system
vim mysqld_exporter.service
:wq
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/prometheus/mysqld_exporter/.my.cnf --web.listen-address=:9104
[Install]
WantedBy=multi-user.target
cat mysqld_exporter.service
systemctl daemon-reload
systemctl start mysqld_exporter.service
systemctl status mysqld_exporter.service
systemctl enable mysqld_exporter.service
curl localhost:9104/metrics
在Docker部署之前,首先要确保拥有Docker环境
拉取mysqld-exporter最新镜像
docker pull prom/mysqld-exporter
CREATE USER 'exporter'@'<安装Prometheus的主机IP>' IDENTIFIED BY '<your password>';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'<安装Prometheus的主机IP>';
flush privileges;
systemctl stop mysqld_exporter.service
关闭服务,否则会造成端口冲突,或者docker的端口映射改为-p 9194:9104
也可。注意环境变量DATA_SOURCE_NAME
的mysql帐号密码与地址无误docker run -d --restart=always --name mysqld-exporter -p 9104:9104 -e DATA_SOURCE_NAME="exporter:<pass>(11x.xx.xx.7:13306)/" prom/mysqld-exporter
curl localhost:9104/metrics
prometheus.yml
添加以下内容,并执行:wq
,点击回车保存 # 监控mysql
- job_name: 'mysql'
static_configs:
- targets: ['11x.xx.xx.7:9104']
labels:
instance: mysqld-exporter
curl -XPOST localhost:9090/-/reload