Grafana+Prometheus系统监控之MySql

Posted 小柒2012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grafana+Prometheus系统监控之MySql相关的知识,希望对你有一定的参考价值。

架构

grafana和prometheus之前安装配置过,见:Grafana+Prometheus打造全方位立体监控系统

mysql安装

MySql的地位和重要性就不言而喻了,作为开源产品深受广大中小企业以及互联网企业喜爱,所以这里我们也有必要对其进行相应的监控。

由于最近更换了CentOS7,这里对MySql重新安装一遍,顺便做个记录,CentOS7的yum源中默认好像是没有mysql的。为了解决这个问题,我们要先下载mysql的repo源。

列出所有版本信息:

lsb_release -a

下载mysql的repo源:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

安装mysql-community-release-el7-5.noarch.rpm包:

rpm -ivh mysql-community-release-el7-5.noarch.rpm

安装mysql:

 yum install mysql-server -y

修改权限,否则会报错:

chown -R root:root /var/lib/mysql

重启mysql服务:

service mysqld restart

登录并重置密码:

## 直接回车进入mysql控制台
mysql -u root
mysql > use mysql;
mysql > update user set password=password(\'123456\') where user=\'root\';
mysql > exit;

mysqld_exporter安装

下载并解压:

https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz

tar -xvf mysqld_exporter-0.10.0.linux-amd64.tar.gz

mysqld_exporter需要连接Mysql,首先为它创建用户并赋予所需的权限:

 GRANT REPLICATION CLIENT, PROCESS ON *.* TO \'exporter\'@\'localhost\' identified by \'123456\';
 GRANT SELECT ON performance_schema.* TO \'exporter\'@\'localhost\';
 flush privileges;

创建.my.cnf文件 vi .my.cnf:

[client]
user=exporter
password=123456

运行mysqld_exporter:

./mysqld_exporter -config.my-cnf=".my.cnf" &

Prometheus配置

修改prometheus.yml加入MySql节点:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:

  - job_name: prometheus
    static_configs:
      - targets: [\'localhost:9090\']
        labels:
          instance: prometheus

  - job_name: linux1
    static_configs:
      - targets: [\'192.168.1.120:9100\']
        labels:
          instance: sys1

  - job_name: linux2
    static_configs:
      - targets: [\'192.168.1.130:9100\']
        labels:
          instance: sys2

  - job_name: redis1
    static_configs:
      - targets: [\'192.168.1.120:9121\']
        labels:
          instance: redis1

  - job_name: mysql
    static_configs:
      - targets: [\'192.168.1.120:9104\']
        labels:
          instance: db1

保存以后重启Prometheus,查看targets:

最后登录grafana查看MySql监控信息:

参考文档:
https://github.com/prometheus/mysqld_exporter

以上是关于Grafana+Prometheus系统监控之MySql的主要内容,如果未能解决你的问题,请参考以下文章

Grafana+Prometheus系统监控之SpringBoot

prometheus+grafana+consul+supervisor搭建监控系统之supervisor搭建

Grafana+Prometheus系统监控之webhook

监控工具之---Prometheus数据可视化Grafana

Docker下Prometheus和Grafana三部曲之三:自定义监控项开发和配置

性能监控工具之Grafana+Prometheus+Exporters