Collect MySQL logs with self-managed Filebeat

Updated at:

You can use Filebeat to collect MySQL logs, such as the slow query log and the error log, and send them to an Alibaba Cloud Elasticsearch instance for querying, analysis, and visualization in Kibana.

Procedure

  1. Prerequisites

    Create an Alibaba Cloud Elasticsearch instance to receive logs and a Kibana console for analysis, and an ECS instance to host MySQL and Filebeat.

  2. Step 1: Install and configure MySQL

    Install MySQL and enable the error log and slow query log in the MySQL configuration file. Filebeat can collect these logs only after they are enabled.

  3. Step 2: Install and configure Filebeat

    Install Filebeat to collect MySQL logs and send them to an Alibaba Cloud Elasticsearch instance. Enable the MySQL module and specify the Kibana and Alibaba Cloud Elasticsearch access addresses in the Filebeat configuration file.

  4. Step 3: Visualize MySQL logs in Kibana

    Run a test query and view the error log and slow query log on a Kibana dashboard.

Prerequisites

Step 1: Install and configure MySQL

  1. Connect to the ECS instance.

    For more information, see Connect to a Linux instance by using a password.

    Note

    The commands in this guide are run as a non-root user.

  2. Download and install the MySQL repository.

    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
  3. Install MySQL.

    sudo yum install mysql-server
  4. Configure the error log file and slow query log file in the my.cnf file.

    Note

    By default, MySQL log files are disabled. You must enable them manually. You can also run a MySQL command to temporarily enable the slow query log.

    1. Open the my.cnf file.

      sudo vim /etc/my.cnf
    2. Configure the log files.

      [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

      Parameter

      Description

      log_queries_not_using_indexes

      Specifies whether to record queries that do not use indexes to the slow query log. 1 enables recording, and 0 disables it.

      slow_query_log

      Enables or disables the slow query log. on enables the log, and off disables it.

      slow_query_log_file

      The storage path of the slow query log.

      long_query_time

      The slow query threshold in seconds. MySQL logs queries that exceed this threshold to the file specified by slow_query_log_file.

      Important

      For testing purposes, this parameter is set to 0 in this topic. In a production environment, set the value based on your business requirements.

    3. (Optional) Create the log files.

      Important

      MySQL 5.6 does not create log files automatically. You must create them manually.

      sudo mkdir /var/log/mysql
      sudo touch /var/log/mysql/mysqld.log
      sudo touch /var/log/mysql/slow-mysql-query.log
    4. Grant all users read and write permissions on the log files.

      sudo chmod 777 /var/log/mysql/slow-mysql-query.log /var/log/mysql/mysqld.log
  5. Start MySQL and check its status.

    sudo systemctl start mysqld
    sudo systemctl status mysqld

Step 2: Install and configure Filebeat

  1. Log on to the Kibana console of your Alibaba Cloud Elasticsearch instance.

    For more information, see Log on to the Kibana console.

  2. In the left-side navigation pane, click Logs.

  3. Click View setup instructions.

  4. On the Add Data to Kibana page, click MySQL logs.

  5. On the Self managed page, click RPM.

    Note

    This guide uses a Linux-based instance, so RPM is selected. Select the installation method suitable for your operating system.

  6. Follow the on-screen instructions to install Filebeat on your ECS instance.

  7. Modify the MySQL module configuration to specify the paths for the error and slow query log files.

    1. Enable the MySQL module.

      sudo filebeat modules enable mysql
    2. Open the mysql.yml file.

      sudo vim /etc/filebeat/modules.d/mysql.yml
    3. Modify the MySQL module configuration.

      - 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:

      Parameter

      Description

      enabled

      Set the value to true.

      var.paths

      The path of the log file. This path must match the path specified in the MySQL configuration file. For more information, see Step 1: Install and configure MySQL.

  8. Configure the filebeat.yml file.

    1. Open the filebeat.yml file.

      sudo vim /etc/filebeat/filebeat.yml
    2. Modify the Filebeat modules configuration.

      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
    3. Modify the Kibana configuration.

      #================================ 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: "https://es-cn-xxx.kibana.elasticsearch.aliyuncs.com:5601"

      host: The access address of the Kibana console, which you can obtain from the Kibana configuration page. For more information, see View the public endpoint of the Kibana console. The format is <Kibana public endpoint>:5601.

    4. Modify the Elasticsearch output configuration.

      output.elasticsearch:
        # Array of hosts to connect to.
        hosts: ["es-cn-0pp1jxvcl000*****.elasticsearch.aliyuncs.com:9200"]
        # Optional protocol and basic auth credentials.
        #protocol: "https"
        username: "elastic"
        password: "<your_password>"

      Parameter

      Description

      hosts

      The access address of your Alibaba Cloud Elasticsearch instance in the format <Instance private IP address or public IP address>:9200. You can find the address on the instance's Basic Information page. For more information, see View the basic information of an instance.

      Note

      Use the private IP address if your ECS and Alibaba Cloud Elasticsearch instances are in the same Virtual Private Cloud (VPC). Otherwise, use the public IP address and configure an IP whitelist. For more information, see Configure a public or private IP whitelist for an instance.

      username

      The username for accessing the Alibaba Cloud Elasticsearch instance. Default value: elastic.

      password

      The password for the specified username, which is set when you create the instance. If you forget it, you can reset it. For more information, see Reset the access password of an instance.

  9. Run the following commands to upload dashboards and other assets to Elasticsearch and Kibana, and start the Filebeat service.

    sudo filebeat setup
    sudo service filebeat start

Step 3: Visualize MySQL logs in Kibana

  1. On the ECS instance, restart MySQL and run a test query.

    Run the following command to restart MySQL:

    sudo systemctl restart mysqld
  2. View the log content.

    The following examples show sample log entries.

    
    [root@zl-test003 ~]# tail -50 /var/log/mysql/slow-mysql-query.log
    # Query_time: 0.000385  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
    SET timestamp=1590720469;
    ;
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000138  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
    SET timestamp=1590720469;
    ;
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000214  Lock_time: 0.000048 Rows_sent: 5  Rows_examined: 5
    SET timestamp=1590720469;
    SELECT * from student;
    # Time: 200529 10:47:52
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000231  Lock_time: 0.000117 Rows_sent: 2  Rows_examined: 10
    SET timestamp=1590720472;
    SELECT * from runoob_tbl WHERE runoob_author='xxx';
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000171  Lock_time: 0.000075 Rows_sent: 2  Rows_examined: 10
    SET timestamp=1590720472;
    SELECT * from runoob_tbl WHERE runoob_author='xxx';
    # Time: 200529 10:47:53
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000149  Lock_time: 0.000075 Rows_sent: 2  Rows_examined: 10
    SET timestamp=1590720473;
    SELECT * from runoob_tbl WHERE runoob_author='xxx';
    # Time: 200529 10:47:56
    # User@Host: root[root] @ localhost []  Id:     2
    # Query_time: 0.000039  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
    SET timestamp=1590720476;
    sleep(4);
    
    [root@zl-test003 ~]# tail -50 /var/log/mysql/mysqld.log
    2020-05-29 10:47:43 4240 [Note] Shutting down plugin 'INNODB_LOCKS'
    2020-05-29 10:47:43 4240 [Note] Shutting down plugin 'INNODB_TRX'
    2020-05-29 10:47:43 4240 [Note] Shutting down plugin 'InnoDB'
    2020-05-29 10:47:43 4240 [Note] InnoDB: FTS optimize thread exiting.
    2020-05-29 10:47:43 4240 [Note] InnoDB: Starting shutdown...
    2020-05-29 10:47:45 4240 [Note] InnoDB: Shutdown completed; log sequence number 1666138
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'BLACKHOLE'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'ARCHIVE'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'MRG_MYISAM'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'MyISAM'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'MEMORY'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'CSV'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'sha256_password'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'mysql_native_password'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'mysql_old_password'
    2020-05-29 10:47:45 4240 [Note] Shutting down plugin 'binlog'
    2020-05-29 10:47:45 4240 [Note] /usr/sbin/mysqld: Shutdown complete
    
    200529 10:47:45 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
    200529 10:47:45 mysqld_safe Logging to '/var/log/mysql/mysqld.log'.
    200529 10:47:45 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
    2020-05-29 10:47:45 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_def_timestamp server option (see documentation for more details).
    2020-05-29 10:47:45 0 [Note] /usr/sbin/mysqld (mysqld 5.6.48-log) starting as process 21710 ...
    2020-05-29 10:47:45 21710 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000)
  3. Log on to the Kibana console of the destination Alibaba Cloud Elasticsearch instance.

    For more information, see Log on to the Kibana console.

  4. In the left-side navigation pane, click Dashboard.

  5. In the dashboards list, click [Filebeat MySQL] Overview.

  6. In the upper-right corner of the page, select a time range to view the logs.

    查看日志