

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon RDS for Oracle 資料庫日誌檔案
<a name="USER_LogAccess.Concepts.Oracle"></a>

您可使用 Amazon RDS 主控台或 API 存取 Oracle 提醒日誌、稽核檔案及追蹤檔案。如需關於檢視、下載與查看資料庫日誌檔案的資訊，請參閱[監控 Amazon RDS 日誌檔案](USER_LogAccess.md)。

所提供的 Oracle 稽核檔案為標準 Oracle 稽核檔案。Amazon RDS 可支援 Oracle 精細稽核 (FGA) 功能。然而，存放於 `SYS.FGA_LOG$` 資料表且可由 `DBA_FGA_AUDIT_TRAIL` 檢視的 FGA 事件，將無法由日誌存取。

[https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DescribeDBLogFiles.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DescribeDBLogFiles.html) API 操作會列出可供資料庫執行個體使用的 Oracle 日誌檔案，並忽略 `MaxRecords` 參數，且最多回傳 1,000 筆記錄。呼叫傳回 `LastWritten` 作為 POSIX 日期，以毫秒為單位。

**Topics**
+ [保留排程](#USER_LogAccess.Concepts.Oracle.Retention)
+ [使用 Oracle 追蹤檔案](#USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles)
+ [將 Oracle 日誌發佈至 Amazon CloudWatch Logs](#USER_LogAccess.Oracle.PublishtoCloudWatchLogs)
+ [存取警示日誌和接聽程式日誌](#USER_LogAccess.Concepts.Oracle.AlertLogAndListenerLog)

## 保留排程
<a name="USER_LogAccess.Concepts.Oracle.Retention"></a>

若日誌檔案變得過大，Oracle 資料庫引擎可能會輪換這些檔案。欲保留稽核或追蹤檔案，請下載這些檔案。如果您將檔案儲存在本機，可降低 Amazon RDS 的儲存成本，並為資料騰出更多可用空間。

下列資料表顯示 Amazon RDS 上的 Oracle 提醒日誌、稽核檔案及追蹤檔案的保留排程。


****  

| 日誌類型 | 保留排程 | 
| --- | --- | 
|  提醒日誌  |   文字提醒記錄每日輪換，其 30 天的保留則由 Amazon RDS 管理。XML 提醒日誌至少會保留七天。您可使用 `ALERTLOG` 畫面來存取此記錄。  | 
|  稽核檔案  |   預設稽核檔案的保留時間為 7 日。Amazon RDS 可能會刪除超過 7 日的稽核檔案。  | 
|  追蹤檔案  |  預設的追蹤檔案保留時間為 7 日。Amazon RDS 可能會刪除超過 7 日的追蹤檔案。  | 
|  接聽程式日誌  |   預設接聽程式記錄的保留時間為 7 日。Amazon RDS 可能會刪除超過 7 日的接聽程式記錄。  | 

**注意**  
稽核檔案和追蹤檔案共享相同的保留組態。

## 使用 Oracle 追蹤檔案
<a name="USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles"></a>

您可於下方取得 Amazon RDS 建立、重新整理、存取和刪除追蹤檔案的說明。

**Topics**
+ [列出檔案](#USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.ViewingBackgroundDumpDest)
+ [產生追蹤檔案並追蹤工作階段](#USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Generating)
+ [擷取追蹤檔案](#USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Retrieving)
+ [清除追蹤檔案](#USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Purging)

### 列出檔案
<a name="USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.ViewingBackgroundDumpDest"></a>

您可使用這兩個程序之一來存取 `background_dump_dest` 路徑上的檔案。第一個程序會重新整理畫面，其中內含 `background_dump_dest` 中現有的所有檔案。

```
1. EXEC rdsadmin.manage_tracefiles.refresh_tracefile_listing;
```

重新整理畫面後，請查詢下列畫面來存取結果。

```
1. SELECT * FROM rdsadmin.tracefile_listing;
```

上述程序的替代做法是使用 `FROM table`，採用類似資料表的格式來串流非關聯式資料，以列出資料庫目錄內容。

```
1. SELECT * FROM TABLE(rdsadmin.rds_file_util.listdir('BDUMP'));
```

下列查詢顯示日誌檔案的文字。

```
1. SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('BDUMP','alert_dbname.log.date'));
```

在僅供讀取複本上，查詢 `V$DATABASE.DB_UNIQUE_NAME` 以取得 BDUMP 目錄名稱。如果唯一名稱為 `DATABASE_B`，則 BDUMP 目錄為 `BDUMP_B`。下列範例會在複本上查詢 BDUMP 名稱，然後使用此名稱查詢 `alert_DATABASE.log.2020-06-23` 的內容。

```
1. SELECT 'BDUMP' || (SELECT regexp_replace(DB_UNIQUE_NAME,'.*(_[A-Z])', '\1') FROM V$DATABASE) AS BDUMP_VARIABLE FROM DUAL;
2. 
3. BDUMP_VARIABLE
4. --------------
5. BDUMP_B
6. 
7. SELECT TEXT FROM table(rdsadmin.rds_file_util.read_text_file('BDUMP_B','alert_DATABASE.log.2020-06-23'));
```

### 產生追蹤檔案並追蹤工作階段
<a name="USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Generating"></a>

由於 `ALTER SESSION` 不具任何限制，許多在 Oracle 中產生追蹤檔案的標準方法仍可供 Amazon RDS 資料庫執行個體使用。需要更多存取權限的追蹤檔案，可參考下列程序。


****  

|  Oracle 方法  |  Amazon RDS 方法 | 
| --- | --- | 
|  `oradebug hanganalyze 3 `  |  `EXEC rdsadmin.manage_tracefiles.hanganalyze; `  | 
|  `oradebug dump systemstate 266 `  |  `EXEC rdsadmin.manage_tracefiles.dump_systemstate;`  | 

您可使用許多標準方法來追蹤連接至 Amazon RDS 中 Oracle 資料庫執行個體的個別工作階段。若要啟用工作階段的追蹤功能，您可以在 Oracle 提供的 PL/SQL 套件中執行子程式，例如 `DBMS_SESSION` 和 `DBMS_MONITOR`。如需詳細資訊，請參閱 Oracle 文件中的[為工作階段啟用追蹤](https://docs.oracle.com/database/121/TGSQL/tgsql_trace.htm#GUID-F872D6F9-E015-481F-80F6-8A7036A6AD29)。

### 擷取追蹤檔案
<a name="USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Retrieving"></a>

您可使用 Amazon RDS 管理之外部資料表上的標準 SQL 查詢，擷取 `background_dump_dest` 中的所有追蹤檔案。欲使用此方法，務必執行此程序，為此資料表設定特定追蹤檔案的位置。

例如，您可以使用上述的 `rdsadmin.tracefile_listing` 畫面，之後即可列出系統上的所有追蹤檔案。接著，請使用下列程序，將 `tracefile_table` 畫面設定為指向特定追蹤檔案。

```
1. EXEC rdsadmin.manage_tracefiles.set_tracefile_table_location('CUST01_ora_3260_SYSTEMSTATE.trc');
```

下列範例會在目前的結構描述內建立外部資料表，其位置則設定為所提供的文件。您可使用 SQL 查詢將內容擷取至本機檔案。

```
1. SPOOL /tmp/tracefile.txt
2. SELECT * FROM tracefile_table;
3. SPOOL OFF;
```

### 清除追蹤檔案
<a name="USER_LogAccess.Concepts.Oracle.WorkingWithTracefiles.Purging"></a>

追蹤檔案可能會累積和耗用磁碟空間。依預設，Amazon RDS 會清除超過 7 日的追蹤檔案和記錄檔案。您可使用 `show_configuration` 程序來檢視並設定追蹤檔案保留期間。您應執行命令 `SET SERVEROUTPUT ON` 以檢視組態結果。

下列範例顯示目前的追蹤檔案保留期間，然後設定為新的追蹤檔案保留期間。

```
 1. # Show the current tracefile retention
 2. SQL> EXEC rdsadmin.rdsadmin_util.show_configuration;
 3. NAME:tracefile retention
 4. VALUE:10080
 5. DESCRIPTION:tracefile expiration specifies the duration in minutes before tracefiles in bdump are automatically deleted.
 6. 		
 7. # Set the tracefile retention to 24 hours:
 8. SQL> EXEC rdsadmin.rdsadmin_util.set_configuration('tracefile retention',1440);
 9. SQL> commit;
10. 
11. #show the new tracefile retention
12. SQL> EXEC rdsadmin.rdsadmin_util.show_configuration;
13. NAME:tracefile retention
14. VALUE:1440
15. DESCRIPTION:tracefile expiration specifies the duration in minutes before tracefiles in bdump are automatically deleted.
```

除了定期的清除程序，您也可透過 `background_dump_dest` 手動移除檔案。下列範例說明如何清除超過五分鐘的所有檔案。

```
EXEC rdsadmin.manage_tracefiles.purge_tracefiles(5);
```

您亦可清除符合特定模式的所有檔案 (如果這樣做，請勿包含副檔名，例如 .trc)。下列範例說明如何清除以 `SCHPOC1_ora_5935` 為開頭的所有檔案。

```
1. EXEC rdsadmin.manage_tracefiles.purge_tracefiles('SCHPOC1_ora_5935');
```

## 將 Oracle 日誌發佈至 Amazon CloudWatch Logs
<a name="USER_LogAccess.Oracle.PublishtoCloudWatchLogs"></a>

您可以設定 RDS for Oracle 資料庫執行個體，以將日誌資料發佈至 Amazon CloudWatch Logs 中的日誌群組。使用 CloudWatch Logs，您可以分析日誌資料，並使用 CloudWatch 來建立警示和檢視指標。您可以使用 CloudWatch Logs 將日誌記錄存放在高耐用性的儲存裝置中。

Amazon RDS 將每個 Oracle 資料庫日誌發佈為日誌群組中獨立的資料庫流。例如，若您將匯出功能設定為包含稽核日誌，則稽核資料會存放在 `/aws/rds/instance/my_instance/audit` 日誌群組內的稽核日誌流中。下表摘要說明 RDS for Oracle 將日誌發佈至 Amazon CloudWatch Logs 的要求。


| 日誌名稱 | 需求 | 預設 | 
| --- | --- | --- | 
|  警示日誌  |  無。您無法停用此日誌。  |  已啟用  | 
|  追蹤日誌  |  將 `trace_enabled` 參數設定為 `TRUE`，或將其保留為預設值。  |  `TRUE`  | 
|  稽核日誌  |  將 `audit_trail` 參數設定為以下任何允許值： <pre>{ none | os | db [, extended] | xml [, extended] }</pre>  |  `none`  | 
|  接聽程式日誌  |  無。您無法停用此日誌。  |  已啟用  | 
|  Oracle Management Agent 日誌  |  無。您無法停用此日誌。  |  已啟用  | 

此 Oracle Management Agent 日誌包含下表所示的日誌群組。


****  

| 日誌名稱 | CloudWatch 日誌群組 | 
| --- | --- | 
| emctl.log | oemagent-emctl | 
| emdctlj.log | oemagent-emdctlj | 
| gcagent.log | oemagent-gcagent | 
| gcagent\$1errors.log | oemagent-gcagent-errors | 
| emagent.nohup | oemagent-emagent-nohup | 
| secure.log | oemagent-secure | 

如需詳細資訊，請參閱 Oracle 文件中的[尋找 Management Agent 日誌和追蹤檔案](https://docs.oracle.com/en/enterprise-manager/cloud-control/enterprise-manager-cloud-control/13.4/emadm/locating-management-agent-log-and-trace-files1.html#GUID-9C710D78-6AA4-42E4-83CD-47B5FF4892DF)。

### 主控台
<a name="USER_LogAccess.Oracle.PublishtoCloudWatchLogs.console"></a>

**從 將 Oracle 資料庫日誌發佈至 CloudWatch Logs AWS 管理主控台**

1. 前往 [https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)，開啟 Amazon RDS 主控台。

1. 在導覽窗格中選擇 **Databases** (資料庫)，然後選擇您要修改的資料庫執行個體。

1. 選擇 **Modify (修改)**。

1. 在 **Log exports (日誌匯出)** 區段中，選擇您要開始發佈至 CloudWatch Logs 的日誌。

1. 選擇 **Continue (繼續)**，然後在摘要頁面上選擇 **Modify DB Instance (修改資料庫執行個體)**。

### AWS CLI
<a name="USER_LogAccess.Oracle.PublishtoCloudWatchLogs.CLI"></a>

欲發佈 Oracle 日誌，您可搭配下列參數使用 [https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-instance.html](https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-instance.html) 命令：
+ `--db-instance-identifier`
+ `--cloudwatch-logs-export-configuration`

**注意**  
`--cloudwatch-logs-export-configuration` 選項的變更一律立即套用到資料庫執行個體。因此，`--apply-immediately` 和 `--no-apply-immediately` 選項沒有效果。

您亦可使用下列命令來發佈 Oracle 日誌：
+ [https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-db-snapshot.html](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-db-snapshot.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-s3.html](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-s3.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-to-point-in-time.html](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-to-point-in-time.html)

**Example**  
下列範例建立 Oracle 資料庫執行個體並啟用發佈至 CloudWatch Logs。`--cloudwatch-logs-export-configuration` 值為 JSON 字串陣列。字串可以是 `alert`、`audit`、`listener` 以及 `trace` 的任意組合。  
針對 Linux、macOS 或 Unix：  

```
aws rds create-db-instance \
    --db-instance-identifier mydbinstance \
    --cloudwatch-logs-export-configuration '["trace","audit","alert","listener","oemagent"]' \
    --db-instance-class db.m5.large \
    --allocated-storage 20 \
    --engine oracle-ee \
    --engine-version 19.0.0.0.ru-2024-04.rur-2024-04.r1 \
    --license-model bring-your-own-license \
    --master-username myadmin \
    --manage-master-user-password
```
在 Windows 中：  

```
aws rds create-db-instance ^
    --db-instance-identifier mydbinstance ^
    --cloudwatch-logs-export-configuration trace alert audit listener oemagent ^
    --db-instance-class db.m5.large ^
    --allocated-storage 20 ^
    --engine oracle-ee ^
    --engine-version 19.0.0.0.ru-2024-04.rur-2024-04.r1 ^
    --license-model bring-your-own-license ^
    --master-username myadmin ^
    --manage-master-user-password
```

**Example**  
下列範例修改現有的 Oracle 資料庫執行個體，以將日誌檔案發佈至 CloudWatch Logs。`--cloudwatch-logs-export-configuration` 值為 JSON 物件。此物件的金鑰為 `EnableLogTypes`，而其值為字串陣列，其中包含 `alert`、`audit`、`listener` 以及 `trace` 的任意組合。  
針對 Linux、macOS 或 Unix：  

```
aws rds modify-db-instance \
    --db-instance-identifier mydbinstance \
    --cloudwatch-logs-export-configuration '{"EnableLogTypes":["trace","alert","audit","listener","oemagent"]}'
```
在 Windows 中：  

```
aws rds modify-db-instance ^
    --db-instance-identifier mydbinstance ^
    --cloudwatch-logs-export-configuration EnableLogTypes=\"trace\",\"alert\",\"audit\",\"listener\",\"oemagent\"
```

**Example**  
下列範例修改現有的 Oracle 資料庫執行個體，以停止將稽核與接聽程式日誌檔案發佈至 CloudWatch Logs。`--cloudwatch-logs-export-configuration` 值為 JSON 物件。此物件的金鑰為 `DisableLogTypes`，而其值為字串陣列，其中包含 `alert`、`audit`、`listener` 以及 `trace` 的任意組合。  
針對 Linux、macOS 或 Unix：  

```
aws rds modify-db-instance \
    --db-instance-identifier mydbinstance \
    --cloudwatch-logs-export-configuration '{"DisableLogTypes":["audit","listener"]}'
```
在 Windows 中：  

```
aws rds modify-db-instance ^
    --db-instance-identifier mydbinstance ^
    --cloudwatch-logs-export-configuration DisableLogTypes=\"audit\",\"listener\"
```

### RDS API
<a name="USER_LogAccess.Oracle.PublishtoCloudWatchLogs.API"></a>

您可以使用 RDS API 來發佈 Oracle 資料庫日誌。您可以使用下列參數來呼叫 [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBInstance.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBInstance.html) 動作：
+ `DBInstanceIdentifier`
+ `CloudwatchLogsExportConfiguration`

**注意**  
`CloudwatchLogsExportConfiguration` 參數的變更一律立即套用到資料庫執行個體。因此，`ApplyImmediately` 參數沒有效果。

您也可以呼叫下列 RDS API 操作來發佈 Oracle 日誌：
+ [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html)
+ [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html)
+ [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromS3.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromS3.html)
+ [https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceToPointInTime.html](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceToPointInTime.html)

以下列參數來執行其中一個 RDS API 操作：
+ `DBInstanceIdentifier`
+ `EnableCloudwatchLogsExports`
+ `Engine`
+ `DBInstanceClass`

視您執行的 RDS 操作而定，可能需要其他參數。

## 存取警示日誌和接聽程式日誌
<a name="USER_LogAccess.Concepts.Oracle.AlertLogAndListenerLog"></a>

您可以使用 Amazon RDS 主控台檢視提醒日誌。您亦可使用下列 SQL 陳述式。

```
1. SELECT message_text FROM alertlog;
```

使用 Amazon CloudWatch Logs 存取接聽程式日誌。

**注意**  
Oracle 會在提醒與接聽程式日誌超過 10 MB 時進行輪換，此時這些日誌將不會顯示於 Amazon RDS 的畫面。