无法使用 Active Directory 身份验证从 python 连接到 SQL 服务器 [英] Cannot connect to SQL server from python using Active Directory Authentication

查看:44
本文介绍了无法使用 Active Directory 身份验证从 python 连接到 SQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pymssql 库将 python 连接到 Sql Server.我可以使用 windows/sql 服务器身份验证进行连接.我想使用 Active Directory 身份验证进行连接.

I am using pymssql library to connect python to Sql Server. I can connect using windows/sql server authentication. I want to connect using Active Directory Authentication.

尝试了以下连接字符串.但它失败并出现错误:

Tried the below connection string. But it fails with error :

unexpected keyword authentication


conn = pymssql.connect(server='adventureworks.database.windows.net', authentication = 'Active Directory Password',user='username@server.com',password='Enterpasswordhere', database='dbo')

推荐答案

更新 2019-11-01:登录助手"似乎是 Windows Essentials 的一部分,它已已停产,不再可供下载.

Update 2019-11-01: The "sign-in assistant" appears to be part of Windows Essentials, which has been discontinued and is no longer available for download.

如果您使用的是 Windows 计算机,则可以使用适用于 SQL Server 的 ODBC 驱动程序 13.1 和 Microsoft 登录助手执行此操作.

You can do this using ODBC Driver 13.1 for SQL Server and the Microsoft Sign-In assistant if you are on a Windows machine.

  1. 为 SQL Server 安装 ODBC 驱动程序 13.1
    https://www.microsoft.com/download/details.aspx?id=53339

安装登录助手(链接失效)
http://go.microsoft.com/fwlink/?LinkId=234947

测试连接

导入pyodbc
server = 以 windows.net 结尾的服务器名
数据库 = 数据库
用户名 = 用户名
密码 = 密码
表名 = 表名

driver='{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER=' + driver +
';PORT=1433;SERVER=' + 服务器 +
';PORT=1443;DATABASE='+数据库+
';UID='+用户名+
';PWD='+ 密码 +
';Authentication=ActiveDirectoryPassword')

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

cursor = cnxn.cursor()
cursor.execute("SELECT TOP 20 * from " + tablename)
row = cursor.fetchone()

cursor = cnxn.cursor()
cursor.execute("SELECT TOP 20 * from " + tablename)
row = cursor.fetchone()

同时行:

打印 str(row[0]) + " " + str(row[1])
row = cursor.fetchone()

print str(row[0]) + " " + str(row[1])
row = cursor.fetchone()

这篇关于无法使用 Active Directory 身份验证从 python 连接到 SQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆