MySQL study notes. The use of MySQL logs

mysql log overview

MySQL log records the daily operation and error information of the database. MySQL has different types of log files, divided into four categories: error log, binary log, query log and slow query log. Users can query the running status, operation or error records of MySQL from the log to solve the problem.
Of course, starting the log function will also reduce the performance of the MySQL database. For example, in a MySQL database system with very cumbersome queries, if you turn on the general query log and the slow query log, the MySQL database will spend a lot of time recording the log, which will take up a lot of time. disk space.

Error log

It records the problems that occur when the MySQL service starts, runs, or stops the service. The error log in mysql is very useful. When an error occurs when starting or stopping MySQL, the error log will be recorded, so that if we check the error log, we can find the key to the problem.

Startup and configuration error log

By default, the error log will be recorded in the data directory of the database. If there is no configuration file name, the file name defaults to the host name. err
check my
Insert picture description here
My computer name is YING-huo520
through the command: show variables like'log_error%'; view

mysql> show variables like 'log_error';
+---------------+----------------------------------------------------------------------+
| Variable_name | Value                                                                |
+---------------+----------------------------------------------------------------------+
| log_error     | E:\software\phpstudy_pro\Extensions\MySQL5.1.60\data\YING-huo520.err |
+---------------+----------------------------------------------------------------------+
1 row in set (0.00 sec)

Find my file directory and open the file
Insert picture description here
. The start and stop of the error log and the specified file name can be modified in the my.ini file.
The configuration item of the error log is log-error. Configure log-error under [mysqld] to start the error log.

[mysqld]
log-error=[path/[file_name]]

Where path is the directory path where the log is located, and file_name is the name of the log file. After modification, the MySQL service must be restarted.

Delete error log

The mysql error log is stored in the file system in the form of text and can be deleted directly.

For versions of flush logs before mysql5.5.7, you can rename the error log file to filename.err_old and create a new log file, but after mysql5.5.7, it just reopens the log file without doing log backup and creation.
Log in to the mysql database on the client and execute flush logs; to delete the error log.

Binary log

The binary log mainly records changes in the MySQL database, including all updated data or potentially updated data, and also contains information about the execution time of each statement that updates the database, but it does not contain statements that have not modified any data. The main purpose of using the binary log is to recover the database as much as possible.

Start and configure the binary log

By default, the binary log is turned on. You can start and set the binary log by modifying the mysql configuration file.
The my.ini file is modified to

log-bin=path/filename

path is the log path, filename is the file name
expire_logs_days defines the time when MySQL clears the past days, that is, the number of days that the binary log is automatically deleted, and the default is 0.
max_binlog_size defines the size limit of a single file. If the content written in the binary log exceeds the given value, the current file will be closed and a new log file will be reopened.
Add under the MySQLd group in the my.ini configuration

[mysqld]
log-bin
expire_logs_days=10
max_binlog_size=100M

Restart mysql after modification.
Use the command to check whether the binary log is enabled. It can be seen that log_bin is on and has been turned on

mysql> show variables like 'log_%';
+---------------------------------+----------------------------------------------------------------------+
| Variable_name                   | Value                                                                |
+---------------------------------+----------------------------------------------------------------------+
| log_bin                         | ON                                                                   |
| log_bin_trust_function_creators | OFF                                                                  |
| log_bin_trust_routine_creators  | OFF                                                                  |
| log_error                       | E:\software\phpstudy_pro\Extensions\MySQL5.1.60\data\YING-huo520.err |
| log_output                      | FILE                                                                 |
| log_queries_not_using_indexes   | OFF                                                                  |
| log_slave_updates               | OFF                                                                  |
| log_slow_queries                | OFF                                                                  |
| log_warnings                    | 1                                                                    |
+---------------------------------+----------------------------------------------------------------------+
9 rows in set (0.00 sec)

View binary log

You can use show binary logs to view the number of binary files.
You can also use mysqlbinlog to view its contents.

C:\Users\acer>mysqlbinlog E:/software/phpstudy_pro/Extensions/MySQL5.1.60\data/mysql-bin.000001
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
# at 4
#201031 14:32:16 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.60-community-log created 201031 14:32:16 at startup
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
ROLLBACK;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

Delete binary log

1. Use the RESET MASTER statement to delete all binary files.
Syntax format: After RESET MASTER;
executing this statement, all binary logs will be deleted, and the binary logs will be recreated. The new log files will be numbered from 000001.
2. Use the PURGE MASTER LOGS statement to delete the specified log file.
Syntax format: the PURGE{MASTER|BINARY} LOGS TO 'log_name
PURGE{MASTER|BINARY} LOGS BEFORE 'date'
first is to specify the file name, delete the specified binary log smaller than the specified file name, the second is to delete the binary file before the specified date

mysql> PURGE MASTER LOGS TO "mysql-bin.000030";
Query OK, 0 rows affected (2.23 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000030 |       106 |
| mysql-bin.000031 |       106 |
| mysql-bin.000032 |       106 |
| mysql-bin.000033 |       106 |
| mysql-bin.000034 |       106 |
| mysql-bin.000035 |       106 |
| mysql-bin.000036 |       106 |
| mysql-bin.000037 |       106 |
| mysql-bin.000038 |       106 |
| mysql-bin.000039 |       106 |
| mysql-bin.000040 |       106 |
| mysql-bin.000041 |       149 |
| mysql-bin.000042 |       149 |
| mysql-bin.000043 |       149 |
| mysql-bin.000044 |       149 |
| mysql-bin.000045 |       106 |
| mysql-bin.000046 |       106 |
| mysql-bin.000047 |       106 |
| mysql-bin.000048 |       106 |
| mysql-bin.000049 |       106 |
| mysql-bin.000050 |       106 |
+------------------+-----------+
21 rows in set (0.00 sec)
mysql> PURGE MASTER LOGS BEFORE "20201110";
Query OK, 0 rows affected (0.00 sec)

Use the binary log to restore the database

This recovery method is very effective. When the data table is deleted due to an accidental operation, you can use this method to recover the data at a certain time.
Syntax format: mysqlbinlog option filename |mysql -uuser -ppass
option parameters are often: –start-date, –stop-date, –start-position, –stop-position, for
example:mysqlbinlog --stop-date="2020-11-10 18:16:35 filename |mysql -uuser -ppass

Temporarily stop the binary log

When mysql opens the binary log, mysql will keep recording the binary log. How to stop this function? Similarly, it can be stopped by modifying the configuration file, but it needs to be restarted, and the binary log can be temporarily stopped or started by the SET_LOG_BIN statement

SET sql_log_bin=0	停止
SET sql_log_bin=1	开启

General query log

This log records all user operations, including starting and closing services, executing queries and other statements.
MySQL does not open the log by default. Use show variables like'%general%'; you can view the log status

Start query log

mysql> show variables like '%general%';
+------------------+----------------------------------------------------------------------+
| Variable_name    | Value                                                                |
+------------------+----------------------------------------------------------------------+
| general_log      | OFF                                                                  |
| general_log_file | E:\software\phpstudy_pro\Extensions\MySQL5.1.60\data\YING-huo520.log |
+------------------+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

Turn on the log syntax: set @@global.general_log=1;

mysql> set @@global.general_log=1;
Query OK, 0 rows affected (0.07 sec)

mysql> show variables like '%general%';
+------------------+----------------------------------------------------------------------+
| Variable_name    | Value                                                                |
+------------------+----------------------------------------------------------------------+
| general_log      | ON                                                                   |
| general_log_file | E:\software\phpstudy_pro\Extensions\MySQL5.1.60\data\YING-huo520.log |
+------------------+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

It has been opened, just change 1 to 0 to close the log

View query log

Open the file E:\software\phpstudy_pro\Extensions\MySQL5.1.60\data\YING-huo520.log
Insert picture description here

Delete query log

Find the directory and delete the file with the suffix .log, and then create a new query log file on the command line

mysqladmin -u root -p flush-logs

Slow query log

The slow query log is a log that records the query time exceeding the specified time, which is convenient for finding out the sentences with long execution time and low execution efficiency, which is convenient for subsequent optimization.

Enable slow query log

The same is closed by default, modify my.ini configuration to open

[mysqld]
log-slow-queries=path/filename
long_query_time=n

n is the time seconds, the default is 10 seconds

Delete slow query log

The method of deleting is the same as deleting the general query log. First delete the file, and then use the same command to rebuild the new log file

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/109621836