🍁 作者:知识浅谈,阿里云社区技术博主,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 @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@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 remote@'%'IDENTIFIED with mysql_native_password BY 'remote'; grant all privileges on *.* to remote@'%'; # 创建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:exporter@(127.0.0.1:3306)/sakila" prom/mysqld-exporter
注意上边的命令需要制定数据源:-e DATA_SOURCE_NAME="exporter:exporter@(你的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的运行状态,像是锁状态等一些指标都能够可视化出来。