温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python 3.x - Azure server access issue despite fixing firewall settings
azure odbc pyodbc python-3.x sql-server

python 3.x - 尽管修复了防火墙设置,但Azure服务器访问问题

发布于 2020-03-27 10:34:09

我尝试使用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)")

Update: I tried connecting using Visual Studio and it prompts me to create a new firewall rule. I choose 'Add my client IP' and click 'OK'. The prompt then immediately reappears. I tried clicking it a few times and the new rules do appear in the Azure portal, but I am still not able to connect through either Visual Studio or python.

Solution: I was using SQL authentication instead of Active Directory authentication. Solved the problem by adding AUTHENTICATION=ActiveDirectoryPassword to the connection string.

查看更多

查看更多

提问者
Jon Riege
被浏览
296
Leon Yue 2019-07-06 12:36
  1. Please ensure you have added client IP to firewall.

On the Azure SQL database overview, Set server firewall. 在此处输入图片说明

Add client IP:

在此处输入图片说明

  1. Please modify you code like this and try again:
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()

本文档提供了以下代码示例:创建代码以查询SQL数据库

更新:

错误已解决。

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

希望这可以帮助。