【问题标题】:In Python, Is it possible to connect Azure SQL Server using Active Directory Password Authentication?在 Python 中,是否可以使用 Active Directory 密码身份验证连接 Azure SQL Server?
【发布时间】:2018-12-31 16:52:11
【问题描述】:

我尝试使用pyodbc库连接sql server,

driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+host+';PORT=1443;DATABASE='+database+';UID='+user+';PWD='+ password+';Authentication=ActiveDirectoryPassword')

在上面的代码中,我选择 ActiveDirectoryPassword(AAD) 作为我的身份验证类型,因为我只有 AAD 访问权限。但是收到以下错误消息。

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

当我将驱动程序更改为 ODBC Driver 13 时,收到不同的错误消息。

driver= '{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+host+';PORT=1443;DATABASE='+database+';UID='+user+';PWD='+ password+';Authentication=ActiveDirectoryPassword')

这次我收到以下错误消息。

OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The Microsoft Online Services Sign-In Assistant could not be found. Install it from http://go.microsoft.com/fwlink/?LinkId=234947. If it is already present, repair the installation. [2].  (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection (2); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Invalid connection string attribute (0)')

是否可以使用带有 AAD 身份验证的 pyodbc 连接 azure sql server?如果可能,使用 AAD 对 sql server 进行身份验证的正确方法是什么?

【问题讨论】:

    标签: python sql-server azure pyodbc


    【解决方案1】:

    首先,根据官方文档Use Azure Active Directory Authentication for authentication with SQLAdditional considerations小节,pyodbc可以通过AAD认证连接Azure SQL数据库。

    ODBC version 13.1 支持 Azure Active Directory 身份验证,但是 bcp.exe 无法使用 Azure Active Directory 身份验证进行连接,因为它使用的是较旧的 ODBC 提供程序。

    然后,有一个 SQL Server 的官方文档Using Azure Active Directory with the ODBC Driver 介绍了如何在 Windows 上使用以及一些限制。

    Microsoft ODBC Driver for SQL Server 13.1 或更高版本允许 ODBC 应用程序使用 Azure Active Directory 中的联合身份和用户名/密码连接到 SQL Azure 实例、Azure Active Directory 访问令牌或 Windows 集成身份验证(仅限 Windows 驱动程序)。对于 ODBC 驱动程序版本 13.1,Azure Active Directory 访问令牌身份验证仅适用于 Windows。 ODBC 驱动程序版本 17 及更高版本支持跨所有平台(Windows、Linux 和 Mac)的此身份验证。

    然后

    通过使用新的 DSN 和连接字符串关键字以及连接属性来实现。

    您需要按照文档配置一些设置以启用新的 DSN,然后使用连接字符串连接 SQL Azure。

    Driver={ODBC Driver 13 for SQL Server};Server={server};UID=myuser;PWD=myPass;Authentication=ActiveDirectoryPassword
    

    并注意这一点。

    要使用 Azure Active Directory 帐户用户名和密码进行连接,请在连接字符串中指定 Authentication=ActiveDirectoryPassword 以及 UIDPWD 关键字分别使用用户名和密码。

    同时,作为其他资源,这些似乎是旧的,但在 SQL Azure 中配置多因素身份验证详细介绍了blog,并为Connecting to SQL Database By Using Azure Active Directory Authentication 介绍了降价文档。

    另外,对于 Linux 和 Mac,有一个blog 申请另一个可能有助于建立 SQL Server 连接的解决方案。

    希望对你有帮助。

    【讨论】:

    • 感谢您提供有关 AAD 身份验证的更多信息。我尝试在本地系统上创建 DSN 文件。但收到与上述相同的错误消息。 SQLState: '08001' SQL Server Error: 2 [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The Microsoft Online Services Sign-In Assistant could not be found. Install it from http://go.microsoft.com/fwlink/?LinkId=234947. If it is already present, repair the installation. [2].
    • 通过 SSMS 我能够使用 AAD 身份验证访问数据库,但不能从 pyodbc 访问数据库
    • 安装windows登录助手后错误消失,现在可以获取连接对象了。这是我找到的参考资料link
    • 但不知道为什么我需要那个额外的插件
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多