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

Databricks中PySpark无法连接本地SQL但Python可行的问题求助

问题:Databricks搭配PySpark连接本地SQL Server失败,pymssql可正常连接

我在使用Databricks结合PySpark测试连接本地数据库时遇到问题:用Python的pymssql库能成功连接并获取数据,代码如下:

import pymssql
try:
    conn = pymssql.connect(
        host=host,
        user=user,
        password=password,
        database=db
    )
except Exception as e:
 print("An error occurred while connecting to SQL Server:", e)

但用PySpark的JDBC连接却始终失败,代码如下:

df = spark.read \
.format("jdbc") \
    .option("url", f"jdbc:sqlserver://{host}:1433;database={db};user={user};password={password}") \
    .option("dbtable", db) \
    .load()

两者用的是相同的凭据,我试过更换驱动、调整JAR包等方法,还是一直报错com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ...,怀疑是网络问题?有没有人遇到过类似情况?

解决方案

通过启用集成安全并指定NTLM认证方案,可解决该问题,调整后的代码如下:

integrated_security = 'true'
authentication_scheme = "NTLM"
url = f"jdbc:sqlserver://{host}:{port};database={nombre};integratedSecurity={integrated_security};authenticationScheme={authentication_scheme}"

query = f"(SELECT TOP 10 * FROM {table}) AS subquery"

jdbcDF = spark.read \
    .format("jdbc") \
    .option("url", url) \
    .option("dbtable", query) \
    .option("user", f'{user_solo}@{domain}') \
    .option("password", clave) \
    .load()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 14:17:12