

# Amazon CloudWatch Logs への MySQL ログの発行
<a name="USER_LogAccess.MySQLDB.PublishtoCloudWatchLogs"></a>

MySQL DB インスタンスを設定して、ログデータを Amazon CloudWatch Logs のロググループに発行することができます。CloudWatch Logs を使用すると、ログデータのリアルタイム分析や、CloudWatch を使用したアラームの作成、メトリクスの表示を行うことができます。CloudWatch Logs を使用して、耐久性の高いストレージにログレコードを格納できます。

Amazon RDS は、MySQL データベースログを、ロググループの別のデータストリーミングとしてそれぞれ発行します。例えば、エクスポート機能を設定して、スロークエリログを作成すると、スロークエリデータは、`/aws/rds/instance/my_instance/slowquery` ロググループのスロークエリログストリーミングに保存されます。

エラーログはデフォルトで有効になります。他の MySQL ログの要件の概要を次の表に示します。


| ログ | 要件 | 
| --- | --- | 
|  監査ログ  |  DB インスタンスは、`MARIADB_AUDIT_PLUGIN` オプションを指定したカスタムオプショングループを使用する必要があります。  | 
|  全般ログ  |  DB インスタンスは、パラメータ設定 `general_log = 1` を指定して一般ログを有効にしたカスタムパラメータグループを使用する必要があります。  | 
|  スロークエリログ  |  DB インスタンスは、パラメータ設定 `slow_query_log = 1` を指定してスロークエリログを有効にしたカスタムパラメータグループを使用する必要があります。  | 
|  IAM データベース認証エラーログ  |  DB インスタンスを作成または変更して、DB インスタンスのログタイプ `iam-db-auth-error` を有効にする必要があります。  | 
|  ログ出力  |  DB インスタンスは、パラメータ設定 `log_output = FILE` を指定してログをファイルシステムに書き込み、CloudWatch Logs に発行するカスタムパラメータグループを使用する必要があります。  | 

## コンソール
<a name="USER_LogAccess.MySQL.PublishtoCloudWatchLogs.CON"></a>

**コンソールを使用して CloudWatch Logs に MySQL ログを発行するには**

1. Amazon RDS コンソール ([https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)) を開きます。

1. ナビゲーションペインで、[**データベース**] を選択し、変更する DB インスタンスを選択します。

1. [**Modify**] を選択します。

1. [**ログのエクスポート**] セクションで、CloudWatch Logs に公開するログを選択します。

1. [**続行**] を選択し、概要ページで [**Modify DB Instance**] (DB インスタンスの変更) を選択します。

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

 AWS CLI を使用して MySQL ログを発行することができます。以下のパラメータを使用して、[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` オプションへの変更は常に DB インスタンスに即時適用されます。それで、`--apply-immediately` と `--no-apply-immediately` オプションは効果がありません。

以下の AWS CLI コマンドを呼び出すことで MySQL ログを発行することもできます。
+ [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)

以下のオプションを使用して、この AWS CLI コマンドの 1 つを実行します。
+ `--db-instance-identifier`
+ `--enable-cloudwatch-logs-exports`
+ `--db-instance-class`
+ `--engine`

実行する AWS CLI コマンドに応じて、他のオプションが必要となる場合があります。

**Example**  
次の例では、ログファイルが CloudWatch Logs に発行されるよう既存の MySQL DB インスタンスを変更します。`--cloudwatch-logs-export-configuration` 値は JSON オブジェクトです。このオブジェクトのキーは `EnableLogTypes` であり、値は `audit`、`error`、`general`、および `slowquery` を任意に組み合わせた文字列の配列です。  
Linux、macOS、Unix の場合:  

```
1. aws rds modify-db-instance \
2.     --db-instance-identifier mydbinstance \
3.     --cloudwatch-logs-export-configuration '{"EnableLogTypes":["audit","error","general","slowquery"]}'
```
Windows の場合:  

```
1. aws rds modify-db-instance ^
2.     --db-instance-identifier mydbinstance ^
3.     --cloudwatch-logs-export-configuration '{"EnableLogTypes":["audit","error","general","slowquery"]}'
```

**Example**  
次の例では、MySQL DB インスタンスを作成してログファイルを CloudWatch Logs に発行します。`--enable-cloudwatch-logs-exports` 値は、JSON 形式の文字列の配列です。この文字列は `audit`、`error`、`general` および `slowquery` の任意の組み合わせです。  
Linux、macOS、Unix の場合:  

```
1. aws rds create-db-instance \
2.     --db-instance-identifier mydbinstance \
3.     --enable-cloudwatch-logs-exports '["audit","error","general","slowquery"]' \
4.     --db-instance-class db.m4.large \
5.     --engine MySQL
```
Windows の場合:  

```
1. aws rds create-db-instance ^
2.     --db-instance-identifier mydbinstance ^
3.     --enable-cloudwatch-logs-exports '["audit","error","general","slowquery"]' ^
4.     --db-instance-class db.m4.large ^
5.     --engine MySQL
```

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

RDS API を使用して MySQL ログを発行することができます。以下のパラメータを使用して、[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` パラメータへの変更は常に DB インスタンスに即時適用されます。それで、`ApplyImmediately` パラメータは効果がありません。

以下の RDS API オペレーションを呼び出すことで MySQL ログを発行することもできます。
+ [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 オペレーションの 1 つを実行します。
+ `DBInstanceIdentifier`
+ `EnableCloudwatchLogsExports`
+ `Engine`
+ `DBInstanceClass`

実行する AWS CLI コマンドに応じて、他のパラメータが必要となる場合があります。