18. Mysql advanced log

18. Mysql advanced log

In any kind of database, there will be various logs that record all aspects of database work to help database administrators track various events that have occurred in the database. MySQL is no exception. In MySQL, there are 4 different logs, namely error log, binary log (BINLOG log), query log and slow query log. These logs record the traces of the database in different aspects.

1. Error log

The error log is one of the most important logs in MySQL. It records relevant information when mysqld starts and stops, as well as when any serious errors occur while the server is running. When any failure occurs in the database and it cannot be used normally, you can check this log first.

The log is enabled by default. The default storage directory is the data directory of mysql (var/lib/mysql). The default log file name is hostname.err (hostname is the host name).

View log location instructions:

show variables like 'log_error%';

Insert image description here

View log content:

tail -f /var/lib/mysql/xaxh-server.err

Insert image description here

2. Binary log

2.1. Overview

Binary log (BINLOG) records all DDL (data definition language) statements and DML (data manipulation language) statements, but does not include data query statements. This log plays an extremely important role in data recovery during disasters. MySQL's master-slave replication is implemented through this binlog.

Binary logging is not enabled by default. You need to enable it in the MySQL configuration file and configure the format of the MySQL log.

Configuration file location: /usr/my.cnf

Log storage location: During configuration, if the file name is given but the path is not specified, the log is written to the Mysql data directory by default.

#配置开启binlog日志, 日志的文件前缀为 mysqlbin -----> 生成的文件名如 : mysqlbin.000001,mysqlbin.000002
log_bin=mysqlbin

#配置二进制日志的格式
binlog_format=STATEMENT

2.2. Log format

STATEMENT

This log format records all SQL statements in the log file. Every SQL that modifies the data will be recorded in the log file. Through the mysqlbinlog tool provided by Mysql, you can clearly view the text of each statement. During master-slave replication, the slave will parse the log into the original text and re-execute it on the slave.

ROW

This log format records the data changes of each row in the log file instead of recording SQL statements. For example, execute the SQL statement: update tb_book set status='1'. If it is a STATEMENT log format, one line of SQL files will be recorded in the log; if it is ROW, since the entire table is updated, each row of records will be changed. , the data changes of each row will be recorded in the ROW format log.

MIXED

This is the current MySQL default log format, which is a mix of STATEMENT and ROW formats. STATEMENT is used by default, but ROW is used for recording in some special cases. The MIXED format can take advantage of the advantages of both modes while avoiding their disadvantages.

2.3. Log reading

Since the log is stored in binary mode and cannot be read directly, you need to use the mysqlbinlog tool to view it. The syntax is as follows:

mysqlbinlog log-file;

View STATEMENT format log

Execute insert statement:

insert into tb_book values(null,'Lucene','2088-05-01','0');

View the log file:

Insert image description here

mysqlbin.index: This file is a log index file, the file name of the log;

mysqlbing.000001: Log file

View log content:

mysqlbinlog mysqlbing.000001;

Insert image description here

View ROW format logs

Configuration:

#配置开启binlog日志, 日志的文件前缀为 mysqlbin -----> 生成的文件名如 : mysqlbin.000001,mysqlbin.000002
log_bin=mysqlbin

#配置二进制日志的格式
binlog_format=ROW

Insert data:

insert into tb_book values(null,'SpringCloud实战','2088-05-05','0');

If the log format is ROW, you will not understand it if you view the data directly; you can add the parameter -vv after mysqlbinlog

mysqlbinlog -vv mysqlbin.000002 

Insert image description here

2.4. Log deletion

For relatively busy systems, due to the large amount of logs generated every day, these logs will occupy a lot of disk space if they are not clear for a long time. Below we will explain several common methods of deleting logs:

method one

Delete all binlog logs through the Reset Master command. After deletion, the log number will restart from xxxx.000001.

Before querying, first query the log file:

Insert image description here

Execute the delete log command:

Reset Master

After execution, view the log file:

Insert image description here

Method 2

Execute the command purge master logs to 'mysqlbin.******', which will delete all logs before the number ******.

Method three

Execute the command purge master logs before 'yyyy-mm-dd hh24:mi:ss'. This command will delete all logs generated before the log is "yyyy-mm-dd hh24:mi:ss".

Method four

Set the parameter --expire_logs_days=#. The meaning of this parameter is to set the expiration days of the log. After the specified number of days, the log will be automatically deleted, which will help reduce the workload of the DBA to manage the log.

The configuration is as follows:
Insert image description here

3. Query log

The query log records all operation statements of the client, while the binary log does not contain SQL statements for querying data.

By default, query logging is not enabled. If you need to enable query logs, you can set the following configuration:

#该选项用来开启查询日志 , 可选值 : 0 或者 1 ; 0 代表关闭, 1 代表开启 
general_log=1

#设置日志的文件名 , 如果没有指定, 默认的文件名为 host_name.log 
general_log_file=file_name

Configure the following content in the mysql configuration file /usr/my.cnf:

Insert image description here

After the configuration is complete, perform the following operations on the database:

select * from tb_book;
select * from tb_book where id = 1;
update tb_book set name = 'lucene入门指南' where id = 5;
select * from tb_book where id < 8;

After the execution is completed, query the log file again:
Insert image description here

4. Slow query log

The slow query log records the logs of all SQL statements whose execution time exceeds the parameter long_query_time setting value and the number of scan records is not less than min_examined_row_limit. long_query_time defaults to 10 seconds, the minimum value is 0, and the accuracy can be up to microseconds.

4.1. File location and format

The slow query log is turned off by default. The slow query log can be controlled through two parameters:

# 该参数用来控制慢查询日志是否开启, 可取值: 1 和 0 , 1 代表开启, 0 代表关闭
slow_query_log=1 

# 该参数用来指定慢查询日志的文件名
slow_query_log_file=slow_query.log

# 该选项用来配置查询的时间限制, 超过这个时间将认为值慢查询, 将需要进行日志记录, 默认10s
long_query_time=10

4.2. Reading logs

Like error logs and query logs, the format of slow query log records is also plain text and can be read directly.

1) Query the value of long_query_time.

Insert image description here

2) Execute query operation

select id, title,price,num ,status from tb_item where id = 1;

Insert image description here

Since the execution time of this statement is very short, 0s, it will not be recorded in the slow query log.

select * from tb_item where title like '%阿尔卡特 (OT-927) 炭黑 联通3G手机 双卡双待165454%' ;

Insert image description here

The execution time of this SQL statement is 26.77s, which exceeds 10s, so it will be recorded in the slow query log file.

3) Check the slow query log file

Query the log file directly through the cat command:
Insert image description here

If the slow query log contains a lot of content, it is troublesome to view the file directly. At this time, you can use the mysqldumpslow tool that comes with mysql to classify and summarize the slow query log.

Insert image description here

Guess you like

Origin blog.csdn.net/Java__EE/article/details/128371642