使用prometheus + granafa 監控mysql主從


若主從同步數據庫未同步默認的mysql表,則也需要在從庫上創建mysql用戶mysqld_exporter用來收集監控數據

mysqld_exporter安裝部署

這里采取的是mysqld_exporter安裝在mysql主機上

cd /usr/local/src
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
tar -zxv -f mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv mysqld_exporter-0.13.0.linux-amd64/ mysqld_exporter

授權連接

這里采取新創建mysql用戶的方式

create user 'mysqld_exporter'@'127.0.0.1' identified by '123456'; # 注意,創建的密碼除了要符合安全性規則要求外,里面不能包含#,否則會在啟動mysqld_exporter時報錯連不上mysql,看下面錯誤提示
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'exporter'@'localhost' identified by '123456';
flush privileges;

錯誤提示:

level=error msg="Error pinging mysqld: Error 1045: Access denied for user 'mysqld_exporter'@'127.0.0.1' (using password: YES)" 

創建配置文件,啟動服務

vim /usr/local/mysqld_exporter/.my.cnf
[client]
host=127.0.0.1
port=3306
user=mysqld_exporter
password=123456
vim /usr/lib/systemd/system/mysqld_exporter.service

[Unit]
Description=mysqld_exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
        --collect.info_schema.innodb_tablespaces \
        --collect.info_schema.innodb_metrics  \
        --collect.perf_schema.tableiowaits \
        --collect.perf_schema.indexiowaits \
        --collect.perf_schema.tablelocks \
        --collect.engine_innodb_status \
        --collect.perf_schema.file_events \
        --collect.info_schema.processlist \
        --collect.binlog_size \
        --collect.info_schema.clientstats \
        --collect.perf_schema.eventswaits \
        --collect.global_status \
        --collect.slave_status \
        --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=on-failure

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start mysqld_exporter
systemctl status mysqld_exporter
ss -tulnp | grep 9104
systemctl enable mysqld_exporter

添加配置到prometheus

vim prometheus.yml

  - job_name: 'mysql-mater-slave'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.80.2:9104','192.168.80.3:9104']

systemctl restart prometheus

瀏覽器訪問prometheus

查看狀態

Grafana創建模板監控

granafa給 MySQLD_Exporter添加監控圖表:

主從集群監控 MySQL Replication:7371

注意,監控的這倆數據值是從庫的,因此選擇從庫是綠色的,選擇主庫是紅色,這是正常的

緩沖池狀態 MySQL InnoDB Metrics:7365

相關mysql 狀態監控 MySQL Overview:7362


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM