使用 pyodbc 的 azure 函数在本地机器上运行良好,但在 azure 云上运行不正常
azure function using pyodbc works fine on local machine, but not on azure cloud
我开发了一个简单的 python Azure 函数应用程序,使用 pyodbc 从 public IP MS SQL 服务器 select 几行。我的函数应用程序在我的笔记本电脑上运行良好,但当我在 Azure 云上发布它时它不起作用(我使用消费 - 无服务器计划,linux 环境)。通过日志记录,我知道它总是卡在 pyodbc.connect(...) 命令和超时。
#...
conn_str = f'Driver={driver};Server={server},{port};Database={database};Uid={user};Pwd={password};Encrypted=yes;TrustServerCertificate=no;Connection Timeout=30'
sql_query = f'SELECT * FROM {table_name}'
try:
conn = pyodbc.connect(conn_str) # always time-out here if running on Azure cloud!!!
logging.info(f'Inventory API - connected to {server}, {port}, {user}.')
except Exception as error:
logging.info(f'Inventory API - connection error: {repr(error)}.')
else:
with conn.cursor() as cursor:
cursor.execute(sql_query)
logging.info(f'Inventory API - executed query: {sql_query}.')
data = []
for row in cursor:
data.append({'Sku' : row.Sku, 'InventoryId' : row.InventoryId, 'LocationId' : row.LocationId, 'AvailableQuantity' : row.AvailableQuantity})
#...
捕获的日志记录:
Inventory API - connection error: OperationalError('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)').
我已经在 requirements.txt 文件中包含了 pyodbc。我还在 SQL 服务器防火墙上允许我的函数应用程序的所有 outboundIpAddresses 和 possibleOutboundIpAddresses。我的函数应用程序在 Azure 云上没有任何网络限制(或者至少在网络设置上是这样说的)。
我的配置文件:
driver={ODBC Driver 17 for SQL Server}
server=I tried both IP and full internet host name, both didn't work
有人可以给我提示吗?谢谢
修复 PYODBC
连接错误的解决方法
尝试使用以下格式
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
如果您使用的是域名称添加TCP域名server = 'tcp:myserver.database.windows.net'
否则使用 IP server = '129.0.0.1’
如果您正在使用 端口,请像这样使用 'tcp:myserver.database.windows.net,1233’
或 '129.0.0.1,1233'
尝试删除 Connection_Timeout
、Trusted_certificate
等其他属性并立即检查。
参考here
我将以下代码片段放入我的函数中以检查出站 IP,发现 Azure 使用了一些未在 [outboundIpAddresses] 和 [possibleOutboundIpAddresses] 中列出的出站 IP (documented in this MS link)
import requests
#...
outbound_ip_response = requests.request('GET', 'https://checkip.amazonaws.com')
logging.info(f'Inventory API - main()- outbound ip = {outbound_ip_response.text}')
因此,我按照 link 中的说明为我的函数应用程序设置了一个静态出站 IP,并允许该 IP 访问我的 SQL 服务器。成功了。
我开发了一个简单的 python Azure 函数应用程序,使用 pyodbc 从 public IP MS SQL 服务器 select 几行。我的函数应用程序在我的笔记本电脑上运行良好,但当我在 Azure 云上发布它时它不起作用(我使用消费 - 无服务器计划,linux 环境)。通过日志记录,我知道它总是卡在 pyodbc.connect(...) 命令和超时。
#...
conn_str = f'Driver={driver};Server={server},{port};Database={database};Uid={user};Pwd={password};Encrypted=yes;TrustServerCertificate=no;Connection Timeout=30'
sql_query = f'SELECT * FROM {table_name}'
try:
conn = pyodbc.connect(conn_str) # always time-out here if running on Azure cloud!!!
logging.info(f'Inventory API - connected to {server}, {port}, {user}.')
except Exception as error:
logging.info(f'Inventory API - connection error: {repr(error)}.')
else:
with conn.cursor() as cursor:
cursor.execute(sql_query)
logging.info(f'Inventory API - executed query: {sql_query}.')
data = []
for row in cursor:
data.append({'Sku' : row.Sku, 'InventoryId' : row.InventoryId, 'LocationId' : row.LocationId, 'AvailableQuantity' : row.AvailableQuantity})
#...
捕获的日志记录:
Inventory API - connection error: OperationalError('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)').
我已经在 requirements.txt 文件中包含了 pyodbc。我还在 SQL 服务器防火墙上允许我的函数应用程序的所有 outboundIpAddresses 和 possibleOutboundIpAddresses。我的函数应用程序在 Azure 云上没有任何网络限制(或者至少在网络设置上是这样说的)。
我的配置文件:
driver={ODBC Driver 17 for SQL Server}
server=I tried both IP and full internet host name, both didn't work
有人可以给我提示吗?谢谢
修复 PYODBC
连接错误的解决方法
尝试使用以下格式
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
如果您使用的是域名称添加TCP域名server = 'tcp:myserver.database.windows.net'
否则使用 IP server = '129.0.0.1’
如果您正在使用 端口,请像这样使用 'tcp:myserver.database.windows.net,1233’
或 '129.0.0.1,1233'
尝试删除 Connection_Timeout
、Trusted_certificate
等其他属性并立即检查。
参考here
我将以下代码片段放入我的函数中以检查出站 IP,发现 Azure 使用了一些未在 [outboundIpAddresses] 和 [possibleOutboundIpAddresses] 中列出的出站 IP (documented in this MS link)
import requests
#...
outbound_ip_response = requests.request('GET', 'https://checkip.amazonaws.com')
logging.info(f'Inventory API - main()- outbound ip = {outbound_ip_response.text}')
因此,我按照 link 中的说明为我的函数应用程序设置了一个静态出站 IP,并允许该 IP 访问我的 SQL 服务器。成功了。