安裝mysql exporter
Prometheus MySQL Exporter是一個客戶端應用程序,用於獲取MySQL指標並導出到Prometheus Server。
在這里,我們將介紹如何在數據庫服務器上配置Prometheus MySQL Exporter程序,包括MySQL MariaDB和使用Grafana可視化數據。這將使您能夠很好地查看數據庫性能,並在遇到問題時知道在何處檢查。警報規則的配置超出了本指南的范圍,但我將嘗試在下一個指南中介紹它。
本指南將有三個主要步驟
- Prometheus server的安裝和配置
- 在數據庫服務器上安裝和配置MySQL Prometheus exporter
- 創建/導入MySQL Grafana儀表板 - 我們將使用Percona現成的儀表盤。
官方下載地址:
https://prometheus.io/download/
github地址:
https://github.com/prometheus/mysqld_exporter
下載安裝MySQL Exporter
添加Prometheus系統用戶和組:
sudo groupadd --system prometheus sudo useradd -s / sbin / nologin --system -g prometheus prometheus
該用戶將管理exporter service。
下載安裝MySQL Exporter
這應該在MySQL / MariaDB服務器上完成,包括從服務器和主服務器。您可能需要檢查 Prometheus MySQL導出器版本 頁面以獲取最新版本,然后將最新版本導出到 VER
變量,如下所示:
export VER=0.12.1 wget https://github.com/prometheus/mysqld_exporter/releases/download/v${VER}/mysqld_exporter-${VER}.linux-amd64.tar.gz tar xvf mysqld_exporter-${VER}.linux-amd64.tar.gz mv mysqld_exporter-${VER}.linux-amd64/mysqld_exporter /usr/local/bin/ chmod +x /usr/local/bin/mysqld_exporter
通過刪除tarball和提取目錄來清理安裝。
rm -rf mysqld_exporter-${VER}.linux-amd64*
創建Prometheus exporter數據庫用戶
用戶應該有 PROCESS, SELECT, REPLICATION CLIENT
grants.
創建用戶並分配權限
CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY 'Mysql@123' WITH MAX_USER_CONNECTIONS 3; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost'; FLUSH PRIVILEGES; EXIT
如果您具有主從數據庫體系結構,則僅在主服務器上創建用戶。
WITH MAX_USER_CONNECTIONS 2
用於為用戶設置最大連接限制,以避免在負載較重的情況下使用監控擦除來使服務器過載。
配置數據庫憑據
創建數據庫憑證文件,為用戶創建添加正確的用戶名和密碼並設置所有權限:
cat > /etc/.mysqld_exporter.cnf <<EOF [client] user=mysqld_exporter password=Mysql@123 EOF chown root:prometheus /etc/.mysqld_exporter.cnf
創建systemd單元文件
創建一個新的服務文件:
cat > /etc/systemd/system/mysql_exporter.service <<EOF [Unit] Description=Prometheus MySQL Exporter After=network.target User=prometheus Group=prometheus [Service] Type=simple Restart=always ExecStart=/usr/local/bin/mysqld_exporter \ --config.my-cnf /etc/.mysqld_exporter.cnf \ --collect.global_status \ --collect.info_schema.innodb_metrics \ --collect.auto_increment.columns \ --collect.info_schema.processlist \ --collect.binlog_size \ --collect.info_schema.tablestats \ --collect.global_variables \ --collect.info_schema.query_response_time \ --collect.info_schema.userstats \ --collect.info_schema.tables \ --collect.perf_schema.tablelocks \ --collect.perf_schema.file_events \ --collect.perf_schema.eventswaits \ --collect.perf_schema.indexiowaits \ --collect.perf_schema.tableiowaits \ --collect.slave_status \ --web.listen-address=0.0.0.0:9104 [Install] WantedBy=multi-user.target EOF
完成后,重新加載systemd並啟動 mysql_exporter
服務。
systemctl daemon-reload systemctl enable --now mysql_exporter
使用Prometheus監控MySQL
配置要由Prometheus Server抓取的MySQL endpoint.
登錄到您的Prometheus服務器並配置端點以進行抓取。下面是兩個MySQL數據庫服務器的示例。
vim /etc/prometheus/prometheus.yml
#DB Servers - job_name: server1_db static_configs: - targets: ['192.168.93.40:9104'] labels: alias: db1 - job_name: server2_db static_configs: - targets: ['192.168.93.41:9104'] labels: alias: db2
第一台服務器有IP地址192.168.93.40
,第二台服務器是 192.168.93.41。使用類似格式添加其他目標。每個目標的作業名稱應該是唯一的。
注意:Prometheus Server應該能夠通過網絡到達目標。確保您具有正確的網絡/防火牆配置。
創建/導入MySQL Grafana儀表板
現在我們已經配置了目標並且要監控代理,我們應該很好地將Prometheus數據源添加到Grafana,以便我們可以進行度量可視化。需要為MySQL Prometheus exporter創建/導入Grafana儀表板。
如果您沒有足夠的時間來創建自己的儀表板,您可以使用Percona創建的儀表板,它們是開源的。
讓我們下載MySQL_Overview
儀表板,它可以很好地概述數據庫性能。
mkdir ~/grafana-dashboards cd ~/grafana-dashboards/ wget https://raw.githubusercontent.com/percona/grafana-dashboards/master/dashboards/MySQL_Overview.json
將Prometheus MySQL儀表板上傳到grafana
去Dashboards > Import > Upload .json file
使用儀表板文件找到目錄並導入,收集的指標應該開始顯示.
如果要導入Prometheus的所有Percona儀表板,請將它們安裝在Grafana服務器上。
git clone https://github.com/percona/grafana-dashboards.git cp -r grafana-dashboards/dashboards /var/lib/grafana/
您需要重新啟動Grafana服務器才能導入這些儀表板。
sudo systemctl restart grafana-server sudo service grafana-server restart
查看儀表盤