1 安装 node_exporter node_exporter 是Prometheus官方的Linux指标采集器,负责收集CPU、内存、磁盘IO、网络、负载、文件描述符 等。
1 2 3 4 5 6 7 cd /usr/local # 下载最新稳定版 node_exporter wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz # 解压 tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz # 重命名,方便用 mv node_exporter-1.8.2.linux-amd64 node_exporter
启动 1 nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
验证启动 1 ps -ef | grep node_exporter
2 安装 Prometheus 下载 1 2 3 wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz mv prometheus-2.53.0.linux-amd64 prometheus
速度超慢,本地下载后使用putty上传。
配置 1 vim /usr/local/prometheus/prometheus.yml
prometheus.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 global: scrape_interval: 5s # 每5秒拉取一次指标数据 evaluation_interval: 15s # 抓取任务配置 scrape_configs: # 监控Linux服务器 - job_name: "linux-server" static_configs: - targets: ["localhost:9100"] # 添加MySQL监控 # - job_name: "mysql" # static_configs: # - targets: ["localhost:9104"]
创建数据存储目录 1 mkdir -p /data/prometheus
启动 1 2 3 4 5 nohup /usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path=/data/prometheus \ --storage.tsdb.retention.time=30d \ # 长时间压测,数据六久一点 > /dev/null 2>&1 &
验证
访问 Web 界面:http://IP:9090。
菜单 Status → Targets,linux-server状态为UP。
3 安装mysqld_exporter 创建监控专用账号:
1 2 3 4 5 CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'Exporter@123'; -- 只读权限 GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; FLUSH PRIVILEGES; exit;
下载 1 2 3 4 cd /usr/local wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz tar -zxvf mysqld_exporter-0.15.0.linux-amd64.tar.gz mv mysqld_exporter-0.15.0.linux-amd64 mysqld_exporter
创建配置文件 1 vim /usr/local/mysqld_exporter/.my.cnf
配置
1 2 3 4 5 [client] user=exporter password=xxx host=localhost port=3306
启动 mysqld_exporter 1 2 3 nohup /usr/local/mysqld_exporter/mysqld_exporter \ --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf \ > /dev/null 2>&1 &
Prometheus监控mysql配置 1 vim /usr/local/prometheus/prometheus.yml
配置
1 2 3 4 5 6 7 8 scrape_configs: - job_name: "linux-server" static_configs: - targets: ["localhost:9100"] - job_name: "mysql" static_configs: - targets: ["localhost:9104"]
重启 Prometheus, 进入查看监控mysql的任务状态是否为up。
4 安装 Grafana 1 sudo yum install -y https://dl.grafana.com/oss/release/grafana-10.0.0-1.x86_64.rpm
更多安装教程见:https://grafana.org.cn/docs/grafana/latest/setup-grafana/installation/
启动并设置开机自启
1 2 3 4 # 启动服务 systemctl start grafana-server # 开机自启 systemctl enable grafana-server
访问:IP:3000,使用admin-admin登录后,修改一个新密码。
添加数据源 Connections → Data sources → Add data source 添加Prometheus,配置server url: http://localhost:9090.点击底部 Save & test看是否提示成功。
创建监控大盘 这里直接用别人建好的。 左侧菜单 → Dashboards → New → Import:输入Linux监控大盘的ID。 点击Load,选择数据源,这里选择Prometheus。点击Import导入即可。
再创建一个mysql监控盘的ID,步骤同上。