2017-09-14 130 views
0

首先,我不是开发人员,因此如果这是多余或无用的问题,我表示歉意。使用Python连接到使用Active Directory通用身份验证的SQL Server数据库

在Microsoft SQL Server Management Studio中,我能够连接到使用Active Directory通用认证某个服务器,但我不能想出一个办法做到这一点使用Python。

当我与应用程序登录,我点击连接后,一个蓝色的登录屏幕上弹出,而我能够用我的用户名登录:[email protected]和密码。

然而,当我尝试使用pyodbc用下面的代码连接时,我得到的错误如下:

import pyodbc 

def main(): 
    serverName = "name.database.windows.net" 
    dbName = "DatabaseName" 

    connection = pyodbc.connect("DRIVER={SQL Server};SERVER=" + serverName + 
       ";DATABASE=" + dbName + 
       ";[email protected];PWD=mypassword") 
    cursor = connection.cursor() 
    data = cursor.execute("select * from sometable;") 
    allData = data.fetchall() 
    connection.close() 
    for i in allData: 
     print(i) 

if __name__== "__main__": 
    main() 

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server 'domain' requested by the login. Client with IP address 'xx.xx.xx.xx' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect. (40615) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]

我知道已经有关于类似问题的帖子,但我不知道这是同样的问题。我必须为此服务器使用Active Directory通用身份验证。我尝试了所有从pyodbc.drivers()返回的SQL Server驱动程序,但都没有运气。我使用相同的IP地址通过SQL Server Management Studio登录,就像当我通过Python程序尝试登录一样。是否有其他模块可用于使用Azure登录,或者这是不可能的?

仅供参考,我看到,当我通过Microsoft SQL Server Management Studio中登陆的弹出如下。我一直坚持这一点,所以任何帮助,非常感谢。 enter image description here

enter image description here

回答

1

我得到同样的错误消息,如果我有一套Azure的SQL防火墙: enter image description here

我们可以通过天蓝色门户网站设置SQL服务器的防火墙,就像这样: enter image description here

当我添加防火墙设置后,我可以使用你的pyt hon脚本来查询,python脚本适用于我。

+0

我无法理解为什么我需要使用不同的IP地址使用Python比我在使用SQL Server Management Studio中登录的一个登录。这是一个配置问题?我目前没有管理员权限来更改防火墙设置。 – user5932842

+0

你的python和ssms在同一台机器上运行吗? –

+0

我使用同一台计算机运行它们 - 当我使用这种身份验证方法时,SSMS是否可以切换到运行虚拟机上的登录进程,或者是否有可能发生多因素身份验证的奇怪事情(有时我必须输入我的密码两次在SSMS中的两个不同的弹出窗口)? – user5932842

相关问题