使用Prometheus监控MySql
-
安装mysqld_experter组件
在node_exporter所在管理机器上安装mysqld_exporter组件
下载地址: mysqld_exporter-0.12.1.linux-amd64下载
上传mysqld_exporter-0.12.1.linux-amd64.tar.gz压缩包到linux服务器 -
解压mysqld_experter组件
解压:
# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
重命名:
# mv /usr/local/mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter
- 安装mariadb数据库或者安装mysql数据库,如下是安装mariadb数据库步骤
安装mariadb数据库,并授权
# yum install mariadb\* -y
# systemctl start mariadb
# systemctl enable mariadb
# mysql
- 创建mysql账号
# mysql> grant select,replication client,process on *.* to 'root'@'localhost' identified by '123';
# mysql> flush privileges;
# mysql> exit;
- 在mysqld_exporter组件中配置mysql信息
# vim /usr/local/mysqld_exporter/.my.cnf #这个文件是手工创建的
[client]
host=IP地址 # 如果mysql跟mysqld_exporter安装在同一台机器,则可以不加host
user=root # 用户名
password=123456 # 密码
- 启动mysqld_exporter组件
启动mysqld_exporter
# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
确认端口(9104)
# lsof -i:9104
-
通过浏览器访问 http://192.168.31.11:9104/metrics 就可以查看到mysqld_exporter在被监控端收集的监控信息
-
让prometheus监控可以拉取mysqld节点信息
在prometheus配置文件里添加被监控机器的配置信息
prometheus配置文件
# vim /usr/local/prometheus/prometheus.yml
按如下配置格式配置需要监控的机器
- job_name: 'mariadb' #取一个job名称来代表被监控的机器
static_configs:
- targets: ['192.168.31.11:9104'] #这里改成被监控机器的IP,后面端口接9104
改完配置文件后,再重启prometheus服务
# pkill prometheus #杀死promeheus进程
# lsof -i:9090 #确认端口没有进程占用
#重启prometheus服务
# cd /usr/local/prometheus
# ./prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
# lsof -i:9090 #确认端口,被占用说明重启成功
回到prometheus的web管理页面,点Status–>Targets可以看到多了一台监控目标