You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python驱动连接Astra DB失败,求助解决网络不可达问题

问题描述

严格按照AstraDB Python驱动文档操作,代码如下:

cloud_config= {
        'secure_connect_bundle': 'secure-connect-test-warehouse.zip'
}

logging.basicConfig(level=logging.DEBUG)

auth_provider = PlainTextAuthProvider(my_id, my_secret)
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()

row = session.execute("select release_version from system.local").one()
if row:
    print(row[0])
else:
    print("An error occurred.")

执行后出现错误:

File "connect_database.py", line 23, in <module>
    session = cluster.connect()
  File "cassandra/cluster.py", line 1677, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1713, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1700, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 3507, in cassandra.cluster.ControlConnection.connect
  File "cassandra/cluster.py", line 3552, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers
OSError(101, "Tried connecting to [(...', ..., 0, 0)]. Last error: Network is unreachable")})

已尝试将control_connection_timeout从默认2.0调整为10,问题仍未解决,求解决方案。

解决方案
  • 检查Secure Connect Bundle文件
    确认secure-connect-test-warehouse.zip路径正确,文件未损坏。可重新从AstraDB控制台下载最新的bundle文件替换原文件,下载时确保文件完整。

  • 验证认证信息正确性
    确认my_id和my_secret是AstraDB的客户端ID和客户端密钥,而非用户名密码。可在AstraDB控制台的「Connect」页面获取正确凭据。

  • 排查网络限制

    • 检查本地防火墙、杀毒软件是否拦截Python程序的网络请求,临时关闭防火墙测试连接。
    • 确认所在网络未屏蔽443端口(AstraDB使用该端口通信,需确保出站443端口开放)。
    • 若在企业网络环境,联系IT部门确认是否有代理或准入规则限制外部连接,必要时配置代理:
      cloud_config= {
              'secure_connect_bundle': 'secure-connect-test-warehouse.zip',
              'proxy_url': 'http://your-proxy-host:port'
      }
      
  • 检查驱动版本兼容性
    确认cassandra-driver版本与AstraDB兼容,建议安装4.15.x及以上版本,执行以下命令升级:

    pip install --upgrade cassandra-driver
    
  • 调整连接参数并启用重试
    同时设置connect_timeout和control_connection_timeout,并配置重试策略:

    from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
    from cassandra.auth import PlainTextAuthProvider
    from cassandra.policies import DowngradingConsistencyRetryPolicy
    
    profile = ExecutionProfile(
        retry_policy=DowngradingConsistencyRetryPolicy(),
        connect_timeout=15,
        control_connection_timeout=15
    )
    
    cloud_config= {
            'secure_connect_bundle': 'secure-connect-test-warehouse.zip'
    }
    
    auth_provider = PlainTextAuthProvider(my_id, my_secret)
    cluster = Cluster(
        cloud=cloud_config,
        auth_provider=auth_provider,
        execution_profiles={EXEC_PROFILE_DEFAULT: profile}
    )
    session = cluster.connect()
    

内容的提问来源于stack exchange,提问作者Amine SAIHI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:07:34