首頁>技術>

透過Prometheus監控Mysql,我們需要在Mysql端安裝一個mysql-exporter,然後Prometheus透過mysql-exporter暴露的埠抓取資料。

1.安裝一個MYSQL
配置MYSQL源echo '# Enable to use MySQL 5.7[mysql57-community]name=MySQL 5.7 Community Serverbaseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/enabled=1gpgcheck=1gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql' >/etc/yum.repos.d/mysql.repo安裝mysql 5.7yum install mysql-community-server -y啟動並設定開機啟動systemctl start mysqld.servicesystemctl status mysqld.servicesystemctl enable mysqld.service檢視mysql初始密碼grep 'temporary password' /var/log/mysqld.log2021-01-22T01:30:46.703325Z 1 [Note] A temporary password is generated for root@localhost: k+F<ucNmR5sK修改mysql root密碼su - mysqlmysql -uroot -pALTER USER 'root'@'localhost' IDENTIFIED BY 'zaq1@WSX';flush privileges;到這裡mysql5.7安裝完成。
2.登入mysql為exporter建立賬號並授權
# 建立資料庫使用者。mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'zaq1@WSX';# 可檢視主從執行情況檢視執行緒,及所有資料庫。mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
3.下載mysql-exporter並做配置
1.下載mysql-exporterwget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gztar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysql_exporter2.建立mysql配置檔案、執行時可免密碼連線資料庫echo '[client]user=exporterpassword=zaq1@WSX ' >/usr/local/mysql_exporter/.my.cnf 3.設定mysql_exporter啟動服務echo '[Unit]Description=https://prometheus.io[Service]Restart=on-failureExecStart=/usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysql_exporter/.my.cnf[Install]WantedBy=multi-user.target' >/etc/systemd/system/mysql_exporter.service4.啟動新增後的系統服務systemctl daemon-reloadsystemctl restart mysql_exporter.service完成mysql_exporter部署
4.檢查mysql_exporter監聽埠
訪問頁面http://192.168.0.107:9104/metrics得到下面結果# HELP go_gc_duration_seconds A summary of the GC invocation durations.# TYPE go_gc_duration_seconds summarygo_gc_duration_seconds{quantile="0"} 0go_gc_duration_seconds{quantile="0.25"} 0go_gc_duration_seconds{quantile="0.5"} 0go_gc_duration_seconds{quantile="0.75"} 0go_gc_duration_seconds{quantile="1"} 0go_gc_duration_seconds_sum 0go_gc_duration_seconds_count 0
5.Prometheus監控MYSQL
Prometheus配置檔案/etc/prometheus/prometheus.yml,新增如下內容:scrape_configs:  - job_name: 'mysql'    # 靜態新增node    static_configs:    # 指定監控端    - targets: ['localhost:9104']重啟Prometheussystemctl restart prometheus
6.檢查Prometheus target

判斷Prometheus是否獲取mysql_exporter資料。

訪問這個地址 http://192.168.0.107:9090/targets ,看到紅框部分,說明Prometheus監控成功

mysql_exporter

7.Grafana匯入mysql模板

還是一樣 https://grafana.com/grafana/dashboards 裡面找到一個模板

我這裡用這個 7362

mysql 監控

至此,mysql監控已經完成。

10
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 如何使用 Dapper 中的非同步方法