尽管修复了防火墙设置,但Azure服务器访问问题

时间:2019-07-03 13:33:01

标签: sql-server python-3.x azure odbc pyodbc

尝试使用pyodbc连接到Azure SQL服务器时收到错误消息。

我在Azure门户的ODBC的“连接字符串”下找到了连接参数。我已经在防火墙设置中添加了我的IP地址(并等待了超过1个小时),但这不能解决问题。

import pyodbc

DRIVER = '{SQL Server}'
SERVER = 'tcp:[server name].database.windows.net'
PORT = '1433'
DATABASE = [database name]
USERNAME = [username]
PASSWORD = [password]
CONNECTION_STRING = f'DRIVER={DRIVER};PORT={PORT};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}'

cursor = pyodbc.connect(CONNECTION_STRING).cursor()

我收到以下错误:

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]
Cannot open server [server name] requested by the login. Client with IP address [my IP]
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]Invalid connection string attribute (0); [42000] [Microsoft]
[ODBC SQL Server Driver][SQL Server]Cannot open server [server name] requested by the 
login. Client with IP address [my IP] 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); [42000] [Microsoft]
[ODBC SQL Server Driver]Invalid connection string attribute (0)")

更新: 我尝试使用Visual Studio进行连接,它提示我创建新的防火墙规则。我选择“添加我的客户端IP”,然后单击“确定”。提示然后立即重新出现。我尝试单击几次,新规则的确出现在Azure门户中,但仍然无法通过Visual Studio或python连接。

解决方案: 我使用的是SQL身份验证,而不是Active Directory身份验证。通过向连接字符串添加AUTHENTICATION = ActiveDirectoryPassword解决了该问题。

1 个答案:

答案 0 :(得分:0)

  
      
  1. 请确保已将客户端IP添加到防火墙。
  2.   

在Azure SQL数据库概述中,设置服务器防火墙。 enter image description here

添加客户端IP:

enter image description here

  
      
  1. 请这样修改您的代码,然后重试:
  2.   
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

此代码示例由以下文档提供:Create code to query your SQL database

更新

错误已解决。.

  

最后弄清楚了-事实证明问题出在Jon123使用的是SQL身份验证而不是Active Directory身份验证。

希望这会有所帮助。