我写的这些笔记算是一个系列,所以都是接着上一篇一步步来切入的:
MySql的地位和重要性就不言而喻了,作为开源产品深受广大中小企业以及互联网企业喜爱,所以这里我们也有必要对其进行相应的监控。
下载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 -u root
mysql > use mysql;
mysql > update user set password=password('asd9577') where user='root';
mysql > exit;
下载并解压:
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.10.0.linux-amd64.tar.gz
mysqld_exporter需要连接Mysql,首先为它创建用户并赋予所需的权限:
GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'asd9577'@'localhost' identified by 'asd9577';
GRANT SELECT ON performance_schema.* TO 'asd9577'@'localhost';
flush privileges;
创建.my.cnf文件
vim /etc/my.cnf
[client]
user=asd9577
password=asd9577
运行mysqld_exporter:
./mysqld_exporter -config.my-cnf="/etc/my.cnf" &
修改prometheus.yml加入MySql节点,然后重启prometheus:
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.161.162:9100']
labels:
instance: sys1
- job_name: linux2
static_configs:
- targets: ['192.168.161.163:9100']
labels:
instance: sys2
- job_name: redis
static_configs:
- targets: ['192.168.161.162:9121']
labels:
instance: redis162
- job_name: mysql
static_configs:
- targets: ['192.168.161.163:9104']
labels:
instance: db1
如上最后一条!
保存以后重启Prometheus,查看targets:
最后登录grafana查看MySql监控信息:
参考文档: