MySQL查询日志介绍
MySQL的查询日志记录了所有MySQL数据库请求的信息。无论这些请求是否得到了正确的执行。默认文件名为hostname.log。
默认情况下MySQL查询日志是关闭的。生产环境,如果开启MySQL查询日志,对性能还是有蛮大的影响的。
另外很多时候,MySQL慢查询日志基本可以定位那些出现性能问题的SQL,所以MySQL查询日志应用的场景其实不多,有点鸡肋的感觉。
MySQL查询日志配置
MySQL中的参数general_log用来控制开启、关闭MySQL查询日志,参数general_log_file用来控制查询日志的位置。所以如果你要判断MySQL数据库是否开启了查询日志,可以使用下面命令。general_log为ON表示开启查询日志,OFF表示关闭查询日志。
mysql> show variables like '%general_log%';
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/DB-Server.log |
+------------------+------------------------------+
2 rows in set (0.00 sec)
mysql>
另外,MySQL的查询日志支持写入文件或写入数据表两种形式,这个由参数log_output控制,如下所示:
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
这三个参数general_log、 general_log_file、 log_output都是动态参数,可以随时动态修改。
1、开启MySQL查询日志
mysql> set global general_log = on;
Query OK, 0 rows affected (0.11 sec)
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | ON |
+---------------+-------+
1 row in set (0.02 sec)
mysql>
2:关闭MySQL查询日志
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | ON |
+---------------+-------+
1 row in set (0.01 sec)
mysql> set global general_log=off;
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
3:设置日志输出方式为表(如果设置log_output=table
的话,则日志结果会记录到名为gengera_log
的表中,这表的默认引擎是CSV):
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global log_output='table';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+
1 row in set (0.01 sec)
mysql>
查看查询日志信息。
mysql> select * from mysql.general_log;
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| 2017-07-06 12:32:05 | root[root] @ localhost [] | 1 | 1 | Query | show variables like 'general%' |
| 2017-07-06 12:32:28 | root[root] @ localhost [] | 1 | 1 | Query | show variables like 'log_output' |
| 2017-07-06 12:32:41 | root[root] @ localhost [] | 1 | 1 | Query | select * from MyDB.test |
| 2017-07-06 12:34:36 | [root] @ localhost [] | 3 | 1 | Connect | root@localhost on |
| 2017-07-06 12:34:36 | root[root] @ localhost [] | 3 | 1 | Query | KILL QUERY 1 |
| 2017-07-06 12:34:36 | root[root] @ localhost [] | 3 | 1 | Quit | |
| 2017-07-06 12:34:51 | root[root] @ localhost [] | 1 | 1 | Query | select * from mysql.general_log |
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
7 rows in set (0.02 sec)
mysql>
4: 查询日志归档
mysql> system mv /var/lib/mysql/DB-Server.log /var/lib/mysql/DB-Server.log.20170706
mysql> system mysqladmin flush-logs -p
Enter password:
或者你在shell中执行下面命令
[root@DB-Server mysql]# mv /var/lib/mysql/DB-Server.log /var/lib/mysql/DB-Server.log.20170706
[root@DB-Server mysql]# mysqladmin flush-logs -p
Enter password:
5: 修改查询日志名称或位置
mysql> show variables like 'general_log%';
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | ON |
| general_log_file | /var/lib/mysql/DB-Server.log |
+------------------+------------------------------+
2 rows in set (0.00 sec)
mysql> set global general_log='OFF';
Query OK, 0 rows affected (0.00 sec)
mysql> set global general_log_file='/u02/mysql_log.log';
Query OK, 0 rows affected (0.00 sec)
mysql> set global general_log='ON';
Query OK, 0 rows affected (0.02 sec)
如果你遇到下面类似问题,这个是因为权限问题导致。
mysql> set global general_log_file='/u02/mysql_log.log';
ERROR 1231 (42000): Variable 'general_log_file' can't be set to the value of '/u02/mysql_log.log'
将对应目录的owner修改为mysql即可解决问题。如下所示:
[root@DB-Server u02]# chown -R mysql:mysql /u02
另外,MySQL的查询日志记录了所有MySQL数据库请求的信息。无论这些请求是否得到了正确的执行。这个就是即使我查询一个不存在的表的SQL,查询日志依然会记录。如下测试所示:
mysql> select * from MyDB.test1;
ERROR 1146 (42S02): Table 'MyDB.test1' doesn't exist
mysql> select * from MyDB.test2;
+-------+------+
| id | sex |
+-------+------+
| 10001 | |
| 10002 | |
| 10003 | |
+-------+------+
3 rows in set (0.07 sec)
mysql> select * from MyDB.kkk;
ERROR 1146 (42S02): Table 'MyDB.kkk' doesn't exist
mysql>
MySQL查询日志的应用场景
存在即是合理,既然MySQL提供了查询日志,那么肯定有其应用的地方。比如,我就遇到这样一个例子,前阵子碰到别人问mysqlslap压力测试工具中参数的问题,问题如下:
-c, --concurrency=name Number of clients to simulate for query to run.
--number-of-queries=# Limit each client to this number of queries (this is not exact).
比如我指定:--concurrency=50 --number-of-queries=100, 那么这次测试总的sql执行次数是=100 还是 50*100=5000 ?
我们不知道--number-of-queries这个参数代表所有客户端的执行次数还是每一个客户端的执行次数,这些英文如果看得不是太明白,那么我们就动手测试一下,这个时候MySQL查询日志就能发挥其用武之地了。
# mysqlslap -u root -p --concurrency=50 --create-schema='MyDB' --query='select * from MyDB.test2' --number-of-queries=100;
Enter password:
Benchmark
Average number of seconds to run all queries: 0.023 seconds
Minimum number of seconds to run all queries: 0.023 seconds
Maximum number of seconds to run all queries: 0.023 seconds
Number of clients running queries: 50
Average number of queries per client: 2
如下所示,我们可以查到这个SQL总共执行了100次,也就是说number-of-queries代表总共要运行多少次查询。每个客户运行的查询数量可以用查询总数/并发数来计算。--concurrency 表示并发量,也就是模拟多少个客户端同时执行select。 如果你指定 --number-of-queries=100, --concurrency=50 测试总的SQL执行应该是100
网友评论