导读 | MySQL的查询日志记录了所有MySQL数据库请求的信息。无论这些请求是否得到了正确的执行。默认文件名为hostname.log。默认情况下MySQL查询日志是关闭的。生产环境,如果开启MySQL查询日志,对性能还是有蛮大的影响的。另外很多时候,MySQL慢查询日志基本可以定位那些出现性能问题的SQL,所以MySQL查询日志应用的场景其实不多,有点鸡肋的感觉,它跟SQL Server中的profiler有点类似,但是这个不能跟踪某个会话、用户、客户端。它只能对整个数据库进行跟踪。 |
window 环境下
找到my.ini,在下面添加:
log=存放日志的路径/my.log
保存文件,重启mysql服务,之后就可以在对应目录的下找到my.log文件。
linux环境下
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的查询日志支持写入文件或写入数据表两种形式,这个由参数log_output控制,如下所示:
mysql> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ 1 row in set (0.00 sec)
开启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查询日志
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)
设置日志输出方式为表
如果设置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> 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)
原文来自:
本文地址://gulass.cn/mysql-operation-records.html编辑:薛鹏旭,审核员:逄增宝
Linux大全:
Linux系统大全: