Prometheus监控MySQL:
1.下载mysqld_exporter: root@dokcer:~# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz 2.解压mysqld_exporter: root@dokcer:~# tar -xzvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local 3.创建链接文件: root@dokcer:~# ln -sv /usr/local/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter /usr/local/bin/mysql_exporter '/usr/local/bin/mysqld_exporter' -> '/usr/local/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter' 4.安装并启动mysql server root@dokcer:~# apt install mysql-server root@dokcer:~# sudo systemctl start mysql.service 5.添加用户和赋予权限信息: mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3; Query OK, 0 rows affected (0.00 sec) mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 6.编辑mysqld_exporter配置文件 .my.cnf文件(默认文件在~/.my.cnf) root@dokcer:~# vim /usr/local/mysqld_exporter-0.14.0.linux-amd64/.my.cnf [client] user=exporter #mysql 用户 password=123456 #mysql密码 7.后台运行mysqld_exporter root@dokcer:~# nohup mysqld_exporter --config.my-cnf='/usr/local/mysqld_exporter-0.14.0.linux-amd64/.my.cnf' & 8.查看mysqld exporter端口状态 root@dokcer:~# lsof -i:9104 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld_ex 2456 root 3u IPv6 26780 0t0 TCP *:9104 (LISTEN) root@dokcer:~# netstat -tulp | grep 9104 tcp6 0 0 [::]:9104 [::]:* LISTEN 2456/mysqld_exporte root@dokcer:~# ss -natlp | grep 9104 LISTEN 0 128 *:9104 *:* users:(("mysqld_exporter",pid=2456,fd=3))
访问http://IP:9104/metrics:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2Fz9GrvZJAykTapHQ.png?table=block&id=1201b5e4-113d-4da7-a696-724d45bbdcf0&cache=v2)
Prometheus添加MySQL主机:
1.添加Prometheus配置文件,添加MySQL主机 root@dokcer:~# vim /usr/local/prometheus/prometheus.yml - job_name: "mysql" static_configs: - targets: ["192.168.0.50:9104"] 2.重启prometheus root@dokcer:~# pkill prometheus root@dokcer:~# nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &
Grafana添加MySQL图形界面:
添加Prometheus数据源:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2FIzC5WmQY62tabPF.png?table=block&id=4b551e36-5ec0-408a-9dd9-154b8adf9580&cache=v2)
需要注意的是如果是Prometheus官方的exporter的话,不用另外添加mysql数据源,只要添加Prometheus数据源即可,只要添加地址为http://IP:9090就可以查到监控的所有主机的数据(
mysql
localhost
数据):试着查询一下
mysql_up
数据,mysql_server已经启动:![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2FkHJvYoLxq8wVycu.png?table=block&id=fac0101f-a39c-4ff1-8b27-5769765230ce&cache=v2)
导入mysql监控模板:
模板链接:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2F4w7Auqsd51CBFvc.png?table=block&id=42b16bce-8978-46ac-96dc-e7d818be594a&cache=v2)
导入模板之后模板会自动读取默认的Prometheus数据源,然后就可以显示mysql运行数据,某些面板要安装插件:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2FwjxfTAsOt6R7MiP.png?table=block&id=66b53d2c-286a-4746-a50c-61885ad02837&cache=v2)
mysql展示数据:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F07%2F17%2FiP7nN8aSTJmWGKo.png?table=block&id=11258a68-0677-41a4-bff4-d65446acd67d&cache=v2)