Prometheus➕Grafana监控MySQL性能

实验环境

IP 服务
192.168.1.10 Prometheus、Grafana
192.168.1.20 mysqld_exporter、MySQL

192.168.1.20安装mysqld_exporter、MySQL

  • MySQL
    参考链接
  • mysqld_exporter
下载安装包mysqld_exporter-0.12.1.linux-amd64.tar.gz
进行解压
[root@localhost ~]# tar -zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz

运行mysqld_exporter服务它会依赖于.my.cnf文件并不是MySQL的配置文件
.my.cnf文件存放的是被授权用户的账号密码,所以先去授权

mysql>  grant select,replication client,process ON *.* to 'mysql'@'localhost' identified by '123.com'; 
(注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb。所以这个localhost是指的mysql_exporter的IP)
mysql> flush privileges;
mysql> quit
  • 编写.my.cnf文件

在这里插入图片描述

如果直接到mysql_exporter目录下进行启动会报错。默认回去root目录下寻找.my.cnf文件,

  1. 可以直接在root目录下编辑文件则为默认不用指定
[root@localhost mysqld_exporter-0.12.1.linux-amd64]# ./mysqld_exporter
  1. 可以自定义目录启动的时候需要指定文件路径
 /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/root/mysqld_exporter-0.12.1.linux-amd64/.my.cnf 

我直接在root下创建了

[root@localhost ~]# cat .my.cnf 
[client] 
user=mysql
password=123.com

[root@localhost ~]# ls -a #这是一个隐藏文件需要使用ls -a查看
  • 启动mysqld_exporter
    后台启动
[root@localhost mysqld_exporter-0.12.1.linux-amd64]# nohup ./mysqld_exporter &
或者
./mysqld_exporter &

前台启动

[root@localhost mysqld_exporter-0.12.1.linux-amd64]# ./mysqld_exporter 
INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96)  source="mysqld_exporter.go:257"
INFO[0000] Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)  source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:269"
INFO[0000]  --collect.slave_status                       source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmp             source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmpmem          source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.query_response_time    source="mysqld_exporter.go:273"
INFO[0000] Listening on :9104                            source="mysqld_exporter.go:283"
没有报错就是启动成功
[root@localhost mysqld_exporter-0.12.1.linux-amd64]# netstat -tunlp | grep 9104
tcp6       0      0 :::9104                 :::*                    LISTEN      12844/./mysqld_expo 

192.168.1.10安装Prometheus&Grafana

  • 下载安装Prometheus
[root@localhost ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
[root@localhost ~]# tar -zxf prometheus-2.10.0.linux-amd64.tar.gz
[root@localhost ~]# mv prometheus-2.10.0.linux-amd64 /usr/local/prometheus
  • 配置Prometheus
[root@localhost ~]# vim /usr/local/prometheus/prometheus.yml
# 修改如下
 - job_name: 'mysql'
    static_configs:
    - targets: ['192.168.1.20:9104']
  • 启动Prometheus
[root@localhost ~]# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &
  • 安装Grafana
[root@localhost ~]# wget https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm
[root@localhost ~]# yum -y install initscripts urw-fonts
[root@localhost ~]# rpm -Uvh grafana-5.4.2-1.x86_64.rpm 
  • 启动Grafana
[root@localhost ~]# systemctl start grafana-server

Grafana页面配置
访问http://192.168.1.10:3000

默认用户密码都是admin
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
导入一个MySQL的仪表盘,将json文件导入,官网有很多可以去参考,也可以直接使用我的json文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46152207/article/details/113365078
今日推荐