CentOS 7 安装 Prometheus 监控套件全过程总结
一、环境信息与目标组件版本
1. 系统环境
2. 目标组件及版本
| 组件 | 版本 | 功能 | 监听端口 |
|---|---|---|---|
| Prometheus | 2.45.0 | 监控核心(数据采集与存储) | 9090 |
| Node Exporter | 1.6.1 | 主机系统监控(CPU / 内存 / 磁盘) | 9100 |
| MySQL Exporter | 0.15.0 | MySQL 数据库监控 | 9104 |
| Grafana | 12.1.1 | 监控可视化面板 | 3000 |
二、安装前准备(解决依赖与环境配置)
CentOS 7 官方源已停止维护,需先修复 YUM 源;同时配置网络、权限等基础环境,避免后续组件安装失败。
1. 修复 YUM 源(关键步骤)
操作命令与注释
\# 1. 进入 YUM 源配置目录
cd /etc/yum.repos.d/
\# 2. 删除原有失效的 repo 文件(风险操作,需确认无自定义重要源)
rm -rf \*
\# 3. 下载阿里云 CentOS 7 镜像源(推荐,稳定性高)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
\# 4. 下载 EPEL 扩展源(后续安装依赖可能需要,原命令“yum install epel”有误,需修正)
yum install -y epel-release
\# 5. 清理旧缓存并生成新缓存
\# 6. 非阿里云ECS用户会出现 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息,不影响使用。用户也可自行修改相关配置: eg:
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
注意事项
-
若
curl命令不存在,先执行yum install -y curl; -
避免使用错误的 repo URL(如用户曾误用清华源的
help页面 URL),正确第三方源地址需从镜像站官方文档获取(如阿里云、清华源的 CentOS 7 专区); -
若
yum makecache失败,优先检查 DNS 解析(见下一步)。
2. 配置 DNS 与禁用 NetworkManager
操作命令与注释
\# 1. 写入阿里云/114 公共 DNS(避免解析镜像站失败)
echo "nameserver 223.5.5.5" > /etc/resolv.conf # 阿里云 DNS
echo "nameserver 223.6.6.6" >> /etc/resolv.conf # 阿里云 DNS
echo "nameserver 114.114.114.114" >> /etc/resolv.conf # 114 DNS
\# 2. 停止并禁用 NetworkManager(避免其自动覆盖 resolv.conf)
systemctl stop NetworkManager # 立即停止服务
systemctl disable NetworkManager # 禁止开机自启(原注释“立即停止”有误,此处修正)
\# 3. 重启网络服务使 DNS 生效
systemctl restart network
\# 4. 验证 DNS 解析(确保能解析镜像站)
getent hosts mirrors.aliyun.com
ping mirrors.aliyun.com -c 4 # 测试网络连通性
注意事项
-
NetworkManager 是核心坑点:CentOS 7 中 NetworkManager 会自动管理网络配置,可能覆盖手动写入的
resolv.conf,导致 DNS 解析失效(表现为yum或wget无法访问外部地址),必须禁用; -
若
ping失败,检查网关配置(cat /etc/sysconfig/network-scripts/ifcfg-eth0),确保网络通畅。
3. 关闭 SELinux 与开放防火墙端口
操作命令与注释
\# 1. 临时关闭 SELinux(立即生效,无需重启)
setenforce 0
\# 2. 永久关闭 SELinux(重启后生效,避免后续服务端口被拦截)
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
\# 3. 开放监控套件所需端口(永久生效)
firewall-cmd --zone=public --add-port=9090/tcp --permanent # Prometheus
firewall-cmd --zone=public --add-port=9100/tcp --permanent # Node Exporter
firewall-cmd --zone=public --add-port=9104/tcp --permanent # MySQL Exporter
firewall-cmd --zone=public --add-port=3000/tcp --permanent # Grafana
\# 4. 重载防火墙规则(使端口配置生效)
firewall-cmd --reload
\# 5. 安装基础工具(后续下载/解压需用到)
yum install -y wget tar vim curl lsof
注意事项
-
SELinux 若未关闭,会拦截 Prometheus/Node Exporter 绑定端口(表现为
systemctl start失败,日志提示 “permission denied”); -
防火墙端口必须开放,否则外部无法访问 Prometheus(
http://IP:9090)和 Grafana(http://IP:3000)界面。
三、组件安装与配置(按顺序执行)
1. Prometheus 安装与配置
操作命令与注释
\# 1. 创建 Prometheus 系统用户(无登录权限,安全性更高)
useradd -r -m -s /sbin/nologin prometheus
\# 2. 创建 Prometheus 目录(配置文件/数据/日志)
mkdir -p /etc/prometheus /var/lib/prometheus /var/log/prometheus
\# 3. 授权目录归属(避免权限不足)
chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus /var/log/prometheus
\# 4. 下载并解压 Prometheus 二进制包(/tmp 为临时目录,避免占用根目录)
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.45.0.linux-amd64.tar.gz
cd prometheus-2.45.0.linux-amd64
\# 5. 复制二进制文件到系统路径(便于全局调用)
cp prometheus promtool /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
\# 6. 复制配置文件与静态资源(Web 界面依赖)
cp prometheus.yml /etc/prometheus/
cp -r consoles/ console\_libraries/ /etc/prometheus/
chown -R prometheus:prometheus /etc/prometheus/
\# 7. 创建 systemd 服务文件(便于管理启停)
cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
# 启动参数:指定配置文件、数据存储路径、Web 监听地址
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.listen-address=0.0.0.0:9090
[Install]
WantedBy=multi-user.target
EOF
\# 8. 启动并设置开机自启
systemctl daemon-reload # 重新加载 systemd 配置
systemctl start prometheus
systemctl enable prometheus
\# 9. 验证服务状态(确保“active (running)”)
systemctl status prometheus
关键配置文件:/etc/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# 监控本机 Node Exporter(需替换为实际 IP)
- job_name: 'node'
static_configs: # 修正:缩进 4 个空格(与 job_name 对齐)
- targets: ['192.168.200.200:9100'] # 修正:缩进 8 个空格(在 static_configs 下)
注意事项
-
YAML 缩进错误是高频坑:
scrape_configs下的job_name需缩进 2 空格,static_configs需缩进 4 空格,targets需缩进 6 空格;若缩进错误,服务启动失败,可通过journalctl -u prometheus -xe查看日志报错; -
若
wget下载失败(GitHub 访问慢),可替换为 HTTP 协议(http://github.com/...)或国内镜像(如https://mirrors.huaweicloud.com/github-release/...)。
2. Node Exporter 安装与配置(监控主机系统)
操作命令与注释
\# 1. 进入临时目录下载二进制包
cd /tmp
wget https://github.com/prometheus/node\_exporter/releases/download/v1.6.1/node\_exporter-1.6.1.linux-amd64.tar.gz
\# 2. 解压并复制二进制文件
tar -zxvf node\_exporter-1.6.1.linux-amd64.tar.gz
cd node\_exporter-1.6.1.linux-amd64
cp node\_exporter /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/node\_exporter
\# 3. 创建 systemd 服务文件
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
# 启动参数:启用系统/进程/磁盘监控,监听 9100 端口
ExecStart=/usr/local/bin/node_exporter \
--collector.systemd \
--collector.processes \
--collector.diskstats \
--web.listen-address=0.0.0.0:9100
[Install]
WantedBy=multi-user.target
EOF
\# 4. 启动并设置开机自启
systemctl daemon-reload
systemctl start node\_exporter
systemctl enable node\_exporter
\# 5. 验证服务状态
systemctl status node\_exporter
注意事项
-
Node Exporter 依赖
prometheus用户权限,若提示 “permission denied”,检查/usr/local/bin/node_exporter的归属; -
启用
--collector.systemd可监控系统服务状态(如node_systemd_unit_state指标),需确保prometheus用户有权限访问 systemd 接口。
3. MySQL Exporter 安装与配置(监控 MySQL)
操作命令与注释(用户原操作遗漏部分已补充)
\# 1. 进入临时目录下载二进制包
cd /tmp
wget https://github.com/prometheus/mysqld\_exporter/releases/download/v0.15.0/mysqld\_exporter-0.15.0.linux-amd64.tar.gz
\# 2. 解压并复制二进制文件
tar -zxvf mysqld\_exporter-0.15.0.linux-amd64.tar.gz
cd mysqld\_exporter-0.15.0.linux-amd64
cp mysqld\_exporter /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/mysqld\_exporter
\# 3. 【关键】创建 MySQL 连接配置文件(用户原操作遗漏,必须补充)
mkdir -p /etc/mysqld\_exporter
cat > /etc/mysqld_exporter/.my.cnf << EOF
[client]
user=exporter_user # MySQL 监控用户(需提前在 MySQL 中创建)
password=Exporter@123 # 密码
host=127.0.0.1 # MySQL 地址(本地填 127.0.0.1,远程填实际 IP)
port=3306 # MySQL 端口
EOF
\# 4. 授权配置文件(避免密码泄露)
chown -R prometheus:prometheus /etc/mysqld\_exporter/
chmod 600 /etc/mysqld\_exporter/.my.cnf # 仅所有者可读
\# 5. 创建 systemd 服务文件
cat > /etc/systemd/system/mysqld\_exporter.service << EOF
[Unit]
Description=MySQL Exporter
Wants=network-online.target
After=network-online.target mysql.service
# 依赖 MySQL 服务
[Service]
User=prometheus
Group=prometheus
Type=simple
# 启动参数:指定 MySQL 连接配置文件,监听 9104 端口
ExecStart=/usr/local/bin/mysqld_exporter \
--config.my-cnf=/etc/mysqld_exporter/.my.cnf \
--web.listen-address=0.0.0.0:9104
[Install]
WantedBy=multi-user.target
EOF
\# 6. 启动并设置开机自启
systemctl daemon-reload
systemctl start mysqld\_exporter
systemctl enable mysqld\_exporter
\# 7. 验证服务状态
systemctl status mysqld\_exporter
前置要求(MySQL 端配置)
需在 MySQL 中创建监控用户并授权:
CREATE USER 'exporter\_user'@'localhost' IDENTIFIED BY 'Exporter@123';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON \*.\* TO 'exporter\_user'@'localhost';
FLUSH PRIVILEGES;
注意事项
-
用户原操作遗漏连接配置:MySQL Exporter 必须通过
--config.my``-cnf指定 MySQL 账号密码,否则无法采集数据,服务虽启动但无指标输出; -
若提示 “access denied”,检查 MySQL 用户权限、密码正确性,或远程连接时的 IP 白名单。
4. Grafana 安装与配置(可视化面板)
操作命令与注释
\# 1. 添加 Grafana 官方 YUM 源
cat > /etc/yum.repos.d/grafana.repo << EOF
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
\# 2. 安装 Grafana
yum install -y grafana
\# 3. 【修复权限】用户原操作遇到启动失败,需授权数据目录
chown -R grafana:grafana /var/lib/grafana/
chmod -R 755 /var/lib/grafana/
\# 4. 【清理插件缓存】若提示插件错误,删除缓存目录
rm -rf /var/lib/grafana/plugins/.cache
\# 5. 启动并设置开机自启
systemctl start grafana-server
systemctl enable grafana-server
\# 6. 验证服务状态
systemctl status grafana-server
\# 7. 查看日志排查错误(若启动失败)
cat /var/log/grafana/grafana.log | grep -i "error"
[root@Test ~]# cat /var/log/grafana/grafana.log | grep -i "error"
logger=migrator t=2025-08-26T17:05:37.378880923+08:00 level=info msg="Executing migration" id="add snapshot error_string column"
logger=migrator t=2025-08-26T17:05:37.386846059+08:00 level=info msg="Migration successfully executed" id="add snapshot error_string column" duration=7.961711ms
logger=migrator t=2025-08-26T17:05:37.484979331+08:00 level=info msg="Executing migration" id="add cloud_migration_resource.error_code column"
logger=migrator t=2025-08-26T17:05:37.492673825+08:00 level=info msg="Migration successfully executed" id="add cloud_migration_resource.error_code column" duration=7.689799ms
logger=renderer.manager t=2025-08-26T17:05:37.848383107+08:00 level=error msg="Failed to get renderer plugin sources" error="failed to open plugins path"
logger=plugin.sources t=2025-08-26T17:05:38.064594561+08:00 level=error msg="Failed to load external plugins" error="failed to open plugins path"
logger=plugins.registration t=2025-08-26T17:05:38.095987054+08:00 level=error msg="Could not register plugin" pluginId=table error="plugin table is already registered"
logger=plugins.initialization t=2025-08-26T17:05:38.096063152+08:00 level=error msg="Could not initialize plugin" pluginId=table error="plugin table is already registered"
logger=renderer.manager t=2025-08-26T17:05:38.150109925+08:00 level=error msg="Failed to get renderer plugin sources" error="failed to open plugins path"
# 1. 停止 Grafana 服务
systemctl stop grafana-server
# 2. 检查并创建默认插件目录(若不存在)
if [ ! -d "/var/lib/grafana/plugins" ]; then
mkdir -p /var/lib/grafana/plugins
fi
# 3. 授权插件目录(确保 Grafana 进程有权访问)
chown -R grafana:grafana /var/lib/grafana/plugins
chmod -R 755 /var/lib/grafana/plugins # 只读权限,避免误改
初始访问与配置
-
访问 Grafana 界面:
http://IP:3000; -
初始账号密码:
admin/admin(首次登录需修改密码); -
添加 Prometheus 数据源:
-
左侧菜单 → Data Sources → Add data source → 选择 Prometheus;
-
URL 填写
http://localhost:9090(若 Grafana 与 Prometheus 同主机),点击 Save & Test;


- 导入 Node Exporter 仪表盘:
-
左侧菜单 → Dashboards → Import → 输入仪表盘 ID(如官方 Node Exporter 仪表盘 ID:
1860),选择 Prometheus 数据源,完成导入。方法二:手动导入 1860 仪表盘(Node Exporter Full)
获取 JSON 文件:https://grafana.com/grafana/dashboards/1860-node-exporter-full/
点击右侧 Download JSON 按钮(若提示登录,注册一个免费账号即可),保存文件(如 1860_rev28.json)

注意事项
- Grafana 启动失败高频原因:
-
数据目录权限不足(
/var/lib/grafana/归属root而非grafana),解决:chown -R grafana:grafana /var/lib/grafana/; -
插件缓存冲突,解决:删除
/var/lib/grafana/plugins/.cache后重启; -
端口被占用,解决:用
lsof -i:3000查找占用进程,杀死后重启。
四、常见易错点、原因及解决方法
| 易错点描述 | 根本原因 | 解决方法 |
|---|---|---|
YUM 源 yum makecache 失败 | 原有 repo 文件失效,或 DNS 解析失败 | 重新下载阿里云 / 清华 CentOS 7 repo;配置公共 DNS(223.5.5.5),禁用 NetworkManager |
| Prometheus/Node Exporter 启动失败 | SELinux 拦截端口,或配置文件缩进错误 | 临时 setenforce 0 + 永久关闭 SELinux;用 yamllint 检查 YAML 缩进 |
| Grafana 访问白屏或启动失败 | 数据目录权限不足,或插件缓存冲突 | chown -R grafana:grafana /var/lib/grafana/;删除 /var/lib/grafana/plugins/.cache |
| MySQL Exporter 无指标输出 | 未配置 MySQL 连接文件,或用户权限不足 | 创建 /etc/mysqld_exporter/.my.cnf;在 MySQL 中给监控用户授权 PROCESS, SELECT 权限 |
| GitHub 下载二进制包超时 | 网络限制(GitHub 访问慢) | 改用 HTTP 协议下载;或用国内镜像(华为云、阿里云 GitHub 镜像);手动下载后上传 |
| 外部无法访问 Prometheus/Grafana 界面 | 防火墙端口未开放,或服务器 IP 配置错误 | 执行 firewall-cmd --add-port=9090/tcp --permanent && firewall-cmd --reload;检查服务器 IP 是否为内网可访问 |
五、验证步骤
- 验证 Prometheus 服务:
-
访问
http://服务器IP:9090,查看界面是否正常; -
进入
Status → Targets,确认prometheus和node目标状态为UP。
- 验证 Node Exporter 指标:
- 访问
http://服务器IP:9100/metrics,查看是否输出主机监控指标(如node_cpu_seconds_total)。
- 验证 MySQL Exporter 指标:
- 访问
http://服务器IP:9104/metrics,查看是否输出 MySQL 指标(如mysql_up)。
- 验证 Grafana 可视化:
- 访问
http://服务器IP:3000,登录后导入仪表盘 ID1860(Node Exporter),查看主机 CPU、内存等图表是否正常显示。
577



被折叠的 条评论
为什么被折叠?



