[mysql] 실행한 쿼리를 로깅/조회하는 방법.('general_log'의 Value를 On으로 설정하여, 실행한 쿼리를 'general_log_file'의 Value에 로깅하도록 설정.)

2023. 6. 13. 10:54Database/mysql

728x90
728x90

1.MySQL 서버 노드에서 root 계정으로 DB에 접속.

# mysql -u root -p $MySQL_DB_NAME
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 337
Server version: 5.7.13-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2.'general_log' 관련 설정 확인.

mysql> show variables like 'general%';
+------------------+---------------------------+
| Variable_name    | Value                     |
+------------------+---------------------------+
| general_log      | OFF                       |
| general_log_file | /var/lib/mysql/hkhost.log |
+------------------+---------------------------+
2 rows in set (0.00 sec)

3.'general_log' Value ON으로 설정.

  • 'general_log'의 Value가 ON일 경우, 'general_log_file'의 Value에 수행되는 쿼리의 내용이 기록됨.
mysql> set global general_log=on;
Query OK, 0 rows affected (0.03 sec)

mysql> show variables like 'general%';
+------------------+---------------------------+
| Variable_name    | Value                     |
+------------------+---------------------------+
| general_log      | ON                        |
| general_log_file | /var/lib/mysql/hkhost.log |
+------------------+---------------------------+
2 rows in set (0.01 sec)

mysql> quit
Bye
# date "+%Y-%m-%d %H:%M:%S"
2023-06-12 10:15:59

4.DB에서 수행되는 쿼리가 로깅되는 로그 파일('general_log_file'의 Value) 조회.

# tail -f /var/lib/mysql/hkhost.log
2023-06-12T10:16:21.735533Z  317 Query	SHOW SLAVE STATUS
2023-06-12T10:16:21.736441Z  317 Query	show full processlist
2023-06-12T10:16:22.428964Z  278 Query	COMMIT
2023-06-12T10:16:22.429498Z  278 Query	COMMIT
2023-06-12T10:16:22.743298Z  317 Query	show global status
2023-06-12T10:16:22.752913Z  317 Query	show global variables like "read_only"
2023-06-12T10:16:22.759118Z  317 Query	SHOW SLAVE STATUS
2023-06-12T10:16:22.760177Z  317 Query	show full processlist
...
728x90
728x90