我正在尝试使用 AzureOpenAI 和 CosmosDB 开发 RAG 我在 azure 中创建了一个用于 MongoDB (vCore) 的 Azure Cosmos DB。我正在尝试连接到数据库,但出现 ServerSelectionTimeoutError。
尝试通过下面的Python代码设置连接:
from urllib.parse import quote_plus
username = quote_plus("username")
password = quote_plus("password")
server = "service_name.mongocluster.cosmos.azure.com"
mongo_conn = f"mongodb+srv://{username}:{password}@{server}/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
mongo_client = pymongo.MongoClient(mongo_conn)
此代码成功运行,但出现以下警告:
C:\Users\AppData\Local\Temp\ipykernel_3292\24156620.py:9: UserWarning: You appear to be connected to a CosmosDB cluster. For
有关功能兼容性和支持的更多信息,请 访问 https://www.mongodb.com/supportability/cosmosdb mongo_client = pymongo.MongoClient(mongo_conn)
然后我尝试执行以下代码设置数据库和集合
# create a database called TryDB
db = mongo_client['TryDB']
# Create collection if it doesn't exist
COLLECTION_NAME = "Try_Collection"
collection = db[COLLECTION_NAME]
if COLLECTION_NAME not in db.list_collection_names():
# Creates a unsharded collection that uses the DBs shared throughput
db.create_collection(COLLECTION_NAME)
print("Created collection '{}'.\n".format(COLLECTION_NAME))
else:
print("Using collection: '{}'.\n".format(COLLECTION_NAME))
此代码块给了我以下错误:
服务器选择超时错误: c.service_name.mongocluster.cosmos.azure.com:10260:超时 (配置的超时:socketTimeoutMS:20000.0ms,connectTimeoutMS: 20000.0ms),超时:30s,拓扑描述:
]>
当我尝试从 MacBook 使用 MongoAPI (vCore) 连接到 Azure CosmosDB 时,我遇到了类似的问题。错误是
pymongo.errors.ServerSelectionTimeoutError: c.<account name>.mongocluster.cosmos.azure.com:10260: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 66806284a3eab282240d69eb, topology_type: Unknown, servers: [<ServerDescription ('c.<account name>.mongocluster.cosmos.azure.com', 10260) server_type: Unknown, rtt: None, error=AutoReconnect('c.<account_name>.cosmos.azure.com:10260: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>
但是在Windows上连接没有问题。在 MacBook 上,我双击“Applications/Python 3.12/”文件夹下的“Install Certificates.command”来解决此问题。我的Python版本是3.12.3。希望有帮助。