Grafana +MySQL Data Source

本文介绍了如何在Grafana中配置和使用MariaDB(MySQL的一种变体)数据源。首先,详细阐述了Grafana和MariaDB的安装过程,接着通过将Excel测试数据导入MariaDB,展示了数据准备步骤。然后,文章指导读者在Grafana中添加、配置数据源,并创建模板,最终实现数据查询及指标上下限的设定。

1. grafana 、mysql安装


  • grafana 安装

Grafana Linux版本安装地址

mysql 安装
这里选 mariadb
  • 安装
 yum -y install mariadb mariadb-server
  • 启动MariaDB,设置开机自启动
systemctl start mariadb

systemctl enable mariadb
  • mariadb初始化
[root@cloud01 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] 
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@cloud01 ~]# 
  • 测试登入mariadb,创建表grafana,修改连接权限
[root@cloud01 ~]# mysql -uroot -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.56-MariaDB MariaDB Server医用

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [mysql]> CREATE DATABASE grafana;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> 
MariaDB [mysql]> select user,host from user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

MariaDB [mysql]> update user set host = '%' where user = 'root'; 
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
MariaDB [mysql]> select user,host from user;
+------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | 127.0.0.1 |
| root | ::1       |
+------+-----------+
3 rows in set (0.00 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> 

2.数据导入mariadb

  • 前期测试直接把测试数据(excle表)导入mariadb中。
  • 贴一个grafana 中使用mysql 的官方文档
    Using MySQL In Grafana

3.Grafana+MariaDB Data Source

- 添加数据源
添加数据源
- 配置数据源
编辑配置
- 创建模板
创建模板
- 选grafana
选grafana
- 编辑配置
这里写图片描述
- 选着添加好的数据源
这里写图片描述
- Add Query

SELECT
  UNIX_TIMESTAMP(Date_Time) as time_sec,
  Total_Current as value,
  Timer_Id as metric
FROM workinfo
WHERE $__timeFilter(Date_Time)
ORDER BY Date_Time ASC

- 指标上下限设置
这里写图片描述

<think>我们正在使用Prometheus和Grafana监控MySQL主从状态。以下是步骤:1.确保MySQL主从已经配置好,并且主从状态正常。2.在MySQL服务器上安装mysqld_exporter,用于将MySQL的监控指标暴露给Prometheus。3.配置Prometheus抓取mysqld_exporter的数据。4.在Grafana中配置数据源为Prometheus,并导入MySQL监控仪表板。详细步骤:一、安装和配置mysqld_exporter1.下载mysqld_exporter,例如从Prometheus官网下载。2.解压并安装。3.创建MySQL监控用户并授权(如果尚未创建):在MySQL主库和从库上分别执行(如果监控多个实例,则每个实例都需要):CREATEUSER'exporter'@'localhost'IDENTIFIEDBY'password'WITHMAX_USER_CONNECTIONS3;GRANTPROCESS,REPLICATIONCLIENT,SELECTON*.*TO'exporter'@'localhost';4.配置mysqld_exporter连接MySQL的配置文件(例如.my.cnf):[client]user=exporterpassword=password注意:该文件应放在mysqld_exporter的目录下,并设置权限(仅允许运行mysqld_exporter的用户可读)。5.启动mysqld_exporter。通常可以使用如下命令:./mysqld_exporter--config.my-cnf=".my.cnf"或者使用systemd管理。二、配置Prometheus抓取mysqld_exporter在Prometheus的配置文件prometheus.yml中添加job,例如:-job_name:'mysql'static_configs:-targets:['mysql_host1:9104','mysql_host2:9104']labels:instance:mysql_master#给主库打标签-targets:['mysql_slave1:9104']labels:instance:mysql_slave1#给从库打标签注意:默认mysqld_exporter监听端口为9104。三、在Grafana中配置1.添加Prometheus数据源(如果尚未添加):Name:PrometheusURL:http://prometheus_host:9090Access:Server(默认)Save&Test2.导入MySQL监控仪表板。常用的仪表板有:MySQLOverview(仪表板ID:7362)或者Percona提供的仪表板(ID:11323)导入方法:Create->Import->输入仪表板ID->Load->选择Prometheus数据源->Import四、监控主从状态的关键指标在Grafana仪表板中,我们通常关注以下指标:-主从延迟(Seconds_Behind_Master):通过mysql_slave_status_seconds_behind_master指标获取。-主从状态(Slave_IO_Running,Slave_SQL_Running):通过mysql_slave_status_slave_io_running和mysql_slave_status_slave_sql_running指标获取(值为0或1)。-主库的binlog位置和从库读取的位置等。注意:如果使用Percona的mysqld_exporter,则指标名前缀可能为mysql_global_status_*或mysql_slave_status_*。不同版本的exporter指标名可能略有不同,请根据实际情况调整。五、验证在Grafana中查看导入的仪表板,确认主从状态正常显示。引用说明:[^2]中提到了在Grafana中添加Prometheus数据源和导入仪表板(ID:7362)的方法。[^3]中提到了监控MySQL的一些指标,如binlog相关指标,我们可以借鉴其表达式。根据上述步骤,您应该能够成功配置MySQL主从监控。相关问题:1.如何为多个MySQL实例配置监控?2.如何自定义Grafana仪表板来监控特定的主从延迟阈值?3.如何设置报警规则以便在主从延迟过大或复制中断时收到通知?</think>### 如何用 Prometheus 和 Grafana 实现 MySQL 主从监控 以下是完整的配置教程,分步实现 MySQL 主从监控: --- #### **一、环境准备** 1. **安装 Prometheus** ```bash # 解压安装包 tar -zxvf prometheus-*.tar.gz mv prometheus-* /opt/prometheus ``` ```yaml # 修改配置文件 prometheus.yml scrape_configs: - job_name: 'mysql' static_configs: - targets: ['mysql_master:9104', 'mysql_slave:9104'] # 主从节点地址 ``` 2. **安装 Grafana** ```bash docker run -d -p 3000:3000 --name=grafana grafana/grafana ``` --- #### **二、配置 MySQL 监控暴露器** 1. **部署 `mysqld_exporter`**(主从节点均需安装) ```bash docker run -d \ --name mysqld_exporter \ -p 9104:9104 \ -e DATA_SOURCE_NAME="exporter:password@(mysql_host:3306)/" \ prom/mysqld-exporter ``` > **注**:需在 MySQL 创建监控账户: > ```sql > CREATE USER 'exporter'@'%' IDENTIFIED BY 'password'; > GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%'; > ``` 2. **验证数据抓取** 访问 `http://mysql_host:9104/metrics` 应看到 MySQL 指标输出。 --- #### **三、Grafana 配置** 1. **添加 Prometheus 数据源** - 访问 `http://grafana_host:3000` 登录(默认 admin/admin) - `Configuration` > `Data Sources` > `Add data source` 选择 Prometheus - URL 填写 `http://prometheus_host:9090` > `Save & Test` [^2] 2. **导入 MySQL 监控仪表板** - `Create` > `Import` > 输入仪表板 ID **`7362`**(官方 MySQL 仪表板)[^2] - 选择 Prometheus 数据源 > `Import` --- #### **四、关键主从监控指标** 在 Grafana 仪表板中重点关注以下指标: | **指标名** | **说明** | **健康值** | |------------|----------|------------| | `mysql_slave_status_seconds_behind_master` | 主从延迟秒数 | `< 30s` | | `mysql_slave_status_slave_io_running` | IO 线程状态 | `1` (运行中) | | `mysql_slave_status_slave_sql_running` | SQL 线程状态 | `1` (运行中) | | `mysql_global_status_wsrep_cluster_status` | 集群状态 | `Primary` | > **异常检测示例**: > 当复制中断时,可在 Prometheus 配置告警规则: > ```yaml > rules: > - alert: MySQL_Replication_Down > expr: mysql_slave_status_slave_io_running == 0 OR mysql_slave_status_slave_sql_running == 0 > for: 1m > ``` --- #### **五、效果验证** 1. 在 Grafana 仪表板查看主从延迟曲线图 2. 检查 `Replication` 面板的线程状态是否为绿色 3. 模拟主从故障(如停止复制线程),观察告警触发 > **故障排查**: > 若无数据,检查: > 1. Prometheus `Targets` 页面是否显示 `UP` > 2. MySQL 用户权限是否正确 > 3. 防火墙是否开放 `9104` 端口 --- #### **常见问题解决** - **监控数据缺失**:确保 `mysqld_exporter` 日志无连接错误 - **主从状态不显示**:验证从库是否启用 `SHOW SLAVE STATUS` - **仪表板无数据**:检查 Grafana 时间范围是否选择正确 通过以上步骤,即可实时监控 MySQL 主从状态,及时响应复制延迟或中断问题。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值