Prometheus 快速監(jiān)控 MySQL 數(shù)據(jù)庫(kù),超簡(jiǎn)單!
今天分享一下Prometheus如何監(jiān)控MySQL數(shù)據(jù)庫(kù)。
實(shí)驗(yàn)環(huán)境:
- 數(shù)據(jù)庫(kù)服務(wù)器:10.0.0.41
- prometheus服務(wù)器:10.0.0.104
默認(rèn)數(shù)據(jù)庫(kù)和prometheus都已部署好!下面演示安裝mysql_exporter過(guò)程。
一、安裝mysqld_exporter
在MySQL數(shù)據(jù)庫(kù)服務(wù)器上安裝mysql_exporter。
1. 創(chuàng)建 MySQL 只讀監(jiān)控用戶
在 MySQL 中運(yùn)行以下命令(換成你自己的密碼):
CREATE USER 'exporter'@'%' IDENTIFIED BY '123456';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
FLUSH PRIVILEGES;
驗(yàn)證登錄:
mysql -uexporter -p123456
2. 安裝 mysqld_exporter
下載并安裝:
cd /opt
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.17.2/mysqld_exporter-0.17.2.linux-amd64.tar.gz
tar -xvf mysqld_exporter-0.17.2.linux-amd64.tar.gz
cp mysqld_exporter-0.17.2.linux-amd64/mysqld_exporter /usr/local/bin/
chmod +x /usr/local/bin/mysqld_exporter
3.創(chuàng)建配置文件
在 /etc 下創(chuàng)建 .mysqld_exporter.cnf 登錄配置:
vi /etc/.mysqld_exporter.cnf
內(nèi)容如下:
[client]
user=exporter
password=123456
4. 配置 systemd 服務(wù)
創(chuàng)建服務(wù)文件:
vi /etc/systemd/system/mysqld_exporter.service
內(nèi)容如下:
[Unit]
Descriptinotallow=Prometheus MySQL Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter--config.my-cnf=/etc/.mysqld_exporter.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
然后啟動(dòng)服務(wù):
# 重載系統(tǒng)服務(wù)
systemctl daemon-reload
# 啟動(dòng)并設(shè)置開(kāi)機(jī)自啟動(dòng)
systemctl enable --now mysqld_exporter
查看狀態(tài):
systemctl status mysqld_exporter
5. 防火墻放行(可選)
確保 Prometheus 節(jié)點(diǎn)(10.0.0.104)能訪問(wèn)此節(jié)點(diǎn)的 9104 端口:
# 關(guān)閉防火墻
systemctl disable --now firewalld
# 或者開(kāi)通9104端口
firewall-cmd --permanent --add-port=9104/tcp
firewall-cmd --reload
二、Prometheus 配置
編輯你的 prometheus.yml:
vi /data/prometheus/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'mysql-exporter'
static_configs:
- targets: ['10.0.0.41:9104']
labels:
mysql: 10.0.0.41數(shù)據(jù)庫(kù)
保存并重啟 Prometheus:
systemctl restart prometheus
訪問(wèn) Prometheus 界面:
http://10.0.0.104:9090
進(jìn)入 Status > Targets,確認(rèn) 10.0.0.41:9104 狀態(tài)為 UP。
三、測(cè)試指標(biāo)是否可用
在瀏覽器或命令行訪問(wèn):
http://10.0.0.41:9104/metrics
你應(yīng)該能看到:
說(shuō)明 exporter 成功運(yùn)行并能讀取數(shù)據(jù)庫(kù)狀態(tài)!
四、使用Grafana可視化展示
導(dǎo)入 MySQL 監(jiān)控 Dashboard(推薦模板 ID:7362 或 14057):