环境
prometheus-2.44.0
mysqld_exporter-0.14.0
grafana-enterprise-9.1.2-1.x86_64.rpm
alertmanager-0.25.0.
简介
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
配置数据源
成功效果:
AlertManager预警功能
安装
[root@nfs prometheus]# mkdir /prom_alert
[root@nfs prometheus]# cd /prom_alert/
[root@nfs prom_alert]# ls
alertmanager-0.25.0.linux-amd64.tar.gz
[root@nfs prom_alert]# tar xf alertmanager-0.25.0.linux-amd64.tar.gz
[root@nfs prom_alert]# ls
alertmanager-0.25.0.linux-amd64 alertmanager-0.25.0.linux-amd64.tar.gz
[root@nfs prom_alert]# mv alertmanager-0.25.0.linux-amd64 alertmanager
[root@nfs prom_alert]# cd alertmanager
[root@nfs alertmanager]# ls
alertmanager alertmanager.yml amtool LICENSE NOTICE
[root@nfs alertmanager]# cp alertmanager.yml alertmanager.yml.bak
[root@nfs alertmanager]# vim alertmanager.yml
[root@nfs alertmanager]# pwd
/prom_alert/alertmanager
使用systemd管理,启动 && 开机自启
[root@nfs alertmanager]# cd /usr/lib/systemd/system
[root@nfs system]# vim alertmanager.service
[root@nfs system]# systemctl daemon-reload
[root@nfs system]# systemctl start alertmanager.service
[root@nfs system]# systemctl status alertmanager.service
● alertmanager.service - alertmanager
Loaded: loaded (/usr/lib/systemd/system/alertmanager.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2023-06-10 15:37:44 CST; 12s ago
Docs: https://prometheus.io/
Main PID: 13175 (alertmanager)
Tasks: 7
Memory: 16.9M
CGroup: /system.slice/alertmanager.service
└─13175 /prom_alert/alertmanager/alertmanager --storage.path=/prom_alert/alertman...
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.753Z caller=main.go:240 ...9)"
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.753Z caller=main.go:241 ...6)"
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.757Z caller=cluster.go:1...094
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.759Z caller=cluster.go:6...=2s
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.798Z caller=coordinator....yml
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.798Z caller=coordinator....yml
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.800Z caller=tls_config.g...093
6月 10 15:37:44 nfs alertmanager[13175]: ts=2023-06-10T07:37:44.800Z caller=tls_config.g...093
6月 10 15:37:46 nfs alertmanager[13175]: ts=2023-06-10T07:37:46.759Z caller=cluster.go:7...91s
6月 10 15:37:54 nfs alertmanager[13175]: ts=2023-06-10T07:37:54.763Z caller=cluster.go:6...97s
Hint: Some lines were ellipsized, use -l to show in full.
[root@nfs system]# systemctl enable alertmanager
Created symlink from /etc/systemd/system/multi-user.target.wants/alertmanager.service to /usr/lib/systemd/system/alertmanager.service.
查看端口
[root@nfs system]# netstat -anplut|grep alert
tcp6 0 0 :::9093 :::* LISTEN 13175/alertmanager
tcp6 0 0 :::9094 :::* LISTEN 13175/alertmanager
udp6 0 0 :::9094 :::* 13175/alertmanager
验证 浏览器访问alertmanager管理页面
浏览器访问 http://IP:9093,显示下图表示无误~
[root@nfs prom_alert]# ls
alertmanager prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
alertmanager-0.25.0.linux-amd64.tar.gz
[root@nfs prom_alert]# tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
[root@nfs prom_alert]# mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 prometheus-webhook-dingtalk
[root@nfs prom_alert]# ls
alertmanager prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
alertmanager-0.25.0.linux-amd64.tar.gz webhook-dingtalk
[root@nfs prom_alert]# cd prometheus-webhook-dingtalk
[root@nfs webhook-dingtalk]# ls
config.example.yml contrib LICENSE prometheus-webhook-dingtalk
[root@nfs webhook-dingtalk]# cp config.example.yml config.yml
[root@nfs alertmanager]# pwd
/prom_alert/alertmanager
在这里插入代码片