0

目前我正在通过这个 youtube 链接 https://www.youtube.com/watch?v=NyDT3KkscSk&t=2439s了解 Astra DB

我设法从 Astra DB 下载 connection.zip 文件并生成管理令牌密钥。但是当我尝试进行连接时,例如:

   from app.crud import create_entry

我会收到这个错误:

raise NoHostAvailable("Unable to connect to any servers", errors)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:611a8370-f129-4099-84e2-c3b2f426ebdc': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:611a8370-f129-4099-84e2-c3b2f426ebdc: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:040ab116-8c77-4eb4-a357-c9bdcbb637d4': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:040ab116-8c77-4eb4-a357-c9bdcbb637d4: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:536e6e99-ef4e-47d0-9308-b0c6cdf4aa37': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:536e6e99-ef4e-47d0-9308-b0c6cdf4aa37: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"')})

这是我的 db.py:

import os
import pathlib
from dotenv import load_dotenv
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.cqlengine.connection import register_connection, set_default_connection


BASE_DIR = pathlib.Path(__file__).parent
CLUSTER_BUNDLE = BASE_DIR / 'ignored'/ 'connect.zip'

load_dotenv()

astra_db_client_id = os.environ.get('ASTRA_DB_CLIENT_ID')
astra_db_client_secret = os.environ.get('ASTRA_DB_CLIENT_SECRET')

def get_cluster():
    cloud_config= {
            'secure_connect_bundle': CLUSTER_BUNDLE
    }
    auth_provider = PlainTextAuthProvider(astra_db_client_id, astra_db_client_secret)
    cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider, control_connection_timeout=30,
connect_timeout=30)
    return cluster


def get_session():
    cluster = get_cluster()
    session = cluster.connect()
    register_connection(str(session), session=session)
    set_default_connection(str(session))
    return session

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

我多次尝试重新创建令牌密钥并重新下载驱动程序,但我仍然没有运气在这里传递错误的凭据错误:

我的crud.py

from .db import get_session
from .models import Product
from cassandra.cqlengine.management import sync_table

session = get_session()
sync_table(Product)


def create_entry(data:dict):
    return Product.create(**data)

模型.py

from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model


class Product(Model): # -> table
    __keyspace__ = "testing" #
    asin = columns.Text(primary_key=True, required=True)
    title = columns.Text()
4

1 回答 1

1

你可能想看看这个

https://docs.datastax.com/en/astra/docs/docs/using-the-datastax-python-driver-to-connect-to-your-database.html

特别是这里的部​​分:

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

cloud_config= {
        'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
}
auth_provider = PlainTextAuthProvider('username', 'password')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()

创建连接时,您需要传递安全连接包 zip。然后,您将提供 clientId 和 clientSecret 作为您下载的连接文件中的用户名和密码。

于 2021-12-13T07:36:50.383 回答