监控MySQL服务器
平台为redhat 7.2 x86_64bit
为了使监控可视化,我们将借助于Grafana,基本安装【略】,对于要监控的对象MySQL还需要安装mysqld_exporter【略】,本文假设安装就绪。
监控配置的基本架构如下:
配置mysqld_exporter
增加一个用于监控的MySQL用户
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by 'redhat';
mysql> GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
mysql> flush privileges;
指定mysqld_exporter连接MySQL的配置文件,当然首先要写配置文件,位置随便,如我的:
[root@prometheus ~]# cat /etc/prometheus/.my.cnf
[client]
user=mysqld_exporter
password=redhat
然后在mysqld_exporter的启动程序中指定
[root@prometheus ~]# systemctl status mysqld_exporter
● mysqld_exporter.service - Prometheus exporter for MySQL server metrics.
Loaded: loaded (/usr/lib/systemd/system/mysqld_exporter.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2018-01-22 22:00:20 CST; 1h 47min ago
Docs: https://github.com/prometheus/mysqld_exporter
Main PID: 2680 (mysqld_exporter)
CGroup: /system.slice/mysqld_exporter.service
└─2680 /usr/bin/mysqld_exporter -config.my-cnf=/etc/prometheus/.my.cnf###注意这里,需要修改Systemd服务把这一项加上
完成以上配置之后重启或者启动mysqld_exporter
配置prometheus,加入mysql监控目标
[root@prometheus ~]# tail -n 12 /etc/prometheus/prometheus.yml | head -n 6
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104']
labels:
instance: 'mysql1'
配置完成重启或者reload prometheus服务
curl -XPOST http://ip:9090/-/reload
下载并且配置监控模板
git clone https://github.com/percona/grafana-dashboards.git
cp -r grafana-dashboards/dashboards /var/lib/grafana/
编辑Grafana配置文件如下:
[root@prometheus ~]# sed -n '370,373p' /etc/grafana/grafana.ini
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
然后重新启动或者启动grafana-server,在浏览器中输入http://ip:3000进行访问(用户密码默认admin)
可以看到我们导入的模板
配置一下数据源为prometheus
点击最左上图标然后选中Data Sources-》add data datasource,然后按照以下内容填入
注意:
Type一定要是Prometheus
URL是prometheus的端点url
Access选择proxy
点击其中的MySQL Overview然后对查询条件做适当调整(点击每个图形的标题-》edit-》metrics)就会看到以下界面了: