• Prometheus+Grafana监控MySQL


    一、准备

    • grafana服务器:192.168.48.136
    • Prometheus服务器:192.168.48.136
    • 被监控服务器:192.168.48.134、192.168.48.135
    • 查看时间是否同步

    二、安装prometheus server

    【2.1】安装

    1. # 解压安装包
    2. tar -zxvf prometheus-2.52.0.linux-amd64.tar.gz -C /opt
    3. # 创建链接目录
    4. cd /opt
    5. ln -s prometheus-2.52.0.linux-amd64.tar.gz prometheus
    6. # 直接使用默认配置文件启动
    7. /opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml" &
    8. # 确认是否正常启动(默认端口9090)
    9. netstat -lnptu | grep 9090

    【2.2】prometheus界面访问 

    【2.3】查看监控本机

    【2.4】监控本机数据展示 

    通过http://服务器IP:9090/metrics可以查看到监控的数据

    【2.5】在web主界面可以通过关键字查询监控项

    三、监控远程Linux主机

    【3.1】在被监控端上安装node_exporter组件

    1. # 解压安装包
    2. tar -xf node_exporter-1.8.1.linux-amd64.tar.gz -C /opt
    3. # 创建链接目录
    4. cd /opt
    5. ln -s node_exporter-1.8.1.linux-amd64.tar.gz node_exporter
    6. # 使用nohup后台运行
    7. nohup /opt/node_exporter/node_exporter &
    8. # 确认是否正常启动(默认端口9100)
    9. netstat -lnptu | grep 9100
    10. 扩展: nohup命令: 如果把启动node_exporter的终端给关闭,那么进程也会随之关闭。nohup命令会帮你解决这个问题。

     同样操作,将所有需要被监控主机安装node_exporter组件

    【3.2】通过浏览器访问http://被监控端IP:9100/metrics就可以查看到node_exporter在被监控端收集的监控信息

    【3.3】 回到prometheus服务器的配置文件里添加被监控机器的配置段

    1. # my global config
    2. global:
    3. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
    4. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
    5. # scrape_timeout is set to the global default (10s).
    6. # Alertmanager configuration
    7. alerting:
    8. alertmanagers:
    9. - static_configs:
    10. - targets:
    11. # - alertmanager:9093
    12. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
    13. rule_files:
    14. # - "first_rules.yml"
    15. # - "second_rules.yml"
    16. # A scrape configuration containing exactly one endpoint to scrape:
    17. # Here it's Prometheus itself.
    18. scrape_configs:
    19. # The job name is added as a label `job=` to any timeseries scraped from this config.
    20. - job_name: "prometheus"
    21. static_configs:
    22. - targets: ["localhost:9090"]
    23. - job_name: '192.168.48.134' # 给被监控主机取个名字,我这里直接填的IP
    24. static_configs:
    25. - targets: ['192.168.48.134:9100'] # 这里填写被监控主机的IP和端口
    26. - job_name: '192.168.48.135'
    27. static_configs:
    28. - targets: ['192.168.48.135:9100']
    29. # scheme defaults to 'http'.

    重启服务

    1. pkill prometheus
    2. /opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml" &
    3. netstat -lnptu | grep 9090

    【3.4】回到web管理界面,可以看到多了两台监控主机

    四、监控远程MySQL

    【4.1】 在被管理MySQL服务器上安装mysqld_exporter组件

    1. # 解压安装包
    2. tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt
    3. # 创建链接目录
    4. cd /opt
    5. ln -s mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
    6. # 在MySQL服务器上创建监控用户
    7. mysql> grant select,replication client, process on *.* to 'mysql_monitor'@'localhost' identified by 'xxxxxxxxxxxxx';
    8. Query OK, 0 rows affected, 1 warning (0.00 sec)
    9. mysql> flush privileges;
    10. Query OK, 0 rows affected (0.03 sec)
    11. mysql> exit
    12. Bye
    13. # 将上面创建的mysql用户信息写入mysqld_exporter配置文件
    14. vim /opt/mysqld_exporter/.my.cnf
    15. [client]
    16. user=mysql_monitor
    17. password=xxxxxxxxxxx
    18. # 启动mysqld_exporter
    19. nohup /opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/.my.cnf &
    20. # 确认是否正常启动(默认端口9104)
    21. netstat -lnptu | grep 9104

     【5.2】回到prometheus服务器的配置文件里添加被监控的mariadb的配置段

    1. # my global config
    2. global:
    3. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
    4. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
    5. # scrape_timeout is set to the global default (10s).
    6. # Alertmanager configuration
    7. alerting:
    8. alertmanagers:
    9. - static_configs:
    10. - targets:
    11. # - alertmanager:9093
    12. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
    13. rule_files:
    14. # - "first_rules.yml"
    15. # - "second_rules.yml"
    16. # A scrape configuration containing exactly one endpoint to scrape:
    17. # Here it's Prometheus itself.
    18. scrape_configs:
    19. # The job name is added as a label `job=` to any timeseries scraped from this config.
    20. - job_name: "prometheus"
    21. static_configs:
    22. - targets: ["localhost:9090"]
    23. - job_name: '192.168.48.134' # 给被监控主机取个名字,我这里直接填的IP
    24. static_configs:
    25. - targets: ['192.168.48.134:9100'] # 这里填写被监控主机的IP和端口
    26. - job_name: '192.168.48.135'
    27. static_configs:
    28. - targets: ['192.168.48.135:9100']
    29. - job_name: 'mysql-134' # 给被监控主机取个名字
    30. static_configs:
    31. - targets: ['192.168.48.134:9104'] # 这里填写被监控主机的IP和端口
    32. - job_name: 'mysql-135'
    33. static_configs:
    34. - targets: ['192.168.48.135:9104']
    35. # metrics_path defaults to '/metrics'
    36. # scheme defaults to 'http'.

    重启服务 

    1. pkill prometheus
    2. /opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml" &
    3. netstat -lnptu | grep 9090

    【5.3】回到web管理界面点Status-->Targets-->就可以看到监控的MySQL服务器了

     回到主界面搜索mysql相关参数

    五、Grafana可视化图形工具

    【5.1】安装grafana服务器

    1. #解压grafana安装包
    2. tar -zxvf grafana-7.3.5.linux-amd64.tar.gz -C /opt
    3. cd /opt/grafana-7.3.5/bin/
    4. #启动
    5. ./grafana-server
    6. 查看端口
    7. netstat -lnptu | grep 3000

     通过浏览器访问 http:// ip服务器IP:3000就到了登录界面,使用默认用户名admin,密码admin

    登录进入首页

     六、在Grafana上添加Prometheus数据源

    【6.1】我们把prometheus服务器收集的数据做为一个数据源添加到grafana,让grafana可以得到prometheus的数据

     七、Grafana图形显示Linux硬件信息

    刚刚添加的

    查看效果

    八、 Grafana图形显示MySQL监控数据

    【8.1】通过Grafana官网导入模板

     九、修改grafana配置文件,添加发件邮箱

    1. vim /etc/grafana/grafana.ini # 找到smtp关键字
    2. #################################### SMTP / Emailing ##########################
    3. [smtp]
    4. enabled = true # 改成true
    5. host = smtp.exmail.qq.com:465 # 发送服务器地址和端口
    6. user = # 这里填写发件邮箱
    7. # If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
    8. password = # 这里填写发件邮箱密码或授权密码
    9. ;cert_file =
    10. ;key_file =
    11. skip_verify = true # 改成true
    12. from_address = # 这里填写发件邮箱
    13. ;from_name = Grafana
    14. # EHLO identity in SMTP dialog (defaults to instance_name)
    15. ;ehlo_identity = dashboard.example.com
    16. # SMTP startTLS policy (defaults to 'OpportunisticStartTLS')
    17. ;startTLS_policy = NoStartTLS

    后面的发件邮箱就不在说明了

    参考博客:Prometheus+Grafana监控MySQL - easydba - 博客园 (cnblogs.com)

  • 相关阅读:
    《爱在 ZStack Cube 超融合》三部曲
    使用HTML制作静态网站:传统文化戏剧锡剧带psd设计图(2个页面)
    ThreadPool实现机制
    el-input输入框涉及到scope的校验问题
    Oracle GoldenGate实现数据同步(Windows环境下的内外网同步)
    gRPC 自定义负载均衡算法
    Prompt-Tuning源码分析
    Linux常用命令
    8月算法训练------第十四天(数学)解题报告
    基于Python实现的糖尿病预测系统
  • 原文地址:https://blog.csdn.net/qq_60275529/article/details/139681093