SoFunction
Updated on 2025-03-04

Implementation of general_log log in mysql

introduce

1. Record scope: This log records all SQL statements in MySQL, whether they are query statements, DML statements, DDL statements, or DCL statements. All of these statements will be recorded in the general log file. Even these statements that we connect and disconnect MySQL database.
2. Record order: MySQL will record all SQL statements it receives in the general log in the order it receives. What we need to note is that the order of SQL statements accepted here does not mean that SQL statements are executed in this accepted order, because sometimes, some SQL may need to wait for other locks to be released before they are actually executed. The execution order of SQL statements matches the order in binlog.
3. Impact: Not enabled by default. After turning on, the log will be very large, which will be a lot of pressure on the disk. It has a certain impact on the performance of MySQL data.

Operation command (restart mysql invalid)

1. Check whether the log is on: show variables like 'general_log';

2. Turn on the logging function: set global general_log=on;

3. Turn off the log function: set global general_log=off;

4. Log file storage location: show variables like 'general_log_file';

5. Set the log file storage location: set global general_log_file='/var/log/mysql/general_query_log';

6. Look at the log output type table or file: show variables like 'log_output';

7. Set the output type to table (in the dedicated log table, it consumes more system resources than recording files, and writes it to the mysql.general_log table. This parameter is shared with the slow query log): set global log_output='table';

8. Set the output type to file: set global log_output='file';

9. Set the output type and support two log storage methods: set global log_output='file,table';

Configuration file modification (restart takes effect)

1. Turn on the log function: To modify the need to be in /etc/: general_log = 1 (0 means off, equal to 1 means on)
2. Set the log file saving location: modify the log file needs to be in /etc/: general_log_file = /var/log/mysql/general_query_log/
3. Set the output type to file (in the dedicated log table, it consumes more system resources than recording files, and writes it to the mysql.general_log table. This parameter is shared with the slow query log): modify it needs to be log_output = file in /etc/
4. Restart mysql: service mysqld restart

Clean up general log files

1. File
(1) Backup: cp general_query_log general_query_log.bak
(2) Clean (remember not to delete this log file directly, otherwise this file will not be automatically generated. MySQL needs to be restarted after accidentally deletion): echo "" > general_query_log

2. Table
(1) Backup: create table mysql.general_log_bak as select * from mysql.general_log;
(2) Clear: truncate table mysql.general_log;

This is the end of this article about the implementation of general_log logs in mysql. For more related contents of mysql general_log logs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!