1、创建/data/apps目录,并且下载mysql_exporte
mkdir -p /data/apps
cd /data/apps
wget https://githubfast.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
或者
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
2、解压压缩包到/usr/local,并且重创建软链接
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local
cd /usr/local
ln -s mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
3、使用systemctl管理mysqld_exporter
vi /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=Prometheus
[Service]
Environment=DATA_SOURCE_NAME=exporter:Prometheus@(localhost:3306)/
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf --web.listen-address=:9104
Restart=on-failure
[Install]
WantedBy=multi-user.target
4、设置开机自启并且开启mysqld_exporter服务,数据库进行添加用户和放开权限
systemctl enable mysqld_exporter && systemctl start mysqld_exporter && systemctl status mysqld_exporter
iptables -I INPUT -p tcp --dport 9104 -j ACCEPT &&
iptables-save
数据库的操作
cd /usr/local/mysqld_exporter
cat > .my.cnf <<EOF
[client]
user=exporter
password=Prometheus
EOF
mysql -u root -p
set global validate_password_policy=LOW;
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'Prometheus' WITH MAX_USER_CONNECTIONS 5;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
5、在prometheus服务器添加mysqld_exporter节点监控
vi /usr/local/prometheus/prometheus.yml
- job_name: "mysqld_exporter"
static_configs:
- targets: ['被监控设备的IP:9104']
labels:
app: mysqld_exporter
role: mysqld_exporter
或者以file_sd_configs方式添加node
vi /usr/local/prometheus/prometheus.yml
- job_name: "mysqld_exporter"
file_sd_configs:
- files:
- /usr/local/prometheus/target/mysqld_exporter.yml
# pwd
/usr/local/prometheus/target
# cat mysqld_exporter.yml
- targets:
- "被监控设备的IP:9104"
labels:
app: mysqld_exporter
role: mysqld_exporter
6、热加载prometheus配置
curl -X POST http://127.0.0.1:9090/-/reload
脚本方式部署
----------------------------------------------------------------------------
mysqld_exporter节点
vi mysqld_exporter.sh
#!/bin/bash
setenforce 0 &&
iptables -I INPUT -p tcp --dport 9104 -j ACCEPT &&
iptables-save > /dev/null &&
mkdir -p /data/apps > /dev/null &&
cd /data/apps &&
wget https://githubfast.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz > /dev/null &&
echo "下载mysqld_exporter压缩包完成" &&
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local > /dev/null &&
cd /usr/local &&
ln -s mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter &&
echo "[Unit]
Description=Prometheus
[Service]
Environment=DATA_SOURCE_NAME=exporter:Prometheus@(localhost:3306)/
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf --web.listen-address=:9104
Restart=on-failure
[Install]
WantedBy=multi-user.target" | tee -i /usr/lib/systemd/system/mysqld_exporter.service > /dev/null &&
systemctl enable mysqld_exporter &&
systemctl start mysqld_exporter &&
systemctl status mysqld_exporter
sh ./mysqld_exporter.sh
进行上面4的数据库的操作
卸载mysqld_exporter节点
vi mysqld_exporter_uninstall.sh
#!/bin/bash
systemctl disable mysqld_exporter &&
systemctl stop mysqld_exporter &&
rm -rf /usr/lib/systemd/system/mysqld_exporter.service &&
setenforce 1 &&
iptables -D INPUT -p tcp --dport 9104 -j ACCEPT &&
iptables-save > /dev/null &&
cd /usr/local &&
unlink mysqld_exporter &&
rm -rf mysqld_exporter-0.12.1.linux-amd64 &&
rm -rf /data/apps/mysqld_exporter-0.12.1.linux-amd64.tar.gz > /dev/null &&
echo "删除mysqld_exporter压缩包完成"
sh ./mysqld_exporter_uninstall.sh
prometheus节点
vi mysqld_exporter_join.sh
#!/bin/bash
#对应被监控的node名称
NODENAME=$1
#对应被监控的IP地址
IPADDRESS=$2
echo "
- job_name: "$NODENAME"
static_configs:
- targets: ["$IPADDRESS:9104"]
" | tee -a /usr/local/prometheus/prometheus.yml > /dev/null &&
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml &&
curl -X POST http://127.0.0.1:9090/-/reload
sh ./mysqld_exporter_join.sh NODENAME(对应被监控的node名称) IPADDRESS(对应被监控的IP地址)