使用Fabric Notebook连接Azure SQL托管实例时连接不稳定求助
关于Azure SQL托管实例PySpark连接的权限疑问
问题背景
我正尝试通过PySpark代码在Microsoft Fabric Notebook中连接Azure SQL托管实例,连接时频繁遇到超时错误,但尝试15次中偶尔有2次能成功返回指定表的DataFrame。位于美国(SQL实例所在地)的项目架构师队友每次都能在2秒内成功连接。我位于印度,架构师已将我的公网IP加入白名单,全程使用指定Wi-Fi,尝试过指定VPN后问题依旧。想咨询:若存在权限访问问题,是否仍会出现15次中有2次连接成功的情况?
错误信息
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host db-sql-mi.public.e789gjshb.database.windows.net, port 3342 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall."
使用的PySpark代码
from pyspark.sql import SparkSession # spark = SparkSession.builder.appName("Python Spark SQL Server Example").getOrCreate() username = 'username' password = 'password' server_type = 'sqlserver' jdbc = 'db-sql-mi.public.e789gjshb.database.windows.net:3342' dbname = 'db-01' driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' table_name = 'tbl_name' schema = 'schema_name' jdbc_df = spark.read \ .format("jdbc") \ .option("url", f"jdbc:{server_type}://{jdbc};databaseName={dbname}") \ .option("dbtable", f"DBO.{table_name}") \ .option("user", username) \ .option("password",password) \ .option("driver", f"{driver}") \ .option("hostNameInCertificate","*e789gjshb.database.windows.net") \ .option("encrypt",True) \ .option("trustServerCertificate",False) \ .option("loginTimeout",180) \ .load() display(jdbc_df.limit(10))
问题解答
权限访问问题不会导致这种偶尔成功、大部分失败的情况。
如果是权限类问题(比如IP未正确加入白名单、用户名密码错误、数据库权限不足),连接结果会是稳定的:要么每次都因权限被拒(报错通常为"Login failed for user"或"IP not allowed"),要么每次都成功,不会出现随机成功的现象。
你遇到的超时问题更可能是跨区域网络不稳定导致的:
- 印度到美国的跨洋网络延迟高、丢包率波动大,偶尔网络条件较好时能建立连接,多数时候因超时失败
- 即便IP已在白名单,网络链路的拥堵、路由波动也会影响TCP连接的建立
- 若VPN仍走跨洋链路,同样会受网络状况影响
建议排查方向:
- 用
traceroute或tcpping工具测试你的网络到SQL实例公网地址的连通性,查看链路丢包和延迟情况 - 尝试进一步调大JDBC连接的超时参数(当前已设置
loginTimeout=180) - 询问架构师是否可使用Azure SQL托管实例的私有端点连接,规避公网跨区域链路的不稳定
内容的提问来源于stack exchange,提问作者Avinash Sekar
相关产品推荐
相关产品推荐

