You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从Azure Functions连接Azure SQL:缺失ODBC Driver 18

解决Azure Functions Linux环境缺失ODBC Driver 18导致的pyodbc连接失败问题

问题原因

你使用的Azure Functions Linux默认镜像未预装ODBC Driver 18 for SQL Server,导致pyodbc无法找到驱动文件抛出错误。以下是两种可靠的解决方法:

方法1:通过部署脚本在线安装驱动

适合快速修复,无需构建自定义镜像:

  1. 在函数应用根目录创建.deployment文件,内容如下:
[config]
command = bash deploy.sh
  1. 同目录创建deploy.sh脚本,填入安装驱动的命令(适配Debian/Ubuntu系镜像):
#!/bin/bash
# 更新包列表并安装依赖工具
apt-get update && apt-get install -y curl apt-transport-https
# 添加微软官方软件源
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
# 静默安装ODBC Driver 18
apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql18
  1. 给脚本添加执行权限:
chmod +x deploy.sh
  1. 将所有文件一起部署到Azure Functions,部署过程中会自动执行脚本安装驱动。

方法2:使用自定义Docker镜像固化环境

适合需要长期稳定环境或有额外依赖的场景:

  1. 编写Dockerfile,基于官方Azure Functions Python镜像并添加驱动安装步骤:
FROM mcr.microsoft.com/azure-functions/python:4-python3.10

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

# 安装ODBC Driver 18及其依赖
RUN apt-get update && apt-get install -y curl apt-transport-https \
    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql18 \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装Python依赖
COPY requirements.txt /
RUN pip install -r /requirements.txt

# 复制函数代码
COPY . /home/site/wwwroot
  1. 构建镜像并推送到Azure Container Registry(ACR),然后将函数应用的部署方式切换为自定义镜像,指定ACR中的镜像地址。

验证驱动安装成功

部署完成后,进入函数应用的高级工具(Kudu),打开控制台执行以下命令,若输出包含ODBC Driver 18 for SQL Server则说明安装成功:

odbcinst -q -d

你的代码逻辑本身无问题,驱动安装完成后,托管身份的token认证流程即可正常运行。

内容的提问来源于stack exchange,提问作者Abhishek Rai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 03:23:34