Prometheus监控MySQL
Prometheus监控MySQL
1:环境
Prometheus-server 10.0.0.10 1C1G
mysql-server 10.0.0.11 1C1G
2:部署Prometheus
3:部署MySQL
[root@mysql-server ~]# yum install -y mariadb-server mariadb
[root@mysql-server ~]# systemctl enable mariadb.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
4:部署mysql-exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
[root@mysql-server ~]# mkdir /usr/local/mysqld_exporter
[root@mysql-server ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar.gz
[root@mysql-server ~]# ls
anaconda-ks.cfg mysqld_exporter-0.10.0.linux-amd64 mysqld_exporter-0.10.0.linux-amd64.tar.gz
[root@mysql-server ~]# cd mysqld_exporter-0.10.0.linux-amd64/
[root@mysql-server mysqld_exporter-0.10.0.linux-amd64]# ls
LICENSE mysqld_exporter NOTICE
[root@mysql-server mysqld_exporter-0.10.0.linux-amd64]# mv mysqld_exporter /usr/local/mysqld_exporter/
5:普通启动与Systemd管理mysqld_exporter
授权连接
[root@mysql-server ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'exporter'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT SELECT ON performance_schema.* TO 'exporter'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 注意:这里只授权了本地登陆,说明这个授权适用于mysql_exporter监控工具部署在MySQL Server上的情况,如果是部署在Prometheus Server上,则需要授权远程登陆
# 启动服务
创建配置信息文件
[root@mysql-server ~]# cd /usr/local/mysqld_exporter
[root@mysql-server ~]# cat << eof>>.my.cnf
[client]
user=exporter
password=123456
eof
方式一,命令行式启动
[root@mysql-server ~]# ./mysqld_exporter -config.my-cnf=".my.cnf" &
方式二,使用 systemd 管理
[root@mysql-server ~]# cat << eof>>/usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter -config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
eof
# 注意:启动加载配置/usr/local/mysqld_exporter/.my.cnf不能再加单引号或者双引号,否则系统会附带识别,从而报配置文件不存在。
# 加载配置并启动
[root@mysql-server mysqld_exporter]# systemctl enable mysqld_exporter.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld_exporter.service to /usr/lib/systemd/system/mysqld_exporter.service.
[root@mysql-server mysqld_exporter]# systemctl start mysqld_exporter.service
[root@mysql-server mysqld_exporter]# ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:9104 [::]:*
LISTEN 0 128 [::]:22 [::]:*
6:配置 prometheus.yml 添加监控目标
[root@prometheus-server ~]# cat /usr/local/prometheus/prometheus.yml
---
- job_name: "mysqld-server"
static_configs:
- targets: ["10.0.0.11:9104"]
#重启
[root@prometheus-server ~]# systemctl restart prometheus.service