| 日誌類型 | 記錄內容 | 主要用途 |
|---|---|---|
| 錯誤日誌 | 啟動/運行/停止問題 | 故障診斷 |
| 查詢日誌 | 客戶端連線與執行語句 | 開發除錯、審計 |
| 二進制日誌 | 所有更改資料的語句 | 主從復制、時間點恢復 |
| 慢查詢日誌 | 執行超過 long_query_time 的查詢 | 效能調優、找出慢 SQL |
| 事務日誌 | InnoDB 等事務引擎產生 | ACID 保證、崩潰恢復 |
slow_query_log=ON,閾值 long_query_time(預設 10 秒)slow_query_log_file,預設 主機名-slow.log記錄所有操作,日誌量極大。MySQL 預設關閉。僅在開發/測試短期開啟定位問題。
SET GLOBAL general_log = 1; -- 1 開啟, 0 關閉 SHOW GLOBAL VARIABLES LIKE '%general_log%';
my.cnf 設 log-bin = filename 啟用,產生 filename.000001 遞增mysqldump 全量備份為基準點mysqlbinlog filename 查看日誌內容| 設定值 | 行為 | 效能 | 風險 |
|---|---|---|---|
| 0 (預設) | 交由檔案系統決定何時刷盤 | 最好 | Crash 丟失 binlog_cache 所有內容 |
| 1 | 每次事務提交強制 fsync 刷盤 | 最差(寫入效能差距可達 5 倍) | 最安全,僅可能丟失未完成事務 |
| N | 每 N 次提交刷盤一次 | 折中 | 折中 |
高並發系統建議視業務容忍度在 1 與 N 之間取捨。
binlog_cache_size:單事務記憶體快取,不足時落地臨時檔 → 增加可減少 IOmax_binlog_cache_size:單事務最大快取,不足報錯 Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage建議 512MB~1GB(不可超 1GB)。遇大事務接近尾部時為保完整性不切換,實際可能略超過設定值。
mysql> SHOW VARIABLES LIKE '%binlog%'; +--------------------------------+------------+ | Variable_name | Value | +--------------------------------+------------+ | binlog_cache_size | 1048576 | | innodb_locks_unsafe_for_binlog | OFF | | max_binlog_cache_size | 4294967295 | | max_binlog_size | 1073741824 | | sync_binlog | 0 | +--------------------------------+------------+