在被管理机agent1上安装mysqld_exporter组件
下载地址: https://prometheus.io/download/
安装mysqld_exporter组件
1、下载到被监控端解压压缩包
[root@localhost ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
2、改名并移动到指定目录
[root@localhost ~]# mv /usr/local/mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter
[root@localhost ~]# ls /usr/local/mysqld_exporter/
LICENSE mysqld_exporter NOTICE
安装mariadb数据库,并授权
[root@agent1 ~]# yum install mariadb\* -y
[root@agent1 ~]# systemctl restart mariadb
[root@agent1 ~]# systemctl enable mariadb
[root@agent1 ~]# mysql
3、登录mysql为exporter创建账号并授权
grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123456';
flush privileges;
4、创建mysql配置文件、运行时可免密码连接数据库
创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)
cat > /usr/local/mysqld_exporter/.my.cnf <<EOF
[client]
user=mysql_monitor
password=123456
EOF
5、启动mysqld_exporter客户端
[root@agent1 ~]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf >> /usr/local/mysqld_exporter/mysqld_exporter.out & 确认端口(9104)
[root@localhost ~]# tail -f -n 50 /usr/local/mysqld_exporter/mysqld_exporter.out
[root@localhost ~]# lsof -i:9104
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld_ex 121342 root 3u IPv6 150935 0t0 TCP *:peerwire (LISTEN)
6、添加系统服务:
vi /usr/lib/systemd/system/mysql_exporter.service
配置服务文件
cat > /usr/lib/systemd/system/mysql_exporter.service <<-EOF
[Unit]
Description=https://prometheus.io
[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
EOF
7、启动添加后的系统服务
systemctl daemon-reload
systemctl restart mysql_exporter.service
systemctl status mysql_exporter.service
8、网站查看捕获mysql数据
访问:http://192.168.66.31:9104/metrics
9、使用prometheus监控修改监控端配置文件:vim prometheus.yml
cat >> /tmp/prometheus.yml <<-EOF
- job_name: 'MySQL'
static_configs:
- targets: ['192.168.66.31:9104']
EOF
10、检查并重启服务
检查并重新加载配置文件
./bin/promtool check config config/prometheus.yml
重启服务
docker restart prometheus
第一种,向prometheus进行发信号
kill -HUP pid
第二种,向prometheus发送HTTP请求
/-/reload只接收POST请求,并且需要在启动prometheus进程时,指定 --web.enable-lifecycle
curl -XPOST http://prometheus.chenlei.com/-/reload