

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon Keyspaces 中的连接错误故障排除
<a name="troubleshooting.connecting"></a>

连接时遇到问题？ 以下介绍一些常见问题以及如何解决这些问题。

## 连接到 Amazon Keyspaces 端点时出错
<a name="troubleshooting-connecting"></a>

连接失败和连接错误会导致不同的错误信息。以下部分涵盖了最常见的场景。

**Topics**
+ [我无法使用 cqlsh 连接到 Amazon Keyspaces](#troubleshooting.connection.cqlsh)
+ [我无法使用 Cassandra 客户端驱动程序连接 Amazon Keyspaces](#troubleshooting.connection.driver)

### 我无法使用 cqlsh 连接到 Amazon Keyspaces
<a name="troubleshooting.connection.cqlsh"></a>

**您尝试使用 cqlsh 连接到 Amazon Keyspaces 端点，但连接失败并显示 `Connection error`。**

如果您尝试连接到 Amazon Keyspaces 表，但 cqlsh 配置不正确，连接将失败。以下部分举例说明了在使用 cqlsh 尝试建立连接时导致连接错误的最常见配置问题。

**注意**  
如果您尝试从 VPC 连接到 Amazon Keyspaces，需要额外的权限。要使用 VPC 端点成功配置连接，请按照[教程：使用接口 VPC 终端节点连接到 Amazon Keyspaces](vpc-endpoints-tutorial.md) 中的步骤操作。

**您尝试使用 cqlsh 连接到 Amazon Keyspaces，但出现连接 `timed out` 错误。**

如果您没有提供正确的端口，则可能会出现这种情况，从而导致以下错误。

```
#  cqlsh cassandra.us-east-1.amazonaws.com 9140 -u "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.199': error(None, "Tried connecting to [('3.234.248.199', 9140)]. Last error: timed out")})
```

要解决此问题，请确认使用端口 9142 进行连接。

**您尝试使用 cqlsh 连接到 Amazon Keyspaces，但出现 `Name or service not known` 错误。**

如果您使用的端点拼写错误或不存在，则可能出现这种情况。在以下示例中，端点名称存在拼写错误。

```
#  cqlsh cassandra.us-east-1.amazon.com 9142 -u "USERNAME" -p "PASSWORD" --ssl
Traceback (most recent call last):
  File "/usr/bin/cqlsh.py", line 2458, in >module>
    main(*read_options(sys.argv[1:], os.environ))
  File "/usr/bin/cqlsh.py", line 2436, in main
    encoding=options.encoding)
  File "/usr/bin/cqlsh.py", line 484, in __init__
    load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
  File "/usr/share/cassandra/lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/policies.py", line 417, in __init__
socket.gaierror: [Errno -2] Name or service not known
```

要在使用公共端点连接时解决此问题，请从 [Amazon Keyspaces 的服务端点](programmatic.endpoints.md)中选择一个可用的端点，并确认该端点的名称没有任何错误。如果使用 VPC 端点进行连接，请验证 cqlsh 配置中的 VPC 端点信息是否正确。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `OperationTimedOut` 错误。**

Amazon Keyspaces 要求为连接启用 SSL，以确保强大的安全性。如果您收到以下错误，则可能是缺少 SSL 参数。

```
# cqlsh cassandra.us-east-1.amazonaws.com -u "USERNAME" -p "PASSWORD"
Connection error: ('Unable to connect to any servers', {'3.234.248.192': OperationTimedOut('errors=Timed out creating connection (5 seconds), last_host=None',)})
#
```

要解决此问题，请将以下标志添加到 cqlsh 连接命令。

```
--ssl
```

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `SSL transport factory requires a valid certfile to be specified` 错误。**

在这种情况下， SSL/TLS 证书的路径丢失，这会导致以下错误。

```
# cat .cassandra/cqlshrc
[connection]
port = 9142
factory = cqlshlib.ssl.ssl_transport_factory
#


# cqlsh cassandra.us-east-1.amazonaws.com -u "USERNAME" -p "PASSWORD" --ssl
Validation is enabled; SSL transport factory requires a valid certfile to be specified. Please provide path to the certfile in [ssl] section as 'certfile' option in /root/.cassandra/cqlshrc (or use [certfiles] section) or set SSL_CERTFILE environment variable.
#
```

要解决此问题，请添加证书文件在计算机上的路径。有关更多信息，请参阅 [如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

```
certfile =  path_to_file/keyspaces-bundle.pem
```

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `No such file or directory` 错误。**

如果计算机上证书文件的路径错误，可能出现这种情况，从而导致以下错误。

```
# cat .cassandra/cqlshrc
[connection]
port = 9142
factory = cqlshlib.ssl.ssl_transport_factory

[ssl]
validate = true
certfile = /root/wrong_path/keyspaces-bundle.pem
#



# cqlsh cassandra.us-east-1.amazonaws.com -u "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.192': IOError(2, 'No such file or directory')})
#
```

要解决此问题，请验证计算机上的证书文件路径是否正确。有关更多信息，请参阅 [如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `[X509] PEM lib` 错误。**

如果 SSL/TLS 证书`pem`文件无效，则可能出现这种情况，从而导致以下错误。

```
# cqlsh cassandra.us-east-1.amazonaws.com -u "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.241': error(185090057, u"Tried connecting to [('3.234.248.241', 9142)]. Last error: [X509] PEM lib (_ssl.c:3063)")})
#
```

要解决此问题，请确保您已下载所需的数字证书。有关更多信息，请参阅 [如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `unknown` SSL 错误。**

如果 SSL/TLS 证书`pem`文件为空，则可能出现这种情况，从而导致以下错误。

```
# cqlsh cassandra.us-east-1.amazonaws.com -u "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.220': error(0, u"Tried connecting to [('3.234.248.220', 9142)]. Last error: unknown error (_ssl.c:3063)")})
#
```

要解决此问题，请确保您已下载所需的数字证书。您可以使用以下主题中的步骤进行确认[如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `SSL: CERTIFICATE_VERIFY_FAILED` 错误。**

如果无法验证 SSL/TLS 证书文件，则可能出现这种情况，从而导致以下错误。

```
Connection error: ('Unable to connect to any servers', {'3.234.248.223': error(1, u"Tried connecting to [('3.234.248.223', 9142)]. Last error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:727)")})
```

要解决此问题，请确保您已下载所需的数字证书。您可以使用以下主题中的步骤进行确认[如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

**您尝试使用 cqlsh 连接到 Amazon Keyspaces，但收到了 `Last error: timed out` 错误。**

如果您没有在 Amazon EC2 安全组中为 Amazon Keyspaces 配置出站规则，则可能会出现这种情况，从而导致以下错误。

```
# cqlsh cassandra.us-east-1.amazonaws.com 9142 -u  "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.206': error(None, "Tried connecting to [('3.234.248.206', 9142)]. Last error: timed out")})
#
```

要确认此问题是由于 Amazon EC2 实例的配置造成的`cqlsh`，而不是由 Amazon EC2 实例的配置引起的，您可以尝试使用连接您的密钥空间 AWS CLI，例如使用以下命令。

```
aws keyspaces list-tables --keyspace-name 'my_keyspace'
```

如果此命令也超时，则表示 Amazon EC2 实例的配置不正确。

要确认您是否有足够的权限访问 Amazon Keyspaces，您可以使用 AWS CloudShell 进行连接。`cqlsh`如果连接已建立，则需要配置 Amazon EC2 实例。

要解决此问题，请确认 Amazon EC2 实例具有允许流量流入 Amazon Keyspaces 的出站规则。如果情况并非如此，则需要为 EC2 实例创建一个新的安全组，并添加一条允许向 Amazon Keyspaces 资源发送出站流量的规则。要更新出站规则以允许流量流入 Amazon Keyspaces，请从**类型**下拉菜单中选择 **CQLSH/CASSANDRA**。

创建具有出站流量规则的新安全组后，您需要将其添加到实例中。选择实例，依次选择**操作**、**安全**和**更改安全组**。添加带有出站规则的新安全组，但要确保默认组仍然可用。

有关如何查看和编辑 EC2 出站规则的更多信息，请参阅《Amazon EC2 用户指南》中的[向安全组添加规则](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/working-with-security-groups.html#adding-security-group-rule)。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `Unauthorized` 错误。**

如果您在 IAM 用户策略中缺少 Amazon Keyspaces 权限，则可能会出现这种情况，从而导致以下错误。

```
# cqlsh cassandra.us-east-1.amazonaws.com 9142 -u  "testuser-at-12345678910" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.241': AuthenticationFailed('Failed to authenticate to 3.234.248.241: Error from server: code=2100 [Unauthorized] message="User arn:aws:iam::12345678910:user/testuser has no permissions."',)})
#
```

要解决此问题，请确保 IAM 用户 `testuser-at-12345678910` 有权访问 Amazon Keyspaces。有关授予 Amazon Keyspaces 访问权限的 IAM 策略的示例，请参阅 [Amazon Keyspaces 基于身份的策略示例](security_iam_id-based-policy-examples.md)。

有关专门针对 IAM 访问的故障排除指南，请参阅[Amazon Keyspaces 身份和访问问题排查](security_iam_troubleshoot.md)。

**您尝试使用 cqlsh 连接 Amazon Keyspaces，但收到 `Bad credentials` 错误。**

如果用户名或密码错误，则可能出现这种情况，从而导致以下错误。

```
# cqlsh cassandra.us-east-1.amazonaws.com 9142 -u  "USERNAME" -p "PASSWORD" --ssl
Connection error: ('Unable to connect to any servers', {'3.234.248.248': AuthenticationFailed('Failed to authenticate to 3.234.248.248: Error from server: code=0100 [Bad credentials] message="Provided username USERNAME and/or password are incorrect"',)})
#
```

要解决此问题，请验证代码*PASSWORD*中的*USERNAME*和是否与生成[服务专用凭证](programmatic.credentials.ssc.md)时获得的用户名和密码相匹配。

**重要**  
如果您在尝试使用 cqlsh 连接时仍然看到错误，请使用 `--debug` 选项重新运行命令，并在联系 支持时提供详细的输出。

### 我无法使用 Cassandra 客户端驱动程序连接 Amazon Keyspaces
<a name="troubleshooting.connection.driver"></a>

以下各节显示了使用 Cassandra 客户端驱动程序进行连接时最常见的错误。

**您正在尝试使用 DataStax Java 驱动程序连接到 Amazon Keyspaces 表，但收到错误`NodeUnavailableException`消息。**

如果尝试请求的连接中断，则会导致以下错误。

```
[com.datastax.oss.driver.api.core.NodeUnavailableException: No connection was available to Node(endPoint=vpce-22ff22f2f22222fff-aa1bb234.cassandra.us-west-2.vpce.amazonaws.com/11.1.1111.222:9142, hostId=1a23456b-c77d-8888-9d99-146cb22d6ef6, hashCode=123ca4567)]
```

要解决此问题，请找到检测信号值，如果检测信号值较高，则将其降低到 30 秒。

```
advanced.heartbeat.interval = 30 seconds
```

然后查找相关的超时时间，并确保将该值设置为至少 5 秒。

```
advanced.connection.init-query-timeout = 5 seconds
```

**您尝试使用驱动程序和 SigV4 插件连接 Amazon Keyspaces，但收到 `AttributeError` 错误。**

如果证书配置不正确，则会导致以下错误。

```
cassandra.cluster.NoHostAvailable: (‘Unable to connect to any servers’,
 {‘44.234.22.154:9142’: AttributeError(“‘NoneType’ object has no attribute ‘access_key’“)})
```

要解决此问题，请验证您在使用 Sigv4 插件时传递了与 IAM 用户或角色关联的证书。SigV4 插件需要以下凭证。
+ `AWS_ACCESS_KEY_ID`— 指定与 IAM 用户或角色关联的 AWS 访问密钥。
+ `AWS_SECRET_ACCESS_KEY` - 指定与访问密钥关联的秘密密钥。这基本上是访问密钥的“密码”。

要了解有关访问密钥和 SigV4 插件的更多信息，请参阅[为 Amazon Keyspaces 创建和配置 AWS 证书](access.credentials.md)。

**您尝试使用驱动程序连接 Amazon Keyspaces 表，但收到 `PartialCredentialsError` 错误。**

如果缺少 `AWS_SECRET_ACCESS_KEY`，可能会导致以下错误。

```
cassandra.cluster.NoHostAvailable: (‘Unable to connect to any servers’, {‘44.234.22.153:9142’: 
 PartialCredentialsError(‘Partial credentials found in config-file, missing: aws_secret_access_key’)})
```

要解决此问题，请验证您在使用 SigV4 插件时传递了 `AWS_ACCESS_KEY_ID` 和 `AWS_SECRET_ACCESS_KEY`。要了解有关访问密钥和 SigV4 插件的更多信息，请参阅[为 Amazon Keyspaces 创建和配置 AWS 证书](access.credentials.md)。

**您尝试使用驱动程序连接 Amazon Keyspaces 表，但收到 `Invalid signature` 错误。**

如果会话中签名所需的任何组件错误或定义不正确，则可能出现这种情况。
+ `AWS_ACCESS_KEY_ID`
+ `AWS_SECRET_ACCESS_KEY`
+ `AWS_DEFAULT_REGION`

以下错误是无效访问密钥的示例。

```
cassandra.cluster.NoHostAvailable: (‘Unable to connect to any servers’, {‘11.234.11.234:9142’: 
 AuthenticationFailed(‘Failed to authenticate to 11.234.11.234:9142: Error from server: code=0100 
 [Bad credentials] message=“Authentication failure: Invalid signature”’)})
```

要解决此问题，请验证访问密钥和 sigV4 插件的配置 AWS 区域 是否正确，以访问 Amazon Keyspaces。要了解有关访问密钥和 SigV4 插件的更多信息，请参阅[为 Amazon Keyspaces 创建和配置 AWS 证书](access.credentials.md)。

#### 我的 VPC 端点连接无法正常工作
<a name="troubleshooting.connection.vpce"></a>

**您尝试使用 VPC 端点连接 Amazon Keyspaces，但收到了令牌映射错误或吞吐量低的问题。**

如果 VPC 端点连接配置不正确，则可能出现这种情况。

要解决这些问题，请验证以下配置详细信息。要按照 step-by-step教程学习如何通过接口 VPC 终端节点为 Amazon Keyspaces 配置连接，请参阅。[教程：使用接口 VPC 终端节点连接到 Amazon Keyspaces](vpc-endpoints-tutorial.md)

1. 确认用于连接到 Amazon Keyspaces 的 IAM 实体拥有 read/write 用户表的访问权限和对系统表的读取权限，如以下示例所示。

   ```
   {
      "Version":"2012-10-17",		 	 	 
      "Statement":[
         {
            "Effect":"Allow",
            "Action":[
               "cassandra:Select",
               "cassandra:Modify"
            ],
            "Resource":[
               "arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/table/mytable",
               "arn:aws:cassandra:us-east-1:111122223333:/keyspace/system*"
            ]
         }
      ]
   }
   ```

1. 确认用于连接 Amazon Keyspaces 的 IAM 实体具有访问 Amazon EC2 实例上 VPC 端点信息所需的读取权限，如以下示例所示。

   ```
   {
      "Version":"2012-10-17",		 	 	 
      "Statement":[
         {
            "Sid":"ListVPCEndpoints",
            "Effect":"Allow",
            "Action":[
               "ec2:DescribeNetworkInterfaces",
               "ec2:DescribeVpcEndpoints"
            ],
            "Resource":"*"
         }
      ]
   }
   ```
**注意**  
托管式策略 `AmazonKeyspacesReadOnlyAccess_v2` 和 `AmazonKeyspacesFullAccess` 包含允许 Amazon Keyspaces 访问 Amazon EC2 实例以读取可用接口 VPC 端点信息所需的权限。

   有关接口 VPC 端点的更多信息，请参阅[将接口 VPC 端点用于 Amazon Keyspaces](vpc-endpoints.md#using-interface-vpc-endpoints)

1. 确认 Java 驱动程序的 SSL 配置将主机名验证设置为 false，如本示例所示。

   ```
   hostname-validation = false
   ```

   有关驱动程序配置的更多信息，请参阅[步骤 2：配置驱动程序](using_java_driver.md#java_tutorial.driverconfiguration)。

1. 要确认 VPC 端点的配置是否正确，可以在 VPC *内*运行以下语句。
**注意**  
您不能使用本地开发人员环境或 Amazon Keyspaces CQL 编辑器来确认此配置，因为它们使用的是公共端点。

   ```
   SELECT peer FROM system.peers;
   ```

   输出应与本示例类似，从 IPv4 网络连接时返回 2 到 6 个带有私有 IPv4 地址的节点，具体取决于您的 VPC 设置和 AWS 区域。

   ```
   peer
   ---------------
    192.0.2.0.15
    192.0.2.0.24
    192.0.2.0.13
    192.0.2.0.7
    192.0.2.0.8
   
   (5 rows)
   ```

#### 我无法使用 `cassandra-stress` 进行连接
<a name="troubleshooting.connection.cassandra-stress"></a>

**您尝试使用 `cassandra-stress` 命令连接 Amazon Keyspaces，但收到 `SSL context` 错误。**

如果您尝试连接 Amazon Keyspaces 但未正确设置 trustStore，则会发生这种情况。Amazon Keyspaces 要求使用传输层安全性协议 (TLS) 来帮助保护与客户端的连接。

在这种情况下，您看到以下错误。

```
Error creating the initializing the SSL Context
```

要解决此问题，请按照[开始前的准备工作](using_java_driver.md#using_java_driver.BeforeYouBegin)主题中所示的说明设置 trustStore。

设置 trustStore 后，您应该能够使用以下命令进行连接。

```
./cassandra-stress user profile=./profile.yaml n=100 "ops(insert=1,select=1)" cl=LOCAL_QUORUM -node "cassandra.eu-north-1.amazonaws.com" -port native=9142 -transport ssl-alg="PKIX" truststore="./cassandra_truststore.jks" truststore-password="trustStore_pw" -mode native cql3 user="user_name" password="password"
```

#### 我无法使用 IAM 身份进行连接
<a name="troubleshooting.connection.IAM"></a>

**您尝试使用 IAM 身份连接 Amazon Keyspaces 表，但收到 `Unauthorized` 错误。**

如果您尝试使用 IAM 身份（例如 IAM 用户）连接 Amazon Keyspaces 表，但没有执行策略并先为用户提供所需的权限，就会发生这种情况。

在这种情况下，您看到以下错误。

```
Connection error: ('Unable to connect to any servers', {'3.234.248.202': AuthenticationFailed('Failed to authenticate to 3.234.248.202: 
Error from server: code=2100 [Unauthorized] message="User arn:aws:iam::1234567890123:user/testuser has no permissions."',)})
```

要解决此问题，请验证 IAM 用户的权限。要连接标准驱动程序，用户必须至少具有对系统表的`SELECT`访问权限，因为大多数驱动程序在建立连接 keyspaces/tables 时都会读取系统。

有关授予 Amazon Keyspaces 系统和用户表访问权限的 IAM 策略示例，请参阅[访问 Amazon Keyspaces 表](security_iam_id-based-policy-examples.md#security_iam_id-based-policy-examples-access-one-table)。

要查看专门针对 IAM 的故障排除部分，请参阅[Amazon Keyspaces 身份和访问问题排查](security_iam_troubleshoot.md)。

#### 我尝试使用 cqlsh 导入数据，但与 Amazon Keyspaces 表的连接断开
<a name="troubleshooting.connection.import"></a>

**您尝试使用 cqlsh 将数据上传到 Amazon Keyspaces，但收到连接错误。**

cqlsh 客户端从服务器连续收到三个任意类型的错误后，与 Amazon Keyspaces 的连接失败。cqlsh 客户端失败时显示以下消息。

```
Failed to import 1 rows: NoHostAvailable - , will retry later, attempt 3 of 100
```

要解决此错误，您需要确保要导入的数据与 Amazon Keyspaces 中的表模式相匹配。查看导入文件中是否存在解析错误。您可以尝试通过 INSERT 语句来使用单行数据，从而隔离错误。

客户端会自动尝试重新建立连接。