Article directory
log
1. Error log
The error log is one of the most important logs in MySQL.
Records information about when MySQLd starts and stops, and when any serious errors occur while the server is running.
The log is enabled by default, and the default storage directory is mysqld.log
View log location
show variables like '%log_error%';
log_error records which file is associated with the error log, and here I am associated with D:\Sort\Mysql\mysql-8.0.32-winx64\data\zhangjingqi-pc.err
The file is opened as shown in the figure below. When the mysql server is abnormal, it will be recorded in this log file
Two, binary log
The binary log (BINLOG) records all DDL (database table) statements and DML (addition, deletion, modification) statements, but does not include data query statements (Select, Show).
2.1 Introduction
effect
①. Data recovery in case of disaster; ②. MySQL master-slave replication
In the MySQL8 version, the default binary log is enabled, and the parameters involved are as follows:
show variables like '%log_bin%';
- log_bin
Binary is on or off
- log_bin_basename
The base name (prefix) of the binlog log of the current database server. The specific binlog file name needs to be numbered on the basis of the basename (the number starts from 000001).
- log_bin_index
Binlog index file, which records the binlog files associated with the current server.
2.2 Format
View log format
show variables like '%binlog_format%';
three formats
- STATEMENT
SQL statement-based logging records SQL statements, and the SQL that modifies the data will be recorded in the log file.
- ROW
Row-based logging records data changes for each row. (default)
- MIXED
The format of STATEMENT and ROW is mixed, STATEMENT is used by default, and it will be automatically switched to ROW for recording in some special cases.
2.3 View the binary log
Since the log is stored in binary mode, it cannot be read directly. It needs to be viewed through the binary log query tool mysqlbinlog
Concrete syntax :
mysqlbinlog [ 参数选项 ] logfilename
parameter options
-d specifies the database name, and only lists the operations related to the specified database.
-o Ignore the first n lines of commands in the log.
-v Refactor row events (data changes) into SQL statements
-vv (two v, not w) Refactor row events (data changes) into SQL statements, and output comment information
2.3.1 Row-based binary log format
example
Enter the bin layer D:\Sort\Mysql\mysql-8.0.32-winx64\bin where mysql is installed, and execute the following command
mysqlbinlog --no-defaults ../data/binlog.000021
After execution, the following content will appear, but the executed SQL statement cannot be seen
If it is a row-based log format, we need to implement parameter options to reconstruct row data into SQL statements
Refactor row data into SQL statements
mysqlbinlog --no-defaults -v ../data/binlog.000021
--no-defaults role:
Solve unknown variable 'default-character-set=utf8mb4 error
2.3.2 Statement-based binary log format
Need to set binlog_format to STATEMENT
restart service
systemctl restart mysqld
Check whether to modify
show variables like '%binlog_format%';
As shown in the figure below, the modification is successful
And after we modified the binary log format, we regenerated the binary log file
Execute modification statement
update tb_user set age = age+1;
To view binary files, there is no need to add -v, because the SQL statement is recorded in the statement log format
mysqlbinlog --no-defaults ../data/binlog.000022
You can also find the corresponding SQL statement in the command line window
2.4 Log deletion
instruction
- reset master
Delete all binlog logs. After deletion, the log number will restart from binlog.000001
- purge master logs to ‘binlog.*’
Delete all logs before the * number, for example, the following is to delete the files before the number 000018, but 18 will still be in
purge master logs to 'binlog.000018'
- purge master logs before ‘yyyy-mm-dd hh24:mi:ss’
Delete all logs generated before the log is "yyyy-mm-dd hh24:mi:ss"
- set expiration time
show variables like '%binlog_expire_logs_seconds%';
If you want to modify it, configure it in the my.ini file
3. Query log
All operation statements of the client are recorded in the query log , but the binary log does not contain SQL statements for querying data .
By default, query logging is disabled.
Enable query log
#该选项用来开启查询日志 , 可选值 : 0 或者 1 ; 0 代表关闭, 1 代表开启
general_log=1
#设置日志的文件名 , 如果没有指定, 默认的文件名为 host_name.log
general_log_file=mysql_query.log
Restart the service and check if it is enabled
show variables like '%general%';
The content is indeed just configured
4. Slow query log
Previous article: MySQL - storage engine and index application_
There is an analysis of the slow query log
The slow query log records the logs of all SQL statements whose execution time exceeds the setting value of the parameter long_query_time and the number of scanned records is not less than min_examined_row_limit. It is not enabled by default .
long_query_time defaults to 10 seconds, the minimum is 0, and the precision can reach microseconds.
Configure in the configuration file
# 开启MySQL慢日志查询开关
slow_query_log=1
# 设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会视为慢查询,记录慢查询日志
long_query_time=2
Check where the slow log is stored , of course we can also reconfigure
show variables like '%slow%';
Execute a slow SQL statement with 2 million records
SELECT * FROM tb_user2;
Check the slow query log file , but I feel that there is a problem with the time zone. I am here in Dongba District, 22:43, which is exactly eight hours apart.
By default, administrative statements are not logged, nor are queries that do not use indexes for lookups. This behavior can be changed using log_slow_admin_statements and log_queries_not_using_indexes as described below.
#记录执行较慢的管理语句 log_slow_admin_statements =1 #记录执行较慢的未使用索引的语句 log_queries_not_using_indexes = 1