【MySQL Database】通过ELK自动收集数据库慢查询日志


ELK
[root@wallet03 ~]# rpm -ivh elasticsearch-7.9.0-x86_64.rpm
[root@wallet03 ~]# vim /etc/elasticsearch/elasticsearch.yml
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.1.203
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

[root@wallet03 ~]# systemctl start elasticsearch.service
[root@wallet03 ~]# systemctl status elasticsearch.service
[root@wallet03 ~]# curl http://192.168.1.203:9200
{
  "name" : "node-1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "LJUum0b2S6ySgnHpAKlOFQ",
  "version" : {
    "number" : "7.9.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
    "build_date" : "2020-08-11T21:36:48.204330Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

[root@wallet03 ~]# rpm -ivh kibana-7.9.0-x86_64.rpm 
[root@wallet03 ~]# vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "192.168.1.203"
elasticsearch.hosts: ["http://192.168.1.203:9200"]

[root@wallet03 ~]# service kibana start
[root@wallet03 ~]# service kibana status
kibana is running
DATABASE
[root@wallet01 ~]# rpm -ivh filebeat-7.9.0-x86_64.rpm 
[root@wallet01 ~]# vim /etc/filebeat/filebeat.yml
# =================================== Kibana ===================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
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: "192.168.1.203:5601"

# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["192.168.1.203:9200"]

[root@wallet01 ~]# filebeat modules enable mysql
[root@wallet01 ~]# filebeat modules list
Enabled:mysql

[root@wallet01 ~]# vim /etc/filebeat/modules.d/mysql.yml 
- module: mysql
  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/lib/mysql/mysqld_error.log"]

  # Slow logs
  slowlog:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/lib/mysql/mysqld_slow.log"]

[root@wallet01 ~]# filebeat setup

[root@wallet01 ~]# service filebeat start
[root@wallet01 ~]# service filebeat status