Home Database Mysql Tutorial MySQL General Query Log (General Query Log)

MySQL General Query Log (General Query Log)

Feb 13, 2017 am 11:11 AM
mysql

Like most relational databases, log files are an important part of the MySQL database. MySQL has several different log files, usually including error log files, binary logs, general logs, slow query logs, etc. These logs can help us locate mysqld internal events, database performance failures, record data change history, user recovery of the database, etc. This article mainly describes the general query log.

1. Composition of MySQL log file system
a. Error log: record startup and operation Or problems when stopping mysqld.
b. General log: records the established client connection and executed statements.
c. Update log: records statements that change data. This log is deprecated in MySQL 5.1.
d. Binary log: records all statements that change data. Also used for copying.
e. Slow query log: records all queries whose execution time exceeds long_query_time seconds or queries that do not use indexes.
f. Innodb log: innodb redo log

By default, all logs are created in the mysqld data directory.
You can force mysqld to close and reopen the log file (or switch to a new log in some cases) by flushing the log.
When you execute a FLUSH LOGS statement or execute mysqladmin flush-logs or mysqladmin refresh, the log is aged.
For the case where MySQL replication exists, the slave replication server will maintain more log files, called replacement logs.

2. General query log
The general query log can be stored in a text file or table , all connections and statements are recorded to this log file or table, and this log is not enabled by default.
Start it with the --log[=file_name] or -l [file_name] option. If no file_name value is given, the default name is host_name.log.
Mysqld records statements to the query log in the order it is received. This may differ from the order of execution.
Unlike update logs and binary logs, they log after the query is executed but before any lock is released.
The query log contains all statements, while the binary log does not contain statements that only query data.
Server restart and log refresh will not generate new general query log files.

3. System variables for general query logs
log_output=[none|file|table| file, table] #General query log output format
general_log=[on|off]                                                                                                                                                        

#4. Backup of general query log

In Linux or Unix, you can rename the file through the following command and Create a new file: shell> mv hostname.log hostname-old.log shell> mysqladmin flush-logs
shell> cp hostname-old.log to-backup-directory
shell> rm hostname-old.log
In Windows, the log file cannot be renamed while the server is opening the log file. You must first stop the server and then rename the log files. Then restart the server to create the new log file.



5. Demonstrate the use of general query log

a、启用通用查询日志
--演示环境
root@localhost[(none)]> show variables like '%version%';
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.5.39                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.5.39-log                   |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+

--查看系统变量
root@localhost[(none)]> show variables like '%general%';
+------------------+----------------------------+
| Variable_name    | Value                      |
+------------------+----------------------------+
| general_log      | OFF                        |
| general_log_file | /var/lib/mysql/suse11b.log |
+------------------+----------------------------+

--查看当前的通用日志,显示无日志文件
root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log 
ls: cannot access /var/lib/mysql/suse11b.log: No such file or directory
--设置变量general_log以开启通用查询日志
root@localhost[(none)]> set @@global.general_log=1;
Query OK, 0 rows affected (0.00 sec)

--再次查看通用日志文件已存在
root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log 
/var/lib/mysql/suse11b.log
root@localhost[(none)]> select * from tempdb.tb1;  --执行查询
+------+------+
| id   | val  |
+------+------+
|    1 | jack |
+------+------+

--查看通用日志文件内容
root@localhost[(none)]> system more /var/lib/mysql/suse11b.log
/usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
141003 16:18:12     4 Query     show variables like '%general%'
141003 16:18:55     4 Query     select * from tempdb.tb1


b、更改通用查询日志位置
root@localhost[(none)]> exit
Bye
suse11b:~ # service mysql stop
Shutting down MySQL...                                          done
suse11b:~ # mysqld --general_log_file=/tmp/suse11b.log --user=mysql &
[1] 47009
suse11b:~ # ps -ef|grep mysql|grep -v grep
mysql    47009 44514  1 16:22 pts/0    00:00:00 mysqld --general_log_file=/tmp/suse11b.log --user=mysql
root     47053 44514  0 16:22 pts/0    00:00:00 grep mysql
suse11b:~ # mysql
root@localhost[(none)]> system ls /tmp/suse11b.log
ls: cannot access /tmp/suse11b.log: No such file or directory
root@localhost[(none)]> show variables like '%gener%';
+------------------+------------------+
| Variable_name    | Value            |
+------------------+------------------+
| general_log      | OFF              |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

root@localhost[(none)]> set global general_log=on;
Query OK, 0 rows affected (0.01 sec)

--此时从系统变量看出,通用日志已经到/tmp目录下
root@localhost[(none)]> show variables like '%gener%';
+------------------+------------------+
| Variable_name    | Value            |
+------------------+------------------+
| general_log      | ON               |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

--发布查询
root@localhost[(none)]> select count(*) from tempdb.tb1;
+----------+
| count(*) |
+----------+
|        1 |
+----------+

--查看通用日志文件内容
root@localhost[(none)]> system more /tmp/suse11b.log
mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
141003 16:30:03     1 Query     show variables like '%gener%'
141003 16:30:09     1 Query     select count(*) from tempdb.tb1


c、通用查询日志输出方式
--可以输出为文件,表以及不输出,即TABLE,FILE,NONE
--系统变量log_output
root@localhost[(none)]> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+

--下面修改为输出为表方式
root@localhost[(none)]> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+

--发布查询
root@localhost[(none)]> select * from tempdb.tb1;
+------+------+
| id   | val  |
+------+------+
|    1 | jack |
+------+------+

--Author: Leshami
--Blog  : http://www.php.cn/

root@localhost[(none)]> system more /tmp/suse11b.log
mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
141003 16:30:03     1 Query     show variables like '%gener%'
141003 16:30:09     1 Query     select count(*) from tempdb.tb1
141003 16:31:00     1 Query     show variables like 'log_output'
141003 17:00:48     1 Query     set global log_output='TABLE'  #通用查询日志输出到文件仅仅记录到全局变量的修改

--mysql.general_log记录了通用查询日志的信息
root@localhost[(none)]> desc mysql.general_log;
+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field        | Type             | Null | Key | Default           | Extra                       |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| event_time   | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host    | mediumtext       | NO   |     | NULL              |                             |
| thread_id    | int(11)          | NO   |     | NULL              |                             |
| server_id    | int(10) unsigned | NO   |     | NULL              |                             |
| command_type | varchar(64)      | NO   |     | NULL              |                             |
| argument     | mediumtext       | NO   |     | NULL              |                             |
+--------------+------------------+------+-----+-------------------+-----------------------------+

--从通用查询日志表里查看通用查询日志的内容
root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log; 
+-----------+--------------+---------------------------------------------------------------+
| thread_id | command_type | argument                                                      |
+-----------+--------------+---------------------------------------------------------------+
|         1 | Query        | show variables like 'log_output'                              |
|         1 | Query        | select * from tempdb.tb1                                      |
|         1 | Query        | desc mysql.general_log                                        |
|         1 | Query        | select thread_id,command_type,argument from mysql.general_log |
+-----------+--------------+---------------------------------------------------------------+

root@localhost[(none)]> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+

--使用FILE,TABLE 2者混合输出通用日志
root@localhost[(none)]> set global log_output='file,table';
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> select @@global.log_output;
+---------------------+
| @@global.log_output |
+---------------------+
| FILE,TABLE          |
+---------------------+

root@localhost[(none)]> insert into tempdb.tb1 values(2,'robinson');
Query OK, 1 row affected (0.06 sec)

root@localhost[(none)]> commit;
Query OK, 0 rows affected (0.01 sec)

--验证结果,表和文件里边存在通用的日志记录
root@localhost[(none)]> system tail /tmp/suse11b.log|grep robinson
141003 17:41:54     2 Query     insert into tempdb.tb1 values(2,'robinson')
root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log
    -> where argument like '%robinson%';
+-----------+--------------+------------------------------------------------------------------------+
| thread_id | command_type | argument                                                               |
+-----------+--------------+------------------------------------------------------------------------+
|         2 | Query        | insert into tempdb.tb1 values(2,'robinson')                            |
|         2 | Query        | select thread_id,command_type,argument from mysql.general_log          |
|           |              |    where argument like ''robinson''                                    | 
+-----------+--------------+------------------------------------------------------------------------+


d、关闭通用查询日志
--可以通过设置系统变量general_log来关闭通用查询日志,此时日志输出设置为FILE,TABLE
root@localhost[(none)]> show variables like 'log_output';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| log_output    | FILE,TABLE |
+---------------+------------+

root@localhost[(none)]> set global general_log=off;
Query OK, 0 rows affected (0.01 sec)

root@localhost[(none)]> show variables like '%gener%';
+------------------+------------------+
| Variable_name    | Value            |
+------------------+------------------+
| general_log      | OFF              |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

root@localhost[(none)]> delete from tempdb.tb1 where id=2;
Query OK, 1 row affected (0.12 sec)

root@localhost[(none)]> commit;
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log
141003 17:45:13     2 Query     set global general_log=off

root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log
    -> where argument like '%delete%';
Empty set (0.00 sec)

--从上面的演示可知,尽管我们设置了log_output为FILE,TABLE,但general_log为OFF,通用日志无任何记录产生

root@localhost[(none)]> set global log_output=none;
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> set global general_log=1;
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> truncate table tempdb.tb1;
Query OK, 0 rows affected (0.01 sec)

root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log
Time                 Id Command    Argument

--通过上面的演示,在log_output=none,general_log=on的清下下无任何通用日志输出。
Copy after login

The above is the content of the MySQL General Query Log. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles