在今天的文章中,我們來詳細地描述如果使用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.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 數據庫做重啟及做查詢,會生產對應的慢日誌及錯誤日誌。
部分慢查詢日誌
部分error日誌
進入Kibana dashboard “[Filebeat MySQL] Overview ECS”,觀察採集到的數據,至此,我們可以看到所有的關於MySQL的信息,這裡包括以下queries及error logs等。
總結
如本教程所示,Filebeat 是用於 MySQL 數據庫和 Elasticsearch 集群的出色日誌傳送解決方案。 與以前的版本相比,它非常輕巧,可以有效地發送日誌事件。 Filebeat支持壓縮,並且可以通過單個 yaml 文件輕鬆配置。 使用Filebeat,您可以輕鬆地管理日誌文件,跟蹤日誌註冊表,創建自定義字 K 段以在日誌中啟用細化過濾和發現,以及使用 Kibana 可視化功能立即為日誌數據供電。
聲明:本文由“Beats:如何使用Filebeat將MySQL日誌發送到Elasticsearch”基於阿里雲服務環境授權改編
原文作者:Elastic 中國社區佈道師——劉曉國
合作編輯:Lettie/大禹
出處鏈接:https://elasticstack.blog.csdn.net/.
【阿里雲Elastic Stack】100%兼容開源ES,獨有9大能力,提供免費X-pack服務(單節點價值$6000)
相關活動
更多折扣活動,請訪問阿里雲 Elasticsearch 官網
阿里雲 Elasticsearch 商業通用版,1核2G ,SSD 20G首月免費
阿里雲 Logstash 2核4G首月免費