简体   繁体   English

使用 pyodbc 的 azure 函数在本地机器上工作正常,但在 azure 云上不能正常工作

[英]azure function using pyodbc works fine on local machine, but not on azure cloud

I developed a simple python Azure function app using pyodbc to select a few rows from a public IP MS SQL server.我开发了一个简单的 python Azure 函数应用程序,使用 pyodbc 从公共 IP MS SQL 服务器中选择几行。 My function app runs fine on my laptop, but it doesn't work when I publish it on Azure cloud (I used Consumption - serverless plan, linux environment).我的函数应用程序在我的笔记本电脑上运行良好,但是当我在 Azure 云上发布它时它不起作用(我使用了消费 - 无服务器计划,linux 环境)。 Thru the logging, I knows that it always gets stuck at the pyodbc.connect(...) command and time-out.通过日志记录,我知道它总是卡在 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})
#...

The logging captured:记录捕获:

Inventory API - connection error: OperationalError('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)').

I already include the pyodbc in the requirements.txt file.我已经在 requirements.txt 文件中包含了 pyodbc。 I also allows all outboundIpAddresses and possibleOutboundIpAddresses of my function app on my SQL server firewall.我还允许在我的 SQL 服务器防火墙上使用我的函数应用程序的所有 outboundIpAddresses 和 possibleOutboundIpAddresses。 My function app does not have any network restriction on Azure cloud (or at least it said so on the network settings).我的函数应用对 Azure 云没有任何网络限制(或者至少它在网络设置上是这样说的)。

my config file:我的配置文件:

driver={ODBC Driver 17 for SQL Server}
server=I tried both IP and full internet host name, both didn't work

Could someone give me a hint?有人可以给我一个提示吗? Thanks.谢谢。

Workarounds to fix the PYODBC connection Error修复PYODBC连接错误的解决方法

Try to use a below format尝试使用以下格式

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)

If you are using Domain Name add TCP with domain name server = 'tcp:myserver.database.windows.net' otherwise use the IP server = '129.0.0.1'如果您使用域名添加TCP与域名server = 'tcp:myserver.database.windows.net'否则使用IP server = '129.0.0.1'

If you are using port use like this 'tcp:myserver.database.windows.net,1233' or '129.0.0.1,1233'如果您正在使用像这样'tcp:myserver.database.windows.net,1233'端口使用'tcp:myserver.database.windows.net,1233''129.0.0.1,1233'

Try to remove additional properties like Connection_Timeout , Trusted_certificate and all and check now.尝试删除其他属性,如Connection_TimeoutTrusted_certificate ,然后立即检查。

Refer here参考这里

I put the following snippet into my function to check the outbound IP, and found out that Azure use a few outbound IPs that are not listed in the [outboundIpAddresses] and [possibleOutboundIpAddresses] ( documented in this MS link )我将以下代码段放入我的函数中以检查出站 IP,并发现 Azure 使用了一些未在 [outboundIpAddresses] 和 [possibleOutboundIpAddresses] 中列出的出站 IP( 记录在此 MS 链接中

import requests
#...
outbound_ip_response = requests.request('GET', 'https://checkip.amazonaws.com')
logging.info(f'Inventory API - main()- outbound ip = {outbound_ip_response.text}')

So, I followed the instructions in this link to setup a static outbound IP for my function app and allowed this IP to access my SQL server.因此,我按照此链接中的说明为我的函数应用设置了一个静态出站 IP,并允许该 IP 访问我的 SQL 服务器。 It worked.有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 本地 Azure Python:函数没有名为“pyodbc”的模块 - Local Azure Python: Function No module named 'pyodbc' Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc & 3) Azure user-managed identity - Accessing Azure SQL Server using 1) Python (local script--not going to be Azure function), 2) pyodbc & 3) Azure user-managed identity 无法导入 pyodbc insisde 一个 azure 函数,但可以在常规 python 文件中导入(在两者中使用相同的 venv) - Cannot import pyodbc insisde an azure function but an import in regular python file works (using the same venv in both) Azure 上的 pyodbc - pyodbc on Azure Python Watchdog可在本地计算机上运行,​​但不能在Azure虚拟机上运行吗? - Python Watchdog works on local computer but not on Azure Virtual Machine? 无法在python azure函数中导入pyodbc模块 - unable to import pyodbc module in python azure function Pyodbc SQL 带有参数的服务器存储过程在本地机器上工作,但在服务器上不工作 - Pyodbc SQL Server stored procedure with params works on local machine but not on server 我应该如何使用pyodbc配置与MS Azure云数据库的连接? - How should I configure the a connection to MS Azure cloud database using pyodbc? 无法使用 pyodbc 连接到 Azure SQL Server - Cannot connect to Azure SQL Server using pyodbc 在 Azure Data Studio 中使用 pyodbc:无驱动程序 - Using pyodbc in Azure Data Studio: no drivers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM