普罗米修斯监控sql(CentOS7)

普罗米修斯监控sql(CentOS7)

1.准备环境

复制代码
确认系统版本
cat /etc/centos-release

输出应该类似:CentOS Linux release 7.x (Core)

安装必备工具

sudo yum install -y wget tar vim net-tools

2.安装必备工具

复制代码
sudo yum install -y wget tar vim net-tools

3.创建用户和目录

复制代码
为了安全起见,我们给 Prometheus 创建一个单独的用户。
sudo useradd -rs /bin/false prometheus
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus

4.下载 Prometheus

复制代码
访问 Prometheus 官方下载页面
 或直接用 wget 下载最新版本,例如:
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz
或者使用安装包解压

5.解压:

复制代码
tar xvf prometheus-2.47.0.linux-amd64.tar.gz
sudo cp prometheus-2.47.0.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.47.0.linux-amd64/promtool /usr/local/bin/

6.复制配置文件和控制台文件:

复制代码
sudo cp -r prometheus-2.47.0.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-2.47.0.linux-amd64/console_libraries /etc/prometheus
sudo cp prometheus-2.47.0.linux-amd64/prometheus.yml /etc/prometheus/

7.修改目录权限

复制代码
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

8.修改 Prometheus 配置

复制代码
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

9.创建 systemd 服务

复制代码
为了让 Prometheus 开机自启,我们创建 systemd 服务文件
sudo vim /etc/systemd/system/prometheus.service
写入:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
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

[Install]
WantedBy=multi-user.target

10.启动

复制代码
重新加载 systemd:
sudo systemctl daemon-reload
启动并设置开机自启:
sudo systemctl start prometheus
sudo systemctl enable prometheus
查看状态:
sudo systemctl status prometheus
如果显示 active (running),说明启动成功。
浏览器访问

11.安装 MySQL Exporter

复制代码
Prometheus 官方提供了一个 mysqld_exporter,可以把 MySQL 的性能指标暴露给 Prometheus。

下载 mysqld_exporter(国内镜像示例):

cd /tmp
wget https://mirrors.tuna.tsinghua.edu.cn/prometheus/releases/mysqld_exporter-0.23.0.linux-amd64.tar.gz
tar xvf mysqld_exporter-0.23.0.linux-amd64.tar.gz
sudo cp mysqld_exporter-0.23.0.linux-amd64/mysqld_exporter /usr/local/bin/
sudo useradd -rs /bin/false mysqld

步骤 2:创建 MySQL 监控账号

登录 MySQL,创建一个只读账号给 Exporter 使用:

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'password123';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
注意:password123 可以换成你自己的安全密码。

步骤 3:配置 mysqld_exporter
设置环境变量,让 exporter 能访问 MySQL:
sudo vim /etc/profile.d/mysqld_exporter.sh

写入:
export DATA_SOURCE_NAME="exporter:password123@(localhost:3306)/"
注意替换账号和密码。

加载环境变量:

source /etc/profile.d/mysqld_exporter.sh

步骤 4:创建 systemd 服务
sudo vim /etc/systemd/system/mysqld_exporter.service
写入

[Unit]
Description=MySQL Exporter
After=network.target

[Service]
User=mysqld
Group=mysqld
Type=simple
EnvironmentFile=/etc/profile.d/mysqld_exporter.sh
ExecStart=/usr/local/bin/mysqld_exporter

[Install]
WantedBy=multi-user.target
启动服务:

sudo systemctl daemon-reload
sudo systemctl start mysqld_exporter
sudo systemctl enable mysqld_exporter

步骤 5:在 Prometheus 中添加监控目标

编辑 Prometheus 配置 /etc/prometheus/prometheus.yml:

scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['localhost:9104']   # mysqld_exporter 默认端口是 9104


保存后,重新加载 Prometheus 配置:

sudo systemctl reload prometheus

步骤 6:访问 Prometheus

打开浏览器访问:

http://你的服务器IP:9090

http://你的服务器IP:9090
在 Targets 页面检查 mysql 的状态,如果显示 UP,说明 Prometheus 已经成功抓取 MySQL 指标了。

你可以在 Prometheus 中直接用 SQL 指标查询,例如:

nginx
复制代码
mysql_global_status_threads_connected
mysql_global_status_queries

/你的服务器IP:9090

在 Targets 页面检查 mysql 的状态,如果显示 UP,说明 Prometheus 已经成功抓取 MySQL 指标了。

你可以在 Prometheus 中直接用 SQL 指标查询,例如:

nginx

复制代码

mysql_global_status_threads_connected

mysql_global_status_queries

复制代码
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%5CUsers%5CY%5CDesktop%5C%E7%85%A7%E7%89%87%5Cimage-20251201203222702.png&pos_id=img-zSOeqcsK-1764592426308)
相关推荐
科技小花5 小时前
全球化深水区,数据治理成为企业出海 “核心竞争力”
大数据·数据库·人工智能·数据治理·数据中台·全球化
X56616 小时前
如何在 Laravel 中正确保存嵌套动态表单数据(主服务与子服务)
jvm·数据库·python
虹科网络安全7 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
2301_771717218 小时前
解决mysql报错:1406, Data too long for column
android·数据库·mysql
小江的记录本8 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka
dvjr cloi8 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
dFObBIMmai9 小时前
MySQL主从同步中大事务导致的延迟_如何拆分大事务优化同步
jvm·数据库·python
szccyw09 小时前
mysql如何限制特定存储过程执行权限_MySQL存储过程安全访问
jvm·数据库·python
czlczl200209259 小时前
利用“延迟关联”优化 MySQL 巨量数据的深分页查询
数据库·mysql
ACP广源盛1392462567310 小时前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑