查看MySQL數據庫的SQL語句的執行記錄日誌
方法1:可以使用processlist查看SQL執行語句,但是有個弊端,就是隻能查看正在執行的sql語句,無法查看歷史執行的語句。
> use information_schema;
> show processlist;
或者
> select * from information_schema.`PROCESSLIST` where info is not null;
+--------+------+-----------+--------------------+---------+------+----------------------+-----------------------------------------------------------------------+---------+-------+-----------+----------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | TIME_MS | STAGE | MAX_STAGE | PROGRESS |
+--------+------+-----------+--------------------+---------+------+----------------------+-----------------------------------------------------------------------+---------+-------+-----------+----------+
| 347182 | root | localhost | information_schema | Query | 0 | Filling schema table | select * from information_schema.`PROCESSLIST` where info is not null | 0.502 | 0 | 0 | 0.000 |
+--------+------+-----------+--------------------+---------+------+----------------------+-----------------------------------------------------------------------+---------+-------+-----------+----------+
1 row in set (0.00 sec)
方法2:開啓數據庫的日誌模式,通過日誌查看歷史執行記錄
* 查看當前配置
> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
> show variables like '%general_log%';
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | OFF |
| general_log_file | /var/log/mariadb/mariadb.log |
+------------------+------------------------------+
* 開啓&關閉日誌模式(可選擇輸出到表或文件中):
> SET GLOBAL log_output = 'FILE'; SET GLOBAL general_log = 'ON'; //日誌開啓(日誌輸出到文件)
> SET GLOBAL log_output = 'FILE'; SET GLOBAL general_log = 'OFF'; //日誌關閉
或者
> SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = 'ON'; //日誌開啓(日誌輸出到表:mysql.general_log)
> SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = 'OFF'; //日誌關閉
> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
> show variables like '%general_log%';
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | ON |
| general_log_file | /var/log/mariadb/mariadb.log |
+------------------+------------------------------+
* 查看日誌文件中的執行記錄:
#tail -f /var/log/mariadb/mariadb.log
......
347189 Query INSERT INTO `table_copy1` (`id`, `col02_str`, `col03_str`, `col04_int`, `col05_integer`, `col06_bigint`)
VALUES ( 1, 'aaaa', 'bbbbb', 20, 30, 655360),
( 2, 'xxxx', 'yyyy', 21, 333, 65536000),
( 3, 'asasdf', 'sdkfjdkf', 55, 900, 2222),
( 4, 'test', 'testaaa', 11, 22, 33),
( 5, 'test05', 'testssssss', 222, 222, 222),
( 6, 'test06', 'aaaa', 111, 111, 222),
( 7, 'aaaa', 'bbbb', 22, 22, 22),
( 8, '你好', 'hello', 25, 25, 25),
( 9, 'aaa', 'aaa', 11, 11, 11),
( 10, '', 'bbbbddddrrrrssss4444', null, null, null),
347189 Query commit
......
* 表查詢執行記錄:
> SELECT * from mysql.general_log ORDER BY event_time DESC;
* 日誌查詢執行記錄
> SELECT * from mysql.general_log ORDER BY event_time DESC;
* 清空表
> truncate table mysql.general_log; //該表僅支持truncate不支持delete