Prometheus和Grafana的监控Linux服务器和MYSQL数据库

【使用root用户执行以下操作】

 

目录

一、安装Prometheus和Grafana

1、安装 Prometheus

1)下载 Prometheus

2)解压安装包

3)启动prometheus

4)我们访问Prometheus :http://100.100.100.25:9090

2、安装Grafana

1)下载 grafana

二、监控Linux服务器

1、安装Node Exporter

1)下载node_exporter

2)将安装包上传到linux 

3)启动node_exporter

 4)监听端口为9100,我们访问 http://100.100.100.25:9100

5)配置Prometheus,将node_exporter加入到监控中

6)在grafana中的配置 (http://100.100.100.25:3000)

7)导入Prometheus监控Linux模板

8)配置prometheus监控linux服务器完成

三、监控MYSQL数据库(Linux版本)

1)下载mysqld_exporter

2)将下载的mysqld_exporter上传到mysql所在的linux服务器

3)在root目录下建立文件 " .my.cnf " ,输入mysql的访问参数

4)启动mysqld_exporter,指定监听端口为9200

5)我们访问 mysqld_exporter :  http://100.100.100.25:9200

6)修改prometheus配置,在prometheus.yml加入mysql的监听地址

7)重启Prometheus

8)访问grafana(http://100.100.100.25:3000),将mysql数据源添加

9)添加mysql仪表模板

10)将my2.sql导入到MYSQL数据库(100.100.100.25:3306/pig),该监控就是利用my2.sql中的数据进行展示的

11)打开MYSQL监控

四、监控MYSQL数据库(Windows版本)


一、安装Prometheus和Grafana

1、安装 Prometheus

  • Prometheus(中文名:普罗米修斯)是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB). Prometheus使用Go语言开发, 是Google BorgMon监控系统的开源版本。
  • Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态, 任意组件只要提供对应的HTTP接口就可以接入监控. 不需要任何SDK或者其他的集成过程。输出被监控组件信息的HTTP接口被叫做exporter,目前开发常用的组件大部分都有exporter可以直接使用, 比如Nginx、MySQL、Linux系统信息、Mongo、ES等

1)下载 Prometheus

  • github链接: https://github.com/prometheus/prometheus/releases/
  • 选择最新的版本进行下载:prometheus-2.15.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第1张图片

  • 将下载的安装包上传到linux服务器

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第2张图片

2)解压安装包

tar -zxvf prometheus-2.15.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第3张图片

3)启动prometheus

./prometheus

 [如果需要挂后台在命令后面加 & 符号] 

 

  • 监听端口是9090

4)我们访问Prometheus :http://100.100.100.25:9090

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第4张图片

  • prometheus安装结束

 

2、安装Grafana

  • Grafana是一个图形化工具, 它可以从很多种数据源(例如Prometheus)中读取数据信息, 使用很漂亮的图表来展示数据, 并且有很多开源的dashborad可以使用,可以快速地搭建起一个非常精美的监控平台。它与Prometheus的关系就类似于Kibana与ElasticSearch。

1)下载 grafana

  • grafana下载网址:https://grafana.com/grafana/download?platform=linux
  • 选择二进制安装包 

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第5张图片

wget https://dl.grafana.com/oss/release/grafana-6.5.2.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第6张图片

  • 解压grafana
tar -zxvf grafana-6.5.2.linux-amd64.tar.gz

 

  • 在bin目录下启动
./grafana-server

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第7张图片

  • 监听端口为 3000

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第8张图片

  • 我们访问 http://100.100.100.25:3000 ,默认的登录用户为admin/admin,后需要修改密码

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第9张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第10张图片

  • grafana安装完成

[准备工作已经完成,下面我们来安装监控Linux服务器的软件和监控mysql数据库的软件]

二、监控Linux服务器

1、安装Node Exporter

  • prometheus可以理解为一个数据库 数据抓取工具, 工具从各处抓来统一的数据, 放入prometheus这一个时间序列数据库中. 那如何保证各处的数据格式是统一的呢?就是通过这个exporter. Exporter是一类数据采集组件的总称. Exporter负责从目标处搜集数据, 并将其转化为Prometheus支持的格式, 它开放了一个http接口(以便Prometheus来抓取数据). 与传统的数据采集组件不同的是, Exporter并不向中央服务器发送数据, 而是等待中央服务器(如Prometheus等)主动前来抓取。https://github.com/prometheus 有很多写好的exporter,可以直接下载使用。

1)下载node_exporter

  • 从github上进行下载:https://github.com/prometheus/node_exporter/releases
  • 选择最新版本下载: node_exporter-0.18.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第11张图片

2)将安装包上传到linux 

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第12张图片

  • 解压安装包
tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第13张图片

3)启动node_exporter

./node_exporter

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第14张图片

 4)监听端口为9100,我们访问 http://100.100.100.25:9100

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第15张图片

5)配置Prometheus,将node_exporter加入到监控中

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第16张图片

  • 编辑Prometheus配置文件
vim prometheus.yml
  • 加入node_exporter监控配置

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第17张图片

  • 修改完成后启动Prometheus
./prometheus

6)在grafana中的配置 (http://100.100.100.25:3000)

  • 添加Prometheus数据源

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第18张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第19张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第20张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第21张图片

  • 添加prometheus数据源完成

7)导入Prometheus监控Linux模板

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第22张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第23张图片

  • 从grafana下载Prometheus模板: https://grafana.com/grafana/dashboards?dataSource=prometheus

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第24张图片

  • 下载sjon格式的模板

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第25张图片

  • 回过头来将下载的json上传到grafana

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第26张图片

  • 选择Prometheus数据源

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第27张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第28张图片

8)配置prometheus监控linux服务器完成

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第29张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第30张图片

 

三、监控MYSQL数据库(Linux版本)

1)下载mysqld_exporter

  • 从github上下载:https://github.com/prometheus/mysqld_exporter/releases
  • 选择最新的linux版本:   mysqld_exporter-0.12.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第31张图片

2)将下载的mysqld_exporter上传到mysql所在的linux服务器

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第32张图片

  • 解压
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第33张图片

3)在root目录下建立文件 " .my.cnf " ,输入mysql的访问参数

vi /root/.my.cnf

[文件内容如下]

[client]
host = 100.100.100.25
user = root
password = password

4)启动mysqld_exporter,指定监听端口为9200

./mysqld_exporter --web.listen-address=":9200" &

5)我们访问 mysqld_exporter :  http://100.100.100.25:9200

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第34张图片

6)修改prometheus配置,在prometheus.yml加入mysql的监听地址

vim prometheus.yml

[增加的内容]

- job_name: '100.100.100.25_mysql'
    static_configs:
    - targets: [100.100.100.25:9200]

 

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第35张图片

7)重启Prometheus

./prometheus

8)访问grafana(http://100.100.100.25:3000),将mysql数据源添加

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第36张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第37张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第38张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第39张图片

  • 填写完mysql数据源的信息后,点击左下角的Save & Test 即可

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第40张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第41张图片

9)添加mysql仪表模板

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第42张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第43张图片

  • 从grafana官网下载 : https://grafana.com/grafana/dashboards?dataSource=mysql

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第44张图片

  • 下载JSON格式的模板

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第45张图片

  • 回过头来,将下载的模板上传到grafana中

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第46张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第47张图片

10)将my2.sql导入到MYSQL数据库(100.100.100.25:3306/pig),该监控就是利用my2.sql中的数据进行展示的

  • 从github上下载:https://github.com/john1337/my2Collector/blob/master/my2.sql
  • 该文件的内容如下:
-- by [email protected]
-- My2 Collector
-- 0.0.1  2013-02-14 First version for MySQL 5.6
-- 0.0.6  2017-04-01 DBCPU as SUM_TIMER_WAIT from events_statements_summary_global_by_event_name
-- 0.0.7  2017-11-01 bug fixed (0 as first value for delta), MariaDB 10.2 support, new custom statistics
-- 0.0.7a 2018-02-18 substr(EVENT_NAME,15) --> substr(EVENT_NAME,15,60)
-- 0.0.8  2018-04-01 MySQL v.8.0 support
-- 0.0.9a 2018-08-15 Delta statistics (useful for Grafana and others), (a) got some useful global_variable
-- 0.0.10 2018-10-31 Replication Lag (also with multi-threaded slaves)

-- Create Database, Tables, Stored Routines and Jobs for My2 dashboard
create database IF NOT EXISTS my2;
use my2;
CREATE TABLE IF NOT EXISTS status (
  VARIABLE_NAME varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '',
  VARIABLE_VALUE varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
  TIMEST timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS current (
  VARIABLE_NAME varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '',
  VARIABLE_VALUE varchar(1024) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB;

ALTER TABLE status
 ADD unique KEY idx01 (VARIABLE_NAME,timest);
-- delete from my2.status where VARIABLE_NAME like 'PROCESSES_HOSTS.%';
-- update my2.status set variable_value=0, timest=timest where VARIABLE_NAME like '%-d' and variable_value<0;
ALTER TABLE current
 ADD unique KEY idx02 (VARIABLE_NAME);

DROP PROCEDURE IF EXISTS collect_stats;
DELIMITER // ;
CREATE PROCEDURE collect_stats()
BEGIN
DECLARE a datetime;
DECLARE v varchar(10);
set sql_log_bin = 0;
set a=now();
select substr(version(),1,3) into v;

if v='5.7' OR v='8.0' then
  insert into my2.status(variable_name,variable_value,timest) 
   select upper(variable_name),variable_value, a
     from performance_schema.global_status
    where variable_value REGEXP '^-*[[:digit:]]+(\.[[:digit:]]+)?$'
      and variable_name not like 'Performance_schema_%'
      and variable_name not like 'SSL_%';
  insert into my2.status(variable_name,variable_value,timest) 
   SELECT 'replication_worker_time', coalesce(max(PROCESSLIST_TIME), 0.1), a
     FROM performance_schema.threads
    WHERE (NAME = 'thread/sql/slave_worker'
            AND (PROCESSLIST_STATE IS NULL
                  OR PROCESSLIST_STATE != 'Waiting for an event from Coordinator'))
       OR NAME = 'thread/sql/slave_sql';
--  *** Comment the following 4 lines with 8.0  ***
 else
  insert into my2.status(variable_name,variable_value,timest) 
   select variable_name,variable_value,a
     from information_schema.global_status;
end if;
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES.',user),count(*),a
   from information_schema.processlist
  group by user;
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES_HOSTS.',SUBSTRING_INDEX(host,':',1)),count(*),a
   from information_schema.processlist
  group by concat('PROCESSES_HOSTS.',SUBSTRING_INDEX(host,':',1));
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES_COMMAND.',command),count(*),a
   from information_schema.processlist
  group by concat('PROCESSES_COMMAND.',command);
insert into my2.status(variable_name,variable_value,timest) 
 select substr(concat('PROCESSES_STATE.',state),1,64),count(*),a
   from information_schema.processlist
  group by substr(concat('PROCESSES_STATE.',state),1,64);
if v='5.6' OR v='5.7' OR v='8.0' OR v='10.' then
  insert into my2.status(variable_name,variable_value,timest) 
   SELECT 'SUM_TIMER_WAIT', sum(sum_timer_wait*1.0), a
     FROM performance_schema.events_statements_summary_global_by_event_name;
end if;

-- Delta values
if v='5.7' OR v='8.0' then
  insert into my2.status(variable_name,variable_value,timest) 
   select concat(upper(s.variable_name),'-d'), greatest(s.variable_value-c.variable_value,0), a
     from performance_schema.global_status s, my2.current c
    where s.variable_name=c.variable_name;
  insert into my2.status(variable_name,variable_value,timest) 
   SELECT concat('COM_',upper(substr(s.EVENT_NAME,15,58)), '-d'), greatest(s.COUNT_STAR-c.variable_value,0), a
     FROM performance_schema.events_statements_summary_global_by_event_name s, my2.current c
    WHERE s.EVENT_NAME LIKE 'statement/sql/%'
      AND s.EVENT_NAME = c.variable_name;
  insert into my2.status(variable_name,variable_value,timest)
   SELECT 'SUM_TIMER_WAIT-d', sum(sum_timer_wait*1.0)-c.variable_value, a
     FROM performance_schema.events_statements_summary_global_by_event_name, my2.current c
    WHERE c.variable_name='SUM_TIMER_WAIT';
  insert into my2.status(variable_name, variable_value, timest) 
   select 'replication_connection_status',if(SERVICE_STATE='ON', 1, 0),a
     from performance_schema.replication_connection_status;
  insert into my2.status(variable_name, variable_value, timest) 
   select 'replication_applier_status',if(SERVICE_STATE='ON', 1, 0),a
     from performance_schema.replication_applier_status;
  delete from my2.current;
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value+0
     from performance_schema.global_status
    where variable_value REGEXP '^-*[[:digit:]]+(\.[[:digit:]]+)?$'
      and variable_name not like 'Performance_schema_%'
      and variable_name not like 'SSL_%';
  insert into my2.current(variable_name,variable_value) 
   SELECT substr(EVENT_NAME,1,40), COUNT_STAR
     FROM performance_schema.events_statements_summary_global_by_event_name
    WHERE EVENT_NAME LIKE 'statement/sql/%';
  insert into my2.current(variable_name,variable_value) 
   SELECT 'SUM_TIMER_WAIT', sum(sum_timer_wait*1.0)
     FROM performance_schema.events_statements_summary_global_by_event_name;

  insert into my2.current(variable_name,variable_value) 
   select concat('PROCESSES_COMMAND.',command),count(*)
     from information_schema.processlist
    group by concat('PROCESSES_COMMAND.',command);
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value
     from performance_schema.global_variables
    where variable_name in ('max_connections', 'innodb_buffer_pool_size', 'query_cache_size', 
                            'innodb_log_buffer_size', 'key_buffer_size', 'table_open_cache');
 else
  insert into my2.status(variable_name,variable_value,timest) 
   select concat(upper(s.variable_name),'-d'), greatest(s.variable_value-c.variable_value,0), a
     from information_schema.global_status s, my2.current c
    where s.variable_name=c.variable_name;
  delete from my2.current;
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value+0
     from information_schema.global_status
    where variable_value REGEXP '^-*[[:digit:]]+(\.[[:digit:]]+)?$'
      and variable_name not like 'Performance_schema_%'
      and variable_name not like 'SSL_%';
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value
     from information_schema.global_variables
    where variable_name in ('max_connections', 'innodb_buffer_pool_size', 'query_cache_size', 
                            'innodb_log_buffer_size', 'key_buffer_size', 'table_open_cache');
end if;

set sql_log_bin = 1;
END //
DELIMITER ; //

-- Collect daily statistics on space usage and delete old statistics (older than 62 days, 1 year for DB size)
DROP PROCEDURE IF EXISTS collect_daily_stats;
DELIMITER // ;
CREATE PROCEDURE collect_daily_stats()
BEGIN
DECLARE a datetime;
set sql_log_bin = 0;
set a=now();
insert into my2.status(variable_name,variable_value,timest)
 select concat('SIZEDB.',table_schema), sum(data_length+index_length), a
   from information_schema.tables group by table_schema;
insert into my2.status(variable_name,variable_value,timest) 
 select 'SIZEDB.TOTAL', sum(data_length+index_length), a
   from information_schema.tables;
delete from my2.status where timest < date_sub(now(), INTERVAL 62 DAY) and variable_name <>'SIZEDB.TOTAL';
delete from my2.status where timest < date_sub(now(), INTERVAL 365 DAY);
set sql_log_bin = 1;
END //
DELIMITER ; //

-- The event scheduler must also be activated in the my.cnf (event_scheduler=1)
set global event_scheduler=1;

set sql_log_bin = 0;
DROP EVENT IF EXISTS collect_stats;
CREATE EVENT collect_stats
    ON SCHEDULE EVERY 10 Minute
    DO call collect_stats();
DROP EVENT IF EXISTS collect_daily_stats;
CREATE EVENT collect_daily_stats
    ON SCHEDULE EVERY 1 DAY
    DO call collect_daily_stats();
set sql_log_bin = 1;

-- Use a specific user (suggested)
-- create user my2@'%' identified by 'P1e@seCh@ngeMe';
-- grant all on my2.* to my2@'%';
  • 粘贴到mysql中执行即可

11)打开MYSQL监控

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第48张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第49张图片

【注意:这个仪表盘的数据要过一会才会有】

四、监控MYSQL数据库(Windows版本)

1)下载mysql_exporter:

  • 在github上进行下载:https://github.com/prometheus/mysqld_exporter/releases

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第50张图片

  • 下载  mysqld_exporter-0.12.1.windows-amd64.tar.gz

2)解压 mysqld_exporter,在目录下创建 ".my.cnf" 文件

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第51张图片

  • ./mycnf 文件内容如下(包含mysql的连接信息:host、user、password)
[client]
host = localhost
user = root
password = password
  • 在mysql库(100.100.100.98:3306/na)中执行 数据采集sql: my2_80.sql
-- by [email protected]
-- My2_80 Collector
-- 0.0.1  2013-02-14 First version for MySQL 5.6
-- 0.0.6  2017-04-01 DBCPU as SUM_TIMER_WAIT from events_statements_summary_global_by_event_name
-- 0.0.7  2017-11-01 bug fixed (0 as first value for delta), MariaDB 10.2 support, new custom statistics
-- 0.0.7a 2018-02-18 substr(EVENT_NAME,15) --> substr(EVENT_NAME,15,60)
-- 0.0.8  2018-04-01 MySQL v.8.0 support
-- 0.0.9a 2018-08-15 Delta statistics (useful for Grafana and others), (a) got some useful global_variable
-- 0.0.10 2018-10-31 Replication Lag (also with multi-threaded slaves)
-- 0.0.11 2019-05-05 MySQL v.8 only

-- Create Database, Tables, Stored Routines and Jobs for My2 dashboard
create database IF NOT EXISTS my2;
use my2;
CREATE TABLE IF NOT EXISTS status (
  VARIABLE_NAME varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '',
  VARIABLE_VALUE varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
  TIMEST timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS current (
  VARIABLE_NAME varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '',
  VARIABLE_VALUE varchar(1024) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB;

ALTER TABLE status
 ADD unique KEY idx01 (VARIABLE_NAME,timest);
-- delete from my2.status where VARIABLE_NAME like 'PROCESSES_HOSTS.%';
-- update my2.status set variable_value=0, timest=timest where VARIABLE_NAME like '%-d' and variable_value<0;
ALTER TABLE current
 ADD unique KEY idx02 (VARIABLE_NAME);

DROP PROCEDURE IF EXISTS collect_stats;
DELIMITER // ;
CREATE PROCEDURE collect_stats()
BEGIN
DECLARE a datetime;
DECLARE v varchar(10);
set sql_log_bin = 0;
set a=now();
select substr(version(),1,3) into v;

insert into my2.status(variable_name,variable_value,timest) 
   select upper(variable_name),variable_value, a
     from performance_schema.global_status
    where variable_value REGEXP '^-*[[:digit:]]+(\.[[:digit:]]+)?$'
      and variable_name not like 'Performance_schema_%'
      and variable_name not like 'SSL_%';
insert into my2.status(variable_name,variable_value,timest) 
   SELECT 'REPLICATION_MAX_WORKER_TIME', coalesce(max(PROCESSLIST_TIME), 0.1), a
     FROM performance_schema.threads
    WHERE (NAME = 'thread/sql/slave_worker'
            AND (PROCESSLIST_STATE IS NULL
                  OR PROCESSLIST_STATE != 'Waiting for an event from Coordinator'))
       OR NAME = 'thread/sql/slave_sql';
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES.',user),count(*),a
   from information_schema.processlist
  group by user;
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES_HOSTS.',SUBSTRING_INDEX(host,':',1)),count(*),a
   from information_schema.processlist
  group by concat('PROCESSES_HOSTS.',SUBSTRING_INDEX(host,':',1));
insert into my2.status(variable_name,variable_value,timest) 
 select concat('PROCESSES_COMMAND.',command),count(*),a
   from information_schema.processlist
  group by concat('PROCESSES_COMMAND.',command);
insert into my2.status(variable_name,variable_value,timest) 
 select substr(concat('PROCESSES_STATE.',state),1,64),count(*),a
   from information_schema.processlist
  group by substr(concat('PROCESSES_STATE.',state),1,64);
insert into my2.status(variable_name,variable_value,timest) 
   SELECT 'SUM_TIMER_WAIT', sum(sum_timer_wait*1.0), a
     FROM performance_schema.events_statements_summary_global_by_event_name;

-- Delta values
  insert into my2.status(variable_name,variable_value,timest) 
   select concat(upper(s.variable_name),'-d'), greatest(s.variable_value-c.variable_value,0), a
     from performance_schema.global_status s, my2.current c
    where s.variable_name=c.variable_name;
  insert into my2.status(variable_name,variable_value,timest) 
   SELECT concat('COM_',upper(substr(s.EVENT_NAME,15,58)), '-d'), greatest(s.COUNT_STAR-c.variable_value,0), a
     FROM performance_schema.events_statements_summary_global_by_event_name s, my2.current c
    WHERE s.EVENT_NAME LIKE 'statement/sql/%'
      AND s.EVENT_NAME = c.variable_name;
  insert into my2.status(variable_name,variable_value,timest)
   SELECT 'SUM_TIMER_WAIT-d', sum(sum_timer_wait*1.0)-c.variable_value, a
     FROM performance_schema.events_statements_summary_global_by_event_name, my2.current c
    WHERE c.variable_name='SUM_TIMER_WAIT';
  insert into my2.status(variable_name, variable_value, timest) 
   select 'REPLICATION_CONNECTION_STATUS',if(SERVICE_STATE='ON', 1, 0),a
     from performance_schema.replication_connection_status;
  insert into my2.status(variable_name, variable_value, timest) 
   select 'REPLICATION_APPLIER_STATUS',if(SERVICE_STATE='ON', 1, 0),a
     from performance_schema.replication_applier_status;
  delete from my2.current;
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value+0
     from performance_schema.global_status
    where variable_value REGEXP '^-*[[:digit:]]+(\.[[:digit:]]+)?$'
      and variable_name not like 'Performance_schema_%'
      and variable_name not like 'SSL_%';
  insert into my2.current(variable_name,variable_value) 
   SELECT substr(EVENT_NAME,1,40), COUNT_STAR
     FROM performance_schema.events_statements_summary_global_by_event_name
    WHERE EVENT_NAME LIKE 'statement/sql/%';
  insert into my2.current(variable_name,variable_value) 
   SELECT 'SUM_TIMER_WAIT', sum(sum_timer_wait*1.0)
     FROM performance_schema.events_statements_summary_global_by_event_name;

  insert into my2.current(variable_name,variable_value) 
   select concat('PROCESSES_COMMAND.',command),count(*)
     from information_schema.processlist
    group by concat('PROCESSES_COMMAND.',command);
  insert into my2.current(variable_name,variable_value) 
   select upper(variable_name),variable_value
     from performance_schema.global_variables
    where variable_name in ('max_connections', 'innodb_buffer_pool_size', 'query_cache_size', 
                            'innodb_log_buffer_size', 'key_buffer_size', 'table_open_cache');
set sql_log_bin = 1;
END //
DELIMITER ; //

-- Collect daily statistics on space usage and delete old statistics (older than 62 days, 1 year for DB size)
DROP PROCEDURE IF EXISTS collect_daily_stats;
DELIMITER // ;
CREATE PROCEDURE collect_daily_stats()
BEGIN
DECLARE a datetime;
set sql_log_bin = 0;
set a=now();
insert into my2.status(variable_name,variable_value,timest)
 select concat('SIZEDB.',table_schema), sum(data_length+index_length), a
   from information_schema.tables group by table_schema;
insert into my2.status(variable_name,variable_value,timest) 
 select 'SIZEDB.TOTAL', sum(data_length+index_length), a
   from information_schema.tables;
delete from my2.status where timest < date_sub(now(), INTERVAL 62 DAY) and variable_name <>'SIZEDB.TOTAL';
delete from my2.status where timest < date_sub(now(), INTERVAL 365 DAY);
set sql_log_bin = 1;
END //
DELIMITER ; //

-- The event scheduler must also be activated in the my.cnf (event_scheduler=1)
set global event_scheduler=1;

set sql_log_bin = 0;
DROP EVENT IF EXISTS collect_stats;
CREATE EVENT collect_stats
    ON SCHEDULE EVERY 10 Minute
    DO call collect_stats();
DROP EVENT IF EXISTS collect_daily_stats;
CREATE EVENT collect_daily_stats
    ON SCHEDULE EVERY 1 DAY
    DO call collect_daily_stats();
set sql_log_bin = 1;

-- Use a specific user (suggested)
-- create user my2@'%' identified by 'P1e@seCh@ngeMe';
-- grant all on my2.* to my2@'%';

3)cmd启动 mysqld_exporter

mysqld_exporter.exe --web.listen-address=":9104" --config.my-cnf=.my.cnf
  • 访问9104端口

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第52张图片

4)在Prometheus配置中增加对该mysql的监听

vim prometheus.yml

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第53张图片

  • 启动Prometheus
./prometheus

5)在grafana上(http://100.100.100.25:3000/)增加该数据源

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第54张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第55张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第56张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第57张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第58张图片

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第59张图片

6)选择MYSQL监控仪表盘

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第60张图片

  • 选择刚刚添加的MYSQL数据源

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第61张图片

【访问Prometheus ,http://100.100.100.25:9090 可以看到在配置中添加的监听服务器】

Prometheus和Grafana的监控Linux服务器和MYSQL数据库_第62张图片

你可能感兴趣的:(Prometheus,Grafana,node_exporter)