Elasticsearch:4.ELK监控 MySQL 慢查询日志

环境

第一步:安装 Filebeat 并打开 MySQL 慢查询日志

1.打开 MySQL 慢查询日志

慢查询日志默认路径:/var/lib/mysql/master118-slow.log
#################### 相关命令 ####################
## 查看是否打开慢查询日志
show variables like '%slow_query_log%';

## 打开慢查询日志(当前生效)
set global slow_query_log=1;

## 大于多少秒的记录日志(而非大于等于)
show variables like 'long_query_time';

## 设置long_query_time阈值(当前生效)(需要重新连接mysql才能看到修改值)
set global long_query_time=4;

## 查看慢查询日志记录方式
show variables like '%log_output%';

## 查询未使用索引的查询是否记录
show variables like 'log_queries_not_using_indexes';

## 未使用索引的查询也记录日志(建议打开)
set global log_queries_not_using_indexes=1;

## 是否将慢管理语句例如ANALYZE TABLE和ALTER TABLE等记入慢查询日志
show variables like 'log_slow_admin_statements';

## 另外,如果你想查询有多少条慢查询记录,可以使用系统变量。
show global status like '%slow_queries%';

#################### 打开慢查询日志(永久生效)####################
vim /etc/my.cnf
# 打开慢日志查询
slow_query_log = 1
# 设置慢查询阈值
long_query_time = 2
# 未使用索引的查询也记录日志(建议打开)
# log_queries_not_using_indexes = 1

systemctl stop mysqld
systemctl start mysqld
systemctl status mysqld

2.安装并设置 Filebeat

1.安装

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.6.0-x86_64.rpm
rpm -ivh filebeat-6.6.0-x86_64.rpm

2.启用模块

filebeat modules enable mysql

    ## 查看模块列表:
    filebeat modules list
    ## 模块文件路径
    /etc/filebeat/modules.d

3.修改配置文件 filebeat.yml

## 备份配置文件
cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak
## 过滤掉 # 开头的注释行和空行,方便修改
cat /etc/filebeat/filebeat.yml.bak | grep -v '^#' | grep -v '^  #' | grep -v '^$' > /etc/filebeat/filebeat.yml
## 编辑配置文件,修改以下内容
vim /etc/filebeat/filebeat.yml

    output.elasticsearch:
      hosts: ["localhost:9200"]
    修改为:
    output.elasticsearch:
      hosts: ["192.168.1.1:9200"]

4.修改配置文件

cp /usr/share/filebeat/module/mysql/slowlog/config/slowlog.yml /usr/share/filebeat/module/mysql/slowlog/config/slowlog.yml.bak
vim /usr/share/filebeat/module/mysql/slowlog/config/slowlog.yml

exclude_lines: ['^[\/\w\.]+, Version: .* started with:.*', '^# Time:']   # Exclude the header
修改为
exclude_lines: ['^[\/\w\.]+, Version: .* started with:.*']   # Exclude the header

5.初始换环境

filebeat setup -e

    -e  指定将将输出发送到标准错误而不是syslog
    该命令加载建议的索引模板以写入 Elasticsearch,并部署 Kibana 可视化仪表板(如果可用)

6.以上命令确认输出没有错误后开启 Filebeat 服务并设置开机自动启动

systemctl stop filebeat
systemctl start filebeat
systemctl start httpd
systemctl status filebeat

附录:

官网介绍:https://www.elastic.co/guide/en/beats/filebeat/6.6/filebeat-module-mysql.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容