search
HomeDatabaseMysql TutorialMonitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

有效监控 MySQL 和 MariaDB 数据库对于保持最佳性能、识别潜在瓶颈以及确保整体系统可靠性至关重要。 Prometheus MySQL Exporter 是一款强大的工具,可提供对数据库指标的详细洞察,这对于主动管理和故障排除至关重要。

在本教程中,您将学习如何设置 Prometheus MySQL 导出器使用手动监控 MySQL 和 MariaDB 数据库 配置和基于脚本的自动化。这些方法提供 灵活选择最佳部署策略和运营

截屏2025-01-09 10.03.57.png

方法一-手动配置

我们继续本节的手动配置方法。

添加 Prometheus 系统用户和组

在此步骤中,您将配置目标 Droplet。让我们创建一个名为“prometheus”的系统用户和组来管理导出器服务。

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

下载并安装 Prometheus MySQL Exporter

在此步骤中,您将下载最新版本的 Prometheus 来自 GitHub 的 MySQL Exporter,提取下载的文件,并将 二进制文件到 /usr/local/bin/目录。

curl -s https://api.github.com/repos/prometheus/mysqld_exporter/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
tar xvf mysqld_exporter*.tar.gz
sudo mv mysqld_exporter-*.linux-amd64/mysqld_exporter /usr/local/bin/
sudo chmod  x /usr/local/bin/mysqld_exporter

创建Prometheus Exporter 数据库用户

以 root 身份登录 MySQL,并为导出器创建具有必要权限的专用用户。

mysql -u root -p

如果需要,请输入 root 密码,您将请参阅 MySQL/MariaDB 提示符。然后执行以下命令(将下面命令中的giveapassword替换为mysqld_exporter数据库用户的新密码)。

创建一个名为mysqld_exporter的新MySQL用户,密码为本地的giveapassword

CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY 'giveapassword';

授予 mysqld_exporter 用户查看进程列表、客户端复制状态和查询所有数据

GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost';

重新加载授权表以应用对用户所做的更改权限。

FLUSH PRIVILEGES;

退出 MySQL 命令行

EXIT

配置数据库凭证

创建配置文件来存储MySQL 导出器

sudo vi /etc/.mysqld_exporter.cnf

将以下用户凭据添加到file:

[client]user=mysqld_exporterpassword=giveapassword

设置配置的所有权权限file:

sudo chown root:prometheus /etc/.mysqld_exporter.cnf

创建 systemd 单元文件

创建 systemd 服务单元用于管理 MySQL Exporter 的文件service:

sudo vi /etc/systemd/system/mysql_exporter.service

将以下数据添加到file:

[Unit]Description=Prometheus MySQL ExporterAfter=network.targetUser=prometheusGroup=prometheus[Service]Type=simpleRestart=alwaysExecStart=/usr/local/bin/mysqld_exporter --config.my-cnf /etc/.mysqld_exporter.cnf --collect.global_status --collect.info_schema.innodb_metrics --collect.auto_increment.columns --collect.info_schema.processlist --collect.binlog_size --collect.info_schema.tablestats --collect.global_variables --collect.info_schema.query_response_time --collect.info_schema.userstats --collect.info_schema.tables --collect.perf_schema.tablelocks --collect.perf_schema.file_events --collect.perf_schema.eventswaits --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.slave_status --web.listen-address=0.0.0.0:9104[Install]WantedBy=multi-user.target

重新加载 systemd 并启动 MySQL Exporter 服务

重新加载 systemd配置并启动 MySQL Exporter服务:

sudo systemctl daemon-reload
sudo systemctl enable mysql_exporter
sudo systemctl start mysql_exporter

正在配置Prometheus Droplet

让我们首先备份 prometheus.yml 文件:

cp /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yml-$(date  '%d%b%Y-%H:%M')

现在登录到您的 Prometheus 服务器并添加要抓取的 MySQL Exporter 端点。

将 IP 地址和端口替换为您的 MySQL Exporter 端点(9104 是 Prometheus MySQLd 的默认端口出口商)。

vi /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: server1_db
    static_configs:
      - targets: ['10.10.1.10:9104']
        labels:
          alias: db1

  - job_name: server2_db
    static_configs:
      - targets: ['10.10.1.11:9104']
        labels:
          alias: db2

方法2-使用脚本进行配置

您还可以通过运行两个脚本来实现这一目标 - 一个用于目标 Droplet,另一个用于 Prometheus Droplet。

让我们从配置目标 Droplet 开始。

第 1 步:通过 SSH 连接到目标 Droplet。

第 2 步:下载目标配置脚本使用以下命令:

wget https://solutions-files.ams3.digitaloceanspaces.com/MySQL-MariaDB-Monitoring/DO_MySQL_MariaDB_Target_Config.sh

第 3 步:下载脚本后,确保它已包含可执行权限由running:

chmod  x DO_MySQL_MariaDB_Target_Config.sh

第 4 步:执行脚本running:

./DO_MySQL_MariaDB_Target_Config.sh

第 5 步:输入 mysql_exporter 用户的新密码,然后输入MySQL 根目录密码:

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

配置完成。

配置 Prometheus Droplet(脚本方法)

第 1 步:通过 SSH 登录 Prometheus Droplet .

第 2 步:使用以下命令下载 Prometheus 配置脚本命令:

wget https://solutions-files.ams3.digitaloceanspaces.com/MySQL-MariaDB-Monitoring/DO_MySQL_MariaDB_Prometheus_Config.sh

这个 bash脚本自动更新Prometheus配置文件添加 新的服务器端点。首先备份当前的 带有时间戳文件名的 prometheus.yml 文件。

然后脚本提示用户指定要添加的服务器数量并验证此输入。

对于每个服务器,它请求一个主机名和IP 地址,确保 每个 IP 地址在配置文件中都是唯一的。有效服务器 配置附加到 prometheus.yml 文件中。更新后 配置完成后,脚本会重启 Prometheus 服务并检查 其状态以确认更改已应用。

最后,它在退出之前等待用户输入。

第 3 步:下载脚本后,通过以下方式确保其具有可执行权限running:

chmod  x DO_MySQL_MariaDB_Prometheus_Config.sh

第 4 步:执行脚本running:

./DO_MySQL_MariaDB_Prometheus_Config.sh

第 5 步:输入要添加到监控的 Droplet 数量。

第6步:输入主机名和IP地址。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

第 7 步:配置完成。

第 8 步:添加后,通过访问 URLprometheushostname:9090/ 检查目标是否已更新目标。

配置 Grafana

步骤1:在浏览器上访问 Grafana-IP:3000 登录 Grafana 仪表板。

步骤 2:转到配置 > 数据源。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

步骤3:点击添加数据源。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

第 4 步:搜索并选择 Prometheus。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

第 5 步:输入 Prometheus 名称和 URL (Prometheus主机名:9090)并单击“保存并测试”。如果看到“数据源正在运行”,则说明您已成功添加数据源。完成后,转到“创建”>“导入”。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

第 6 步:您可以手动配置仪表板或 通过上传 JSON 文件导入仪表板。 JSON 模板 MySQL和MariaDB监控可以在以下链接找到:

https://solutions-files.ams3.digitaloceanspaces.com/MySQL-MariaDB-Monitoring/DO_Grafana-Mysql-MariaDB_Monitoring.json

第7步:填写田野和导入。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

第 8 步:Grafana 仪表板已准备就绪。选择 托管并检查指标是否可见。您可以修改和编辑 根据需要使用仪表板。

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter

The above is the detailed content of Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools