教你快速实现“基于Docker快速构建基于Prometheus的MySQL监控系统”

2023-03-09 数据库云数据库 SQL Server容器Docker命令行工具

这次都给他拿下 基于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/prometheusvi /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

第 3 步:登陆账号密码都是admin

第 3 步 :指定数据源

7362是针对mysql监控的。

第 3 步:查看结果

总结

通过prometheus与grafana,成功监控mysql的运行状态,像是锁状态等一些指标都能够可视化出来。 written by 知识浅谈

上一篇:Python 提取图片中的GPS信息

下一篇:GTA5 如何替换各种人物模型