

# 将 Amazon Aurora MySQL 日志发布到 Amazon CloudWatch Logs
<a name="AuroraMySQL.Integrating.CloudWatch"></a>

您可以配置 Aurora MySQL 数据库集群以将一般、慢速、审核和错误日志数据发布到 Amazon CloudWatch Logs 中的日志组。利用 CloudWatch Logs，可以对日志数据进行实时分析并使用 CloudWatch 创建警报和查看指标。您可以使用 CloudWatch Logs 在高持久性存储中存储日志记录。

要将日志发布到 CloudWatch Logs，必须启用相应日志。默认情况下启用错误日志，但您必须明确启用其他类型的日志。有关在 MySQL 中启用日志的信息，请参阅 MySQL 文档中的[选择一般查询和慢速查询日志输出目标](https://dev.mysql.com/doc/refman/8.0/en/log-destinations.html)。有关启用 Aurora MySQL 审核日志的更多信息，请参阅 [启用高级审核](AuroraMySQL.Auditing.md#AuroraMySQL.Auditing.Enable)。

**注意**  
如果禁用了导出日志数据，则 Aurora 不会删除现有日志组或日志流。如果禁用了导出日志数据，现有日志数据在 CloudWatch Logs 中保持可用 (具体取决于日志保留)，并且您仍将产生存储审核日志数据的费用。您可以使用 CloudWatch Logs 控制台、AWS CLI 或 CloudWatch Logs API 删除日志流和日志组。
将审计日志发布到 CloudWatch Logs 的一种替代方法是启用高级审计，然后创建一个自定义数据库集群参数组并将 `server_audit_logs_upload` 参数设置为 `1`。`server_audit_logs_upload` 数据库集群参数的默认值为 `0`。有关启用高级审计的信息，请参阅[在 Amazon Aurora MySQL 数据库集群中使用高级审计](AuroraMySQL.Auditing.md)。  
如果使用该替代方法，您必须具有 IAM 角色以访问 CloudWatch Logs，并将 `aws_default_logs_role` 集群级参数设置为该角色的 ARN。有关创建 角色的信息，请参阅[设置 IAM 角色以访问AWS服务](AuroraMySQL.Integrating.Authorizing.IAM.md)。不过，如果您具有 `AWSServiceRoleForRDS` 服务相关角色，它提供 CloudWatch Logs 的访问权限并覆盖所有自定义角色。有关 Amazon RDS 的服务相关角色的信息，请参阅 [将服务相关角色用于 Amazon Aurora](UsingWithRDS.IAM.ServiceLinkedRoles.md)。
如果您不希望将审核日志导出到 CloudWatch Logs，请确保导出审核日志的所有方法都已禁用。这些方法包括 AWS 管理控制台、AWS CLI、RDS API 和 `server_audit_logs_upload` 参数。
 对于 Aurora Serverless v1 数据库集群而言，该过程与包含预调配或 Aurora Serverless v2 数据库实例的数据库集群略有不同。Aurora Serverless v1 集群会自动上传您通过配置参数启用的所有日志。  
因此，您将通过在数据库集群参数组中打开和关闭不同的日志类型，来启用或禁用 Aurora Serverless v1 数据库集群的日志上传。您不能通过 AWS 管理控制台、AWS CLI 或 RDS API 修改集群本身的设置。有关打开和关闭 Aurora Serverless v1 集群的 MySQL 日志的信息，请参阅 [Aurora Serverless v1 的参数组](aurora-serverless-v1.how-it-works.md#aurora-serverless.parameter-groups)。

## 控制台
<a name="AuroraMySQL.Integrating.CloudWatch.Console"></a>

您可以使用控制台将预配置集群的 Aurora MySQL 日志发布到 CloudWatch Logs。

**从控制台发布 Aurora MySQL 日志**

1. 通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Databases (数据库)**。

1. 选择要为其发布日志数据的 Aurora MySQL 数据库集群。

1. 选择 **Modify (修改)**。

1. 在 **Log exports (日志导出)** 部分中，选择要开始发布到 CloudWatch Logs 的日志。

1. 选择**继续**，然后选择摘要页面上的 **修改数据库集群**。

## AWS CLI
<a name="AuroraMySQL.Integrating.CloudWatch.CLI"></a>

您可以使用 AWS CLI 发布预置集群的 Aurora MySQL 日志。要这样做，您可以使用以下选项运行 [modify-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-cluster.html) AWS CLI 命令：
+ `--db-cluster-identifier`—数据库集群标识符。
+ `--cloudwatch-logs-export-configuration` — 为数据库集群导出到 CloudWatch Logs 而启用的日志类型的配置设置。

您还可以运行以下 AWS CLI 命令之一以发布 Aurora MySQL 日志：
+ [create-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-cluster.html)
+ [restore-db-cluster-from-s3](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-s3.html)
+ [restore-db-cluster-from-snapshot](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-snapshot.html)
+ [restore-db-cluster-to-point-in-time](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-to-point-in-time.html)

通过以下选项运行上述 AWS CLI 命令之一：
+ `--db-cluster-identifier`—数据库集群标识符。
+ `--engine` — 数据库引擎。
+ `--enable-cloudwatch-logs-exports` — 为数据库集群导出到 CloudWatch Logs 而启用的日志类型的配置设置。

根据您运行的 AWS CLI 命令，可能需要其他选项。

**Example**  
以下命令修改现有的 Aurora MySQL 数据库集群以将日志文件发布到 CloudWatch Logs。  
对于 Linux、macOS 或 Unix：  

```
1. aws rds modify-db-cluster \
2.     --db-cluster-identifier mydbcluster \
3.     --cloudwatch-logs-export-configuration '{"EnableLogTypes":["error","general","slowquery","audit","instance"]}'
```
对于 Windows：  

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

**Example**  
以下命令创建一个 Aurora MySQL 数据库集群以将日志文件发布到 CloudWatch Logs。  
对于 Linux、macOS 或 Unix：  

```
1. aws rds create-db-cluster \
2.     --db-cluster-identifier mydbcluster \
3.     --engine aurora \
4.     --enable-cloudwatch-logs-exports '["error","general","slowquery","audit","instance"]'
```
对于 Windows：  

```
1. aws rds create-db-cluster ^
2.     --db-cluster-identifier mydbcluster ^
3.     --engine aurora ^
4.     --enable-cloudwatch-logs-exports '["error","general","slowquery","audit","instance"]'
```

## RDS API
<a name="AuroraMySQL.Integrating.CloudWatch.API"></a>

您可以使用 RDS API 发布预配置集群的 Aurora MySQL 日志。要这样做，您可以使用以下选项运行 [ModifyDBCluster](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBCluster.html) 操作：
+ `DBClusterIdentifier`—数据库集群标识符。
+ `CloudwatchLogsExportConfiguration` — 为数据库集群导出到 CloudWatch Logs 而启用的日志类型的配置设置。

您还可以通过运行以下 RDS API 操作之一，使用 RDS API 发布 Aurora MySQL 日志：
+ [CreateDBCluster](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html)
+ [RestoreDBClusterFromS3](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterFromS3.html)
+ [RestoreDBClusterFromSnapshot](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterFromSnapshot.html)
+ [RestoreDBClusterToPointInTime](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterToPointInTime.html)

运行带以下参数的 RDS API 操作：
+ `DBClusterIdentifier`—数据库集群标识符。
+ `Engine` — 数据库引擎。
+ `EnableCloudwatchLogsExports` — 为数据库集群导出到 CloudWatch Logs 而启用的日志类型的配置设置。

根据您运行的 AWS CLI 命令，可能需要其他参数。

## 在 Amazon CloudWatch 中监控日志事件
<a name="AuroraMySQL.Integrating.CloudWatch.Monitor"></a>

在启用 Aurora MySQL 日志事件后，您可以在 Amazon CloudWatch Logs 中监控事件。将自动使用以下前缀为 Aurora 数据库集群创建新的日志组，其中 `cluster-name` 表示数据库集群名称，`log_type` 表示日志类型。

```
/aws/rds/cluster/cluster-name/log_type
```

例如，如果您将导出功能配置为包括名为 `mydbcluster` 的数据库集群的慢速查询日志，则慢速查询数据将存储在 `/aws/rds/cluster/mydbcluster/slowquery` 日志组中。

将使用不同的日志流将来自数据库集群中的所有数据库实例的事件推送到一个日志组。该行为取决于以下哪些条件为真：
+ 存在包含指定名称的日志组。

  Aurora 使用现有日志组为集群导出日志数据。您可以使用自动化配置（例如 AWS CloudFormation）创建具有预定义日志保留期、指标筛选条件和客户访问权限的日志组。
+ 具有指定名称的日志组不存在。

  当在实例的日志文件中检测到匹配的日志条目时，Aurora MySQL 会自动在 CloudWatch Logs 中创建一个新的日志组。日志组使用**永不过期**的默认日志保留期。

  要更改日志保留期，请使用 CloudWatch Logs 控制台、AWS CLI 或 CloudWatch Logs API。有关在 CloudWatch Logs 中更改日志保留期的更多信息，请参阅[在 CloudWatch Logs 中更改日志数据保留期](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SettingLogRetention.html)。

要在数据库集群的日志事件中搜索信息，请使用 CloudWatch Logs 控制台、AWS CLI 或 CloudWatch Logs API。有关搜索和筛选日志数据的更多信息，请参阅[搜索和筛选日志数据](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html)。