当前位置:网站首页>开源一夏 | 教你快速实现“基于Docker快速构建基于Prometheus的MySQL监控系统”
开源一夏 | 教你快速实现“基于Docker快速构建基于Prometheus的MySQL监控系统”
2022-08-03 10:06:00 【InfoQ】
作者:知识浅谈,CSDN签约讲师,后端领域优质创作者,阿里云社区技术博主,热爱分享创作 公众号:知识浅谈
擅长领域:全栈工程师、爬虫、ACM算法
联系方式vx:zsqtcc
这次都给他拿下基于Docker快速构建基于Prometheus的MySQL监控系统
先来捋一下数据流的传输

正菜来了
环境
:
Mysql
:127.0.0.1:3306
mysql_export
: 127.0.0.1:9104
prometheus
:127.0.0.1:9090
grafana
:127.0.0.1:3000
环境设置
:(防火墙放行这些指定的端口)
firewalld -cmd --zone=public --add-port=3306/tcp --permanent
firewalld -cmd --zone=public --add-port=9104/tcp --permanent
firewalld -cmd --zone=public --add-port=9090/tcp --permanent
firewalld -cmd --zone=public --add-port=3000/tcp --permanent
Mysql的创建
第 1 步:
schema文件设置
- 创建init.d配置文件的文件夹
mkdir -p /etc/mysql/init.d
- 配置文件的设置
SET NAMES utf8mb4;
SET @[email protected]@UNIQUE_CHECKS,UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE,SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS sakila;
CREATE SCHEMA sakila;
USE sakila;
CREATE TABLE actor(
actor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(actor_id),
KEY idx_actor_last_name (last_name)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
# 创建远程的用户名和密码用于连接数据库
CREATE USER [email protected]'%'IDENTIFIED with mysql_native_password BY 'remote';
grant all privileges on *.* to [email protected]'%';
# 创建exporter需要读取数据库日志信息的登陆账户,并授予权限
CREATE USER 'exporter'@'%' IDENTIFIED BY 'exporter';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'%';
GRANT SELECT ON performance_schema.* TO 'exporter'@'%';
第 2 步:
mysql数据库的创建
解释:docker-entrypoint-initdb.d这个为创建mysql的时候默认执行的初始脚本
docker run -p 3306:3306 --name db -v /etc/mysql/init.d:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=root -d mysql:8
第 4 步:
创建之后结果

mysql_exporter搭建
第 1 步:
创建mysql_exporter容器
docker run -d -p 9104:9104 --name mysql_exporter -e DATA_SOURCE_NAME="exporter:[email protected](127.0.0.1:3306)/sakila" prom/mysqld-exporter
注意上边的命令需要制定数据源:
-e DATA_SOURCE_NAME="exporter:[email protected](你的mysql数据库所在的ip:3306)/sakila"
第 2 步: 查看运行结果

访问ip:9104

Prometheus搭建
第 1 步:
创建prometheus配置文件
- mkdir -p /etc/prometheus
- vi /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']
### 以下内容为mysql_exporter的配置,主要是这个地方
- job_name: 'mysql_promethues'
scrape_interval: 5s
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:9104']
第 2 步 :
prometheus docker容器的创建
docker run -d --name=prometheus -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -p 9090:9090 bitnami/prometheus:latest
第 3 步:
查看结果

Grafana搭建
第 1 步:
创建grafana容器
docker run -d --name=grafana -p 3000:3000 grafana/grafana
第 2 步:
登陆账号密码都是admin

第 3 步 :
指定数据源






7362是针对mysql监控的。

第 3 步:
查看结果

总结
通过Prometheus与Grafana,成功监控mysql的运行状态,像是锁状态等一些指标都能够可视化出来。
边栏推荐
猜你喜欢
随机推荐
milvus
ClickHouse查询语句详解
CRT command keys
SAP Analytics Cloud 和 SAP Cloud for Customer 两款 SaaS 软件的集成
What exactly does a firmware engineer do?
Mysql OCP 73 questions
Apple Silicon配置二进制环境(二)
mysql数据库配置性能调优
Promise 一: 基本问题
报告:想学AI的学生数量已涨200%,老师都不够用了
select statement in go
js函数防抖和函数节流及其使用场景。
type=“module“ 你了解,但 type=“importmap“ 你知道吗
使用GBase 8c数据库的时候,遇到这种报错“[[email protected] ~]$ /home/gbase/script/gha_ctl install -p……
cnpm安装步骤
月薪没到35K的程序员必须要背的面试八股,我先啃为敬!
C# Color颜色RGB对照表、颜色选择器
redis实现分布式锁的原理
Promise 1: Basic Questions
Go的Gin框架学习