資料庫操作審計需求中,記錄命令列互動歷史是基礎。Oracle 與 MySQL 原生提供的機制截然不同,本文整理兩者常用做法。
-- 開始記錄
SPOOL D:\logs\session_20240115.log
-- 執行任意操作 (匯入/匯出/DDL/DML)
IMP system/password@orcl FULL=Y FILE=exp.dmp LOG=imp.log
EXP system/password@orcl OWNER=scott FILE=scott.dmp LOG=exp.log
-- 結束記錄
SPOOL OFF
glogin.sql 或 login.sql 中自動啟用。sqlplus -s user/pwd @script.sql > output.log 2>&1rman target / cmdfile=backup.rman log=backup.logAUDIT SESSION, TABLE, ... BY ACCESS,寫入 SYS.AUD$ (12c+ 統一審計寫入 AUDIT_TRAIL 表)。tee 命令 (互動式會話記錄)mysql> TEE C:\logs\mysql_session_20240115.log
Logging to file 'C:\logs\mysql_session_20240115.log'
mysql> SELECT * FROM users;
mysql> UPDATE accounts SET balance=balance-100 WHERE id=1;
mysql> NOTEE
Outfile disabled.
NOTEE 停止記錄。配置 my.cnf / my.ini:
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/general.log
log_output = FILE -- 或 TABLE (寫入 mysql.general_log 表)
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2 -- 超過 2 秒記錄
| 維度 | Oracle (SQL*Plus) | MySQL (mysql client) |
|---|---|---|
| 互動式會話記錄 | SPOOL file.log | TEE file.log / NOTEE |
| 批次/腳本記錄 | OS 重導向 > log 2>&1 | OS 重導向 > log 2>&1 |
| 服務端全量審計 | 標準審計 / 統一審計 (AUD$) | General Query Log (效能敏感) |
| 單次操作獨立日誌 | 手動 SPOOL 不同檔名 | 手動 TEE 不同檔名 |
| 自動化建議 | 登入腳本自動 SPOOL 帶時間戳檔名 | 封裝腳本自動 TEE 帶時間戳檔名 |
SPOOL/TEE 含日期時間的專屬日誌檔。