Database/MySQL

[MySQL] 실행한 쿼리를 파일로 확인하기

꽁담 2018. 10. 17. 16:09

MySQL 은 실행한 쿼리를 파일 혹은 테이블에 기록하는 기능을 제공합니다.


기록과 관련된 프로퍼티


 프로퍼티

 설명

 general_log

 로깅기능의 활성화 여부를 설정

 general_log_file

 로깅기록시 파일위치 및 명칭을 지정하며, log_output 이 FILE 인 경우 활성화

 log_output

 로깅기록의 저장장소

 TABLE, FILE ,NONE



프로퍼티 설정 값 확인 방법

Master-mysql> SHOW VARIABLES LIKE 'general_log%';
+------------------+-------------------------+
| Variable_name    | Value                   |
+------------------+-------------------------+
| general_log      | OFF                     |
| general_log_file | /data/mysql/data/sh.log |
+------------------+-------------------------+
2 rows in set (0.00 sec)

Master-mysql> SHOW VARIABLES LIKE 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)


프로퍼티 설정 변경 및 쿼리 확인 방법

log_output 을 FILE 로 하는 경우 general_log_file 에 설정된 경로로,

log_output 을 TABLE 로 하는 경우 MYSQL.GENERAL_LOG 테이블에 저장됩니다.



먼저 GENERAL_LOG 를 ON 으로 변경합니다.

기본 log_output 이 FILE 로 되어있기 때문에 general_log_file 에 설정된 경로에 수행한 쿼리가 기록됩니다.

Master-mysql> SET GLOBAL GENERAL_LOG = 'ON';
Query OK, 0 rows affected (0.02 sec)

Master-mysql> SELECT * FROM MOZI;
+------+------+
| C1   | C2   |
+------+------+
|    1 | A    |
|   10 | B    |
|    3 | C    |
|    4 | A    |
|    3 | D    |
|    3 | A    |
+------+------+
6 rows in set (0.00 sec)
$ tail -1 sh.log
2018-10-17T07:05:03.440444Z        25 Query     SELECT * FROM MOZI


log_output 을 TABLE 로 변경합니다.

이 후 mysql.general_log 테이블을 확인하면, 이전에 수행한 쿼리가 기록됩니다.

Master-mysql> SET GLOBAL LOG_OUTPUT = 'TABLE';
Query OK, 0 rows affected (0.00 sec)

Master-mysql> SELECT * FROM MOZI;
+------+------+
| C1   | C2   |
+------+------+
|    1 | A    |
|   10 | B    |
|    3 | C    |
|    4 | A    |
|    3 | D    |
|    3 | A    |
+------+------+
6 rows in set (0.04 sec)
Master-mysql> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| event_time                 | user_host                 | thread_id | server_id | command_type | argument                        |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| 2018-10-17 16:07:07.181035 | root[root] @ localhost [] |        25 |         1 | Query        | SELECT * FROM MOZI              |
| 2018-10-17 16:07:17.524780 | root[root] @ localhost [] |        25 |         1 | Query        | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
2 rows in set (0.00 sec)