

# IAM 数据库身份验证故障排除
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting"></a>

下面，您可以找到一些常见 IAM 数据库身份验证问题的故障排除思路，以及有关 IAM 数据库身份验证的 CloudWatch 日志和指标的信息。

## 将 IAM 数据库身份验证错误日志导出到 CloudWatch Logs
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.ErrorLogs"></a>

IAM 数据库身份验证错误日志存储在数据库主机上，您可以将这些日志导出到您的 CloudWatch Logs 账户。使用本页中的日志和补救方法来解决 IAM 数据库身份验证问题。

可以通过控制台、AWS CLI 和 RDS API 启用向 CloudWatch Logs 导出日志。有关控制台说明，请参阅 [将数据库日志发布到 Amazon CloudWatch Logs](USER_LogAccess.Procedural.UploadtoCloudWatch.md)。

要在从 AWS CLI 中创建数据库集群时将 IAM 数据库身份验证错误日志导出到 CloudWatch Logs，请使用以下命令：

```
aws rds create-db-cluster --db-cluster-identifier mydbinstance \
--region us-east-1 \
--engine postgres \
--engine-version 16 \
--master-username master \
--master-user-password password \
--publicly-accessible \
--enable-iam-database-authentication \
--enable-cloudwatch-logs-exports=iam-db-auth-error
```

要在从 AWS CLI 中修改数据库集群时将 IAM 数据库身份验证错误日志导出到 CloudWatch Logs，请使用以下命令：

```
aws rds modify-db-cluster --db-instance-identifier mydbcluster \
--region us-east-1 \
--cloudwatch-logs-export-configuration '{"EnableLogTypes":["iam-db-auth-error"]}'
```

要验证数据库集群是否正在将 IAM 数据库身份验证日志导出到 CloudWatch Logs，请检查 `describe-db-instances` 命令输出中的 `EnabledCloudwatchLogsExports` 参数是否设置为 `iam-db-auth-error`。

```
aws rds describe-db-cluster --region us-east-1 --db-cluster-identifier mydbcluster
            ...
            
             "EnabledCloudwatchLogsExports": [
                "iam-db-auth-error"
            ],
            ...
```

## IAM 数据库身份验证 CloudWatch 指标
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.CWMetrics"></a>

Amazon Aurora 向您的 Amazon CloudWatch 账户提供有关 IAM 数据库身份验证的近乎实时的指标。下表列出了使用 CloudWatch 时可用的 IAM 数据库身份验证指标：


| 指标 | 描述 | 
| --- | --- | 
|  `IamDbAuthConnectionRequests`  |  使用 IAM 数据库身份验证发出的连接请求总数。  | 
|  `IamDbAuthConnectionSuccess`  |  成功的 IAM 数据库身份验证请求总数。  | 
|  `IamDbAuthConnectionFailure`  |  失败的 IAM 数据库身份验证请求总数。  | 
|  `IamDbAuthConnectionFailureInvalidToken`  | 由于令牌无效而失败的 IAM 数据库身份验证请求总数。 | 
|  `IamDbAuthConnectionFailureInsufficientPermissions`  |  由于策略或权限不正确而失败的 IAM 数据库身份验证请求总数。  | 
|  `IamDbAuthConnectionFailureThrottling`  |  由于 IAM 数据库身份验证节流而失败的 IAM 数据库身份验证请求总数。  | 
|  `IamDbAuthConnectionFailureServerError`  |  由于 IAM 数据库身份验证功能中的内部服务器错误而失败的 IAM 数据库身份验证请求总数。  | 

## 常见问题和解决方案
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.IssuesSolutions"></a>

 使用 IAM 数据库身份验证时可能会遇到以下问题。使用表中的修复步骤来解决问题：


| 错误 | 指标 | 原因 | 解决方案 | 
| --- | --- | --- | --- | 
|  `[ERROR] Failed to authenticate the connection request for user db_user because the provided token is malformed or otherwise invalid. (Status Code: 400, Error Code: InvalidToken)`  |  `IamDbAuthConnectionFailure` `IamDbAuthConnectionFailureInvalidToken`  |  连接请求中的 IAM 数据库身份验证令牌要么不是有效的 SigV4a 令牌，要么格式不正确。  |  在应用程序中检查令牌生成策略。在某些情况下，请确保使用有效的格式传递令牌。截断令牌（或字符串格式不正确）将使令牌失效。  | 
|  `[ERROR] Failed to authenticate the connection request for user db_user because the token age is longer than 15 minutes. (Status Code: 400, Error Code:ExpiredToken)`  |  `IamDbAuthConnectionFailure` `IamDbAuthConnectionFailureInvalidToken`  |  IAM 数据库身份验证令牌已到期。令牌仅在 15 分钟内有效。  |  检查应用程序中的令牌缓存和/或令牌重用逻辑。您不应重用超过 15 分钟的令牌。  | 
|  `[ERROR] Failed to authorize the connection request for user db_user because the IAM policy assumed by the caller 'arn:aws:sts::123456789012:assumed-role/ <RoleName>/ <RoleSession>' is not authorized to perform `rds-db:connect` on the DB instance. (Status Code: 403, Error Code:NotAuthorized)`  |  `IamDbAuthConnectionFailure` `IamDbAuthConnectionFailureInsufficientPermissions`  |  该错误可能是由于以下原因引起的： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.Troubleshooting.html)  |  验证您在应用程序中采用的 IAM 角色和/或策略。请务必采用与连接到数据库相同的策略来生成令牌。  | 
|  `[ERROR] Failed to authorize the connection request for user db_user due to IAM DB authentication throttling. (Status Code: 429, Error Code: ThrottlingException)`  |  `IamDbAuthConnectionFailure` `IamDbAuthConnectionFailureThrottling`  | 您在短时间内向数据库发出的连接请求过多。IAM 数据库身份验证节流限制为每秒 200 个连接。 |  降低使用 IAM 身份验证建立新连接的速率。考虑使用 RDS 代理来实现连接池，以便在应用程序中重用已建立的连接。  | 
|  `[ERROR] Failed to authorize the connection request for user db_user due to an internal IAM DB authentication error. (Status Code: 500, Error Code: InternalError)`  |  `IamDbAuthConnectionFailure` `IamDbAuthConnectionFailureThrottling` |  使用 IAM 数据库身份验证授权数据库连接时出现内部错误。  |  请联系 https://aws.amazon.com/premiumsupport/ 以便调查此问题。  | 