未安装prometheus和Grafana可以参考这个博客操作
Windows版本数据库
一、在数据库服务器安装mysqld-exporter
下载mysqld-exporter文件
Releases · prometheus/mysqld_exporter · GitHub

二、配置mysqld-exporter文件
1、解压进入mysqld_exporter-0.15.1.windows-amd64文件夹
2、新建my.cnf和start.bat文件

my.cnf文件信息
################my.cnf####################
[client]
host=127.0.0.1
prot=3306
user=root
password=123456
#######################################
[client]
host #数据库服务器的ip地址本地可以用127.0.0.1
prot #链接数据库的端口
user #链接数据库的用户名
password #链接数据库的密码
bash
start.bat文件信息
########start.bat##########
mysqld_exporter.exe --config.my-cnf=my.cnf
#############################
Linux版本数据库
一、在数据库服务器安装mysqld-exporter
bash
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

解压文件
bash
tar -xzvf mysqld_exporter-0.15.1.linux-amd64.tar.gz
二、配置mysqld_exporter-0.15.1.linux-amd64服务文件
bash
touch my.cnf
my.cnf文件信息
################my.cnf####################
[client]
host=127.0.0.1
prot=3306
user=root
password=123456
#######################################
[client]
host #数据库服务器的ip地址本地可以用127.0.0.1
prot #链接数据库的端口
user #链接数据库的用户名
password #链接数据库的密码
三、启动
bash
touch start.sh
start.sh文件信息
########start.bat##########
./mysqld_exporter --config.my-cnf=my.cnf
#############################
Docker版本数据库
安装mysqld-exporter
bash
docker run -d --name mysqld_exporter \
-p 9104:9104 \
-e DATA_SOURCE_NAME="数据库用户名:数据库密码@(数据库地址:数据库端口号)/" \
prom/mysqld-exporter:v0.14.0
docker run -d --name mysqld_exporter \
-p 9104:9104 \
-e DATA_SOURCE_NAME="root:123456@(172.17.0.3:3306)/" \
prom/mysqld-exporter:v0.14.0
测试访问
配置prometheus
编写prometheus.yml (/opt/prometheus/prometheus.yml)
bash
#在scrape_configs: 里面添加下面代码
scrape_configs: #配置块:配置采集目标相关, prometheus监视的目标。Prometheus自身的运行信息可以通过HTTP访问,所以Prometheus可以监控自己的运行数据。
- job_name: mysql1 #监控作业的名称 名称随意
static_configs: #表示静态目标配置,就是固定从某个target拉取数据
- targets: ['localhost:9090'] #mysqld-exporter服务IP:mysqld-exporter服务端口
labels:
instance: mysql1 #和上面一致
重启prometheus
bash
#容器重启
docker restart prometheus
#yum重启
systemctl restart prometheus
配置Grafana展示



