Collect MySQL logs with self-managed Filebeat
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
-
Create an Alibaba Cloud Elasticsearch instance to receive logs and a Kibana console for analysis, and an ECS instance to host MySQL and Filebeat.
-
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.
-
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.
-
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
-
An Alibaba Cloud Elasticsearch instance is created.
For more information, see Create an Alibaba Cloud Elasticsearch instance. This guide uses a General-purpose (v6.7.0) instance as an example.
-
An ECS instance is created.
For more information, see Create an instance by using the custom launch tab. This guide uses a CentOS-based ECS instance as an example.
Step 1: Install and configure MySQL
-
Connect to the ECS instance.
For more information, see Connect to a Linux instance by using a password.
NoteThe commands in this guide are run as a non-root user.
-
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 -
Install MySQL.
sudo yum install mysql-server -
Configure the error log file and slow query log file in the my.cnf file.
NoteBy 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.
-
Open the my.cnf file.
sudo vim /etc/my.cnf -
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.logParameter
Description
log_queries_not_using_indexesSpecifies whether to record queries that do not use indexes to the slow query log.
1enables recording, and0disables it.slow_query_logEnables or disables the slow query log.
onenables the log, andoffdisables it.slow_query_log_fileThe storage path of the slow query log.
long_query_timeThe slow query threshold in seconds. MySQL logs queries that exceed this threshold to the file specified by
slow_query_log_file.ImportantFor testing purposes, this parameter is set to
0in this topic. In a production environment, set the value based on your business requirements. -
(Optional) Create the log files.
ImportantMySQL 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 -
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
-
-
Start MySQL and check its status.
sudo systemctl start mysqld sudo systemctl status mysqld
Step 2: Install and configure Filebeat
-
Log on to the Kibana console of your Alibaba Cloud Elasticsearch instance.
For more information, see Log on to the Kibana console.
-
In the left-side navigation pane, click Logs.
-
Click View setup instructions.
-
On the Add Data to Kibana page, click MySQL logs.
-
On the Self managed page, click RPM.
NoteThis guide uses a Linux-based instance, so RPM is selected. Select the installation method suitable for your operating system.
-
Follow the on-screen instructions to install Filebeat on your ECS instance.
-
Modify the MySQL module configuration to specify the paths for the error and slow query log files.
-
Enable the MySQL module.
sudo filebeat modules enable mysql -
Open the mysql.yml file.
sudo vim /etc/filebeat/modules.d/mysql.yml -
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
enabledSet the value to
true.var.pathsThe 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.
-
-
Configure the filebeat.yml file.
-
Open the filebeat.yml file.
sudo vim /etc/filebeat/filebeat.yml -
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 -
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. -
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
hostsThe 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.NoteUse 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.
usernameThe username for accessing the Alibaba Cloud Elasticsearch instance. Default value: elastic.
passwordThe 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.
-
-
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
-
On the ECS instance, restart MySQL and run a test query.
Run the following command to restart MySQL:
sudo systemctl restart mysqld -
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) -
Log on to the Kibana console of the destination Alibaba Cloud Elasticsearch instance.
For more information, see Log on to the Kibana console.
-
In the left-side navigation pane, click Dashboard.
-
In the dashboards list, click [Filebeat MySQL] Overview.
-
In the upper-right corner of the page, select a time range to view the logs.
