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

Azure server access issue despite fixing firewall settings

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

我在 'Connection strings' 下找到了 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 进行连接,它提示我创建新的防火墙规则。我选择 'Add my client IP' 并单击 'OK'。然后提示立即重新出现。我尝试点击它几次,新规则确实出现在 Azure 门户中,但我仍然无法通过 Visual Studio 或 python.

进行连接

解法: 我使用的是 SQL 身份验证而不是 Active Directory 身份验证。通过将 AUTHENTICATION=ActiveDirectoryPassword 添加到连接字符串来解决问题。

  1. Please ensure you have added client IP to firewall.

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

添加客户端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()

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

更新:

错误已解决..

Finally figured it out - it turns out the problem was that Jon123 was using SQL authentication instead of Active Directory authentication.

希望对您有所帮助。