简介
mysql_exporter是用来收集MysQL或者Mariadb数据库相关指标的,mysql_exporter需要连接到数据库并有相关权限。既可以用二进制安装部署,也可以通过容器形式部署,但为了数据收集的准确性,推荐二进制安装。
一,下载安装包并解压
安装包下载地址: https://github.com/prometheus/mysqld_exporter/releases
同样物理机上下载,然后上传到服务器,本次安装使用的版本为:mysqld_exporter-0.14.0.linux-amd64
1,根据服务器情况选择安装目录,上传安装包。
mkdir /mysqld_exporter
cd /mysqld_exporter/
[root@jd-mysql mysqld_exporter]# ls
mysqld_exporter-0.14.0.linux-amd64.tar.gz
#解压
[root@jd-mysql mysqld_exporter]# tar -xvzf mysqld_exporter-0.14.0.linux-amd64.tar.gz
[root@jd-mysql mysqld_exporter]# cp mysqld_exporter-0.14.0.linux-amd64/* .
[root@jd-mysql mysqld_exporter]# rm -rf mysqld_exporter-0.14.0.linux-amd64
[root@jd-mysql mysqld_exporter]# ls
LICENSE mysqld_exporter mysqld_exporter-0.14.0.linux-amd64.tar.gz NOTICE
二,创建配置文件
- 创建配置文件
.my.cnf
,填入如下内容后保存:wq
[root@jd-mysql mysqld_exporter]# cat .my.cnf
[client]
user = exporter
password = Jd123456
host = 192.168.1.150
port = 3306
- mysql创建用户并授权,注意服务ip不要填错
[root@jd-mysql mysqld_exporter]# mysql -uroot -pJiangda123#
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.40-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 11:06 mysql>create user 'exporter'@'%' identified by 'Jd123456';
Query OK, 0 rows affected (1.02 sec)
root@(none) 11:07 mysql>GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%' WITH MAX_USER_CONNECTIONS 3;
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@(none) 11:07 mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@(none) 11:07 mysql>exit
Bye
三,进行系统service编写
- 创建
mysqld_exporter.service
配置文件
cd /usr/lib/systemd/system
vim mysqld_exporter.service
- mysqld_exporter.service 文件填入如下内容后保存
:wq
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/mysqld_exporter/mysqld_exporter --config.my-cnf=/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部署之前,首先要确保拥有Docker环境
-
拉取mysqld-exporter最新镜像
docker pull prom/mysqld-exporter
- 创建mysql监听用户并授权,注意服务ip不要填错
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
- 打开
prometheus.yml
添加以下内容,并执行:wq
,点击回车保存
- job_name: "mysql"
static_configs:
- targets: ["192.168.1.150:9104"]
labels:
instance: mysqld-exporter
- 执行prometheus配置刷新
curl -XPOST localhost:9090/-/reload
-
打开prometheus,搜索mysql,可以看到出现了很多的参数指标
-
打开targets,可以看到节点状态已经UP
-
至此mysqld-exporter插件安装完毕
用Grafana出图
[root@nfs yum.repos.d]# mkdir /grafana
[root@nfs yum.repos.d]# cd /grafana/
[root@nfs grafana]# ls
grafana-enterprise-9.1.2-1.x86_64.rpm
[root@nfs grafana]# yum install grafana-enterprise-9.1.2-1.x86_64.rpm -y
安装好后
[root@nfs grafana]# service grafana-server start
Starting grafana-server (via systemctl): [ 确定 ]
[root@nfs grafana]# systemctl enable grafana-server
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@nfs grafana]# netstat -anplut|grep grafana
tcp 0 0 192.168.1.130:56948 34.120.177.193:443 ESTABLISHED 8775/grafana-server
tcp 0 0 192.168.1.130:48842 185.199.110.133:443 ESTABLISHED 8775/grafana-server
tcp6 0 0 :::3000 :::* LISTEN 8775/grafana-server
访问http://192.168.1.130:3000/
默认的用户名和密码是
用户名admin
密码admin
配置数据源
成功效果: