logstash推送mysql慢查询日志

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

本文将将介绍通过logstash用来收集mysql的慢查询日志,然后推送给elasticsearch,并创建自定义的索引,最终通过kibana进行web展示。

环境介绍:

操作系统版本:centos6.6 64bit

Mysql版本: mysql5.6.17mysql5.1.36

Logstash版本: logstash-2.0.0.tar.gz

Elasticsearch版本:elasticsearch-2.1.0.tar.gz

Kibana版本:Kibana 4.2.1

Java版本:1.8.0_45

 

一:mysql5.1.36版本

1:配置mysql5.1.36版本慢查询日志,这里为了测试,将查询时间超过0.1s的均记录到慢查询日志中

1
2
mysql> show variables like '%slow%';
mysql> show variables like '%long%';

wKiom1Zf4h3hwRsSAABQzxZFlpM360.png

2:配置logstash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# cat /usr/local/logstash/etc/logstach.conf 
input {
   file {
     type => "mysql-slow"
     path => "/mydata/slow-query.log"
     codec => multiline {
       pattern => "^# User@Host:"
       negate => true
       what => "previous"
     }
   }
}
#input节的配置定义了输入的日志类型为mysql慢查询日志类型以及日志路径,采用合并多行数据。negate字段是一个选择开关,可以正向匹配和反向匹配
filter {
   # drop sleep events
   grok {
     match => { "message" => "SELECT SLEEP" }
     add_tag => [ "sleep_drop" ]
     tag_on_failure => [] # prevent default _grokparsefailure tag on real records
   }
   if "sleep_drop" in [tags] {
     drop {}
   }
#filter节的配置定义了过滤mysql查询为sleep状态SQL语句
grok {
match => [ "message", "(?m)^# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?< clienthost >\S*) )?\[(?:%{IP:clientip})?\]\s*# Query_time: %{NUMBER:query_time:float}\s+Lock_time: %{NUMBER:lock_time:float}\s+Rows_sent: %{NUMBER:rows_sent:int}\s+Rows_examined: %{NUMBER:rows_examined:int}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?< query >(?< action >\w+)\s+.*)\n# Time:.*$" ]
}
  
   date {
     match => [ "timestamp", "UNIX" ]
     remove_field => [ "timestamp" ]
   }
}
#grok节定义了慢查询日志输出的正则切割,这个容易头晕眼花!
output {
  stdout {
  codec => rubydebug {}
}
  elasticsearch { 
  hosts => "192.168.1.226:9200" 
  index => "mysql-server81-%{+YYYY.MM.dd}"
}
#output节定义了输出,这里除了打印到屏幕上之外,还输入到elasticsearch,同时起一个自定义的索引名称

3:启动测试

# /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/logstach.conf 

wKioL1Zf4xqB8tebAAFfsjKAAVo381.png

# tail -f /mydata/slow-query.log 

wKiom1Zf4tXw7KpzAABc7oIura8872.png

wKiom1Zf4wHjCSXLAACu9og2lg8186.png

wKiom1Zf4wODVhEYAAHZCwYKraA264.png

wKioL1Zf423jbuOHAACbOVrvnsY108.png

二:mysql5.6.17版本

由于mysql5.6.17版本的的slowlog多了一个id字段,所以需要调整grok节的正则配置。

Mysql5.1.36的slowlog

1
2
3
4
5
6
# tail -f /mydata/slow-query.log 
# Time: 151202 17:29:24
# User@Host: root[root] @  [192.168.1.156]
# Query_time: 6.578696  Lock_time: 0.000039 Rows_sent: 999424  Rows_examined: 999424
SET timestamp=1449048564;
select * from users_test;

Mysql5.6.17的slowlog:对比mysql5.1.36版本的慢查询日志输出,多了Id: 84589

1
2
3
4
5
6
# tail -f /mydata/slow-query.log 
# Time: 151202 16:09:54
# User@Host: root[root] @  [192.168.1.156]  Id: 84589
# Query_time: 7.089324  Lock_time: 0.000112 Rows_sent: 1  Rows_examined: 33554432
SET timestamp=1449043794;
select count(*) from t1;

这里顺便说一下,之前还测试了Percona Server 5.5.34版本,发现慢查询日志多了Thread_idSchemaLast_errnoKilled 4个字段。

1
2
3
4
5
6
7
# tail -f /mydata5.5/slow-query.log 
# User@Host: root[root] @  [192.168.1.228]
# Thread_id: 1164217  Schema: mgr  Last_errno: 0  Killed: 0
# Query_time: 0.371185  Lock_time: 0.000056  Rows_sent: 0  Rows_examined: 0  Rows_affected: 2  Rows_read: 0
# Bytes_sent: 11
SET timestamp=1449105655;
REPLACE INTO  edgemgr_dbcache(id, type, data, expire_time)  VALUES(UNHEX('ec124ee5766c4a31819719c645dab895'), 'sermap', '{\"storages\":{\"sg1-s1\":[{\"download_port\":9083,\"p2p_port\":9035,\"rtmp_port\":9035,\"addr\":\"{\\\"l\\\":{\\\"https://192.168.1.227:9184/storage\\\":\\\"\\\"},\\\"m\\\":{},\\\"i\\\":{\\\"https://192.168.1.227:9184/storage\\\":\\\"\\\"}}\",\"cpu\":6,\"mem\":100,\"bandwidth\":0,\"disk\":0,\"dead\":0}]},\"lives\":{}}', '2016-01-02 09:20:55');

因而5.6.17版本只需要修改logstash.conf配置文件中的grok节内容如下后重启logstash进程即可。

1
2
3
grok {
match => [ "message", "(?m)^# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?< clienthost >\S*) )?\[(?:%{IP:clientip})?\]\s*Id: %{NUMBER:id:int}\s+# Query_time: %{NUMBER:query_time:float}\s+Lock_time: %{NUMBER:lock_time:float}\s+Rows_sent: %{NUMBER:rows_sent:int}\s+Rows_examined: %{NUMBER:rows_examined:int}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?< query >(?< action >\w+)\s+.*)\n# Time:.*$" ]
}

wKiom1Zf45Cyh9OFAACafD6n1Qk068.png

Kibana日志输出

wKioL1Zf5BagM636AACeCxz_-AA033.png

本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/1719214如需转载请自行联系原作者


ylw6006

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
11天前
|
监控 关系型数据库 MySQL
《MySQL 简易速速上手小册》第7章:MySQL监控和日志分析(2024 最新版)
《MySQL 简易速速上手小册》第7章:MySQL监控和日志分析(2024 最新版)
36 3
|
23天前
|
存储 安全 关系型数据库
Mysql 的binlog日志的优缺点
MySQL的binlog(二进制日志)是一个记录数据库更改的日志文件,它包含了所有对数据库执行的更改操作,如INSERT、UPDATE和DELETE等。binlog的主要目的是复制和恢复。以下是binlog日志的优缺点: ### 优点: 1. **数据恢复**:当数据库出现意外故障或数据丢失时,可以利用binlog进行点恢复(point-in-time recovery),将数据恢复到某一特定时间点。 2. **主从复制**:binlog是实现MySQL主从复制功能的核心组件。主服务器将binlog中的事件发送到从服务器,从服务器再重放这些事件,从而实现数据的同步。 3. **审计**:b
|
2月前
|
SQL 关系型数据库 MySQL
MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复
对于MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复。二进制日志是MySQL中记录所有数据库更改操作的日志文件。要进行时间点恢复,您需要执行以下步骤: 1. 确保MySQL配置文件中启用了二进制日志功能。在配置文件(通常是my.cnf或my.ini)中找到以下行,并确保没有被注释掉: Copy code log_bin = /path/to/binary/log/file 2. 在需要进行恢复的时间点之前创建一个数据库备份。这将作为恢复的基准。 3. 找到您要恢复到的时间点的二进制日志文件和位置。可以通过执行以下命令来查看当前的二进制日志文件和位
102 1
|
27天前
|
SQL 监控 关系型数据库
深入理解MySQL日志:通用查询、慢查询和错误日志详解
深入理解MySQL日志:通用查询、慢查询和错误日志详解
|
27天前
|
SQL 存储 关系型数据库
轻松入门MySQL:深入理解MySQL日志,二进制日志、中继日志、回滚日志和重做日志(19)
轻松入门MySQL:深入理解MySQL日志,二进制日志、中继日志、回滚日志和重做日志(19)
|
21天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
87 1
|
21天前
|
SQL 存储 关系型数据库
Mysql主从同步 清理二进制日志的技巧
Mysql主从同步 清理二进制日志的技巧
9 1
|
22天前
|
SQL 存储 关系型数据库
mysql bin-log日志导出
mysql bin-log日志导出
|
2月前
|
存储 SQL 关系型数据库
[MySQL]事务原理之redo log,undo log
[MySQL]事务原理之redo log,undo log
|
2月前
|
SQL 缓存 关系型数据库
MySQL的万字总结(缓存,索引,Explain,事务,redo日志等)
MySQL的万字总结(缓存,索引,Explain,事务,redo日志等)
68 0