Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

เปิด General Log ใน MySQL

วิธีเปิด General Query Log ใน MySQL

General Log บันทึกคำสั่ง SQL ทุกคำสั่งที่ส่งมายัง Server เหมาะสำหรับ Debug และ Audit

ตรวจสอบสถานะปัจจุบัน

mysql> SHOW VARIABLES LIKE '%general_log%';
+------------------+---------------------+
| Variable_name    | Value               |
+------------------+---------------------+
| general_log      | OFF                 |
| general_log_file | DESKTOP-QN2RB3H.log |
+------------------+---------------------+

เปิด General Log

mysql> SET GLOBAL general_log = ON;
Query OK, 0 rows affected (0.18 sec)

ตรวจสอบอีกครั้ง

mysql> SHOW VARIABLES LIKE '%general_log%';
+------------------+---------------------+
| Variable_name    | Value               |
+------------------+---------------------+
| general_log      | ON                  |
| general_log_file | DESKTOP-QN2RB3H.log |
+------------------+---------------------+

เปลี่ยนตำแหน่งไฟล์ Log

mysql> SET GLOBAL general_log_file = 'E:\\mysqllog';
Query OK, 0 rows affected (0.11 sec)

หมายเหตุสำคัญ:

  • การเปิด General Log ส่งผลต่อประสิทธิภาพ (Performance Impact) เพราะเขียนไฟล์ทุก Query
  • ควรเปิดเฉพาะช่วง Debug แล้วปิดทันที: SET GLOBAL general_log = OFF;
  • การเปลี่ยนแปลงด้วย SET GLOBAL จะหายไปเมื่อ Restart MySQL | เพื่อถาวร ต้องแก้ไขในไฟล์ my.cnf / my.ini:
    [mysqld]
    general_log = 1
    general_log_file = /var/log/mysql/general.log
  • ใน Production ควรใช้ Slow Query Log แทน (Log เฉพาะ Query ช้า)