天天看点

mysqld_exporter+Prometheus+grafana监控Mysql

关于Prometheus和grafana的安装这里不做赘述。

需要的朋友可以参考上一篇文章,node_exporter+Prometheus+grafana搭建服务器资源监控系统

一.部署mysqld_exporter

mysqld_exporter需部署到被监控的服务器上。

1.下载部署

(base) [email protected]:~# cd /usr/local/src/
(base) [email protected]:/usr/local/src# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
           

服务器下载太慢时,可直接从github网站下载,并将压缩包上传至服务器。

文章最后分享了网盘链接也可自行下载。

(base) [email protected]:/usr/local/src# tar xvf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
(base) [email protected]:/usr/local/src# cd /usr/local/
(base) [email protected]:/usr/local# mv mysqld_exporter-0.13.0.linux-amd64/ mysql_exporter/
           
mysqld_exporter+Prometheus+grafana监控Mysql

安装运行mysqld_exporter

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

#登录mysql的root用户,并创建用户
(base) [email protected]:/usr/local# mysql -uroot -p'password'
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'tom'@'localhost' identified by 'qaz691';
mysql> GRANT SELECT ON performance_schema.* TO 'tom'@'localhost';
           

创建.my.cnf文件并运行mysqld_exporter:

(base) [email protected]:/usr/local# cd /usr/local/mysql_exporter/
(base) [email protected]:/usr/local/mysql_exporter# vim .my.cnf
           

user与password与之前创建的一致,.my.cnf中内容如下:

[client]

host=127.0.0.1

user= tom

password=qaz691

启动mysqld_exporter:

(base) [email protected]:/usr/local/mysql_exporter# ./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, [email protected], date=20190729-12:35:58)  source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:269"
INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:273"
INFO[0000]  --collect.slave_status                       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"
           

2.访问验证

访问http://192.168.1.12:9104,这里的ip替换为服务器ip。

出现以下页面说明启动成功。

mysqld_exporter+Prometheus+grafana监控Mysql

3.开机自启动

配置文件易出错,文章最后分享了网盘链接也可自行下载。

mysqld_exporter+Prometheus+grafana监控Mysql

二.prometheus接收mysqld_exporter采集的数据

修改prometheus配置文件,重新启动prometheus。

mysqld_exporter+Prometheus+grafana监控Mysql

访问http://192.168.1.13:9090/targets:

mysqld_exporter+Prometheus+grafana监控Mysql

三.Grafana展示mysqld_exporter采集的数据

1.检查Data souces是否正常。

mysqld_exporter+Prometheus+grafana监控Mysql

2.导入Dashboard

登录之后,点击左侧“+”->Import

mysqld_exporter+Prometheus+grafana监控Mysql

输入ID:7362,点击“Load”。(7362是比较经典好用的一个展示MySQL使用情况的模板,这里以导入这个模板为例)

mysqld_exporter+Prometheus+grafana监控Mysql

选择data source,下拉选择上一步配置的数据源,点击“Import”

mysqld_exporter+Prometheus+grafana监控Mysql

展示效果如图:

mysqld_exporter+Prometheus+grafana监控Mysql

至此,我们已经完成了对MySQL监控系统的搭建。

安装所需包及配置文件在百度网盘里面,此链接永久有效,如有需要可下载:

链接:MySQL监控系统

提取码:2pu3

mysqld_exporter+Prometheus+grafana监控Mysql