天天看點

【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

在今天的文章中,我們來詳細地描述如果使用Filebeat把MySQL的日志資訊傳輸到Elasticsearch中。

環境準備

1、準備

centos7.4版本 ECS 環境

,關閉 selinux、firewall。

2、準備

阿裡雲elasticsearch 6.7 版本環境

,并使用建立的

賬号密碼登入Kibana

3、安裝 Filebeat 版本為 6.7.0

4、安裝 MySQL 版本為 5.6.48

安裝 MySQL

我們需要通過以下指令,來對 MySQL 進行安裝。

# yum install mysql-server
# systemctl start mysqld
# systemctl status mysqld
####通過mysqladmin設定root密碼#####
# mysqladmin -u root password "123456"           

下一步在my.cnf中配置error日志檔案和慢速查詢日志檔案,預設情況下這些配置禁用狀态,需要手動開啟;也可通過mysql指令開啟臨時慢日志。

# vim /etc/my.cnf
[mysqld]
log_queries_not_using_indexes = 1
slow_query_log=on
slow_query_log_file=/var/log/mysql/slow-mysql-query.log
long_query_time=0

[mysqld_safe]
log-error=/var/log/mysql/mysqld.log           
注: MySQL 不會主動建立日志檔案,是以需要手動添加,添加完後賦予所有使用者可讀可寫權限,如chmod 777 slow-mysql-query.log。

配置 Filebeat

在 Centos 上安裝 Filebeat 是非常直接的,可以打開 Elasticsearch - Kibana 控制台,進入概覽頁,選擇“ add log data”

【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch
【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

我們按照上面的要求一步一步地進行安裝和修改。在修改filebeat.yml檔案時,我們需要注意以下:

一、配置 Filebeat modules 動态加載。

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: /etc/filebeat/modules.d/mysql.yml

  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  reload.period: 1s           

由于 MySQL 需要分别檢測錯誤日志及慢日志,通過子產品分别指定所在路徑,動态加載子產品,是以path指定子產品所在路徑。

二、 填入 Kibana 位址。

setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  host: "https://es-cn-0p11111000zvqku.kibana.elasticsearch.aliyuncs.com:5601"           

三、填入 Elasticsearch 位址及端口号:

output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["es-cn-0p11111000zvqku.elasticsearch.aliyuncs.com:9200"]
  # Optional protocol and basic auth credentials.
  #protocol: "https"
  username: "elastic"
  password: "elastic@333"           

四、啟用 MySQL 子產品,并進行配置:

# sudo filebeat modules enable mysql
# vim /etc/filebeat/modules.d/mysql.yml
- module: mysql
  # Error logs
  error:
    enabled: true
    var.paths: ["/var/log/mysql/mysqld.log"]
    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

  # Slow logs
  slowlog:
    enabled: true
    var.paths: ["/var/log/mysql/slow-mysql-query.log"]
    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:           

我們需要運作如下的指令來把相應的 Dashboard,Pipeline 及 Template 資訊上傳到 Elasticsearch 和 Kibana 中。

# sudo filebeat setup
# sudo service filebeat start           

Kibana Dashboard

對 MySQL 資料庫做重新開機及做查詢,會生産對應的慢日志及錯誤日志。

部分慢查詢日志

【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

部分error日志

【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

進入Kibana dashboard “[Filebeat MySQL] Overview ECS”,觀察采集到的資料,至此,我們可以看到所有的關于MySQL的資訊,這裡包括以下queries及error logs等。

【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

總結

如本教程所示,Filebeat 是用于 MySQL 資料庫和 Elasticsearch 叢集的出色日志傳送解決方案。 與以前的版本相比,它非常輕巧,可以有效地發送日志事件。 Filebeat支援壓縮,并且可以通過單個 yaml 檔案輕松配置。 使用Filebeat,您可以輕松地管理日志檔案,跟蹤日志系統資料庫,建立自定義字 K 段以在日志中啟用細化過濾和發現,以及使用 Kibana 可視化功能立即為日志資料供電。

聲明:本文由“Beats:如何使用Filebeat将MySQL日志發送到Elasticsearch”基于阿裡雲服務環境授權改編

原文作者:Elastic 中國社群布道師——劉曉國

合作編輯:Lettie/大禹

出處連結:

https://elasticstack.blog.csdn.net/.
【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch

阿裡雲Elastic Stack

】100%相容開源ES,獨有9大能力,提供免費X-pack服務(單節點價值$6000)

相關活動

更多折扣活動,請

通路阿裡雲 Elasticsearch 官網 阿裡雲 Elasticsearch 商業通用版,1核2G ,SSD 20G首月免費 阿裡雲 Logstash 2核4G首月免費
【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch
【最佳實踐】Filebeat實作MySQL日志輕量化發送至Elasticsearch