use*_*682 13 python linux azure docker
我对 Python 和 Azure Web 应用程序相当陌生。任何帮助表示赞赏。
我的设置:
在我的代码中,我使用它pyodbc来连接到 Azure SQL DB。代码在终端本地成功运行。然而,当它在Web上运行时,遇到以下错误:
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
我遵循了几篇故障排除帖子,但是没有成功。
我尝试使用 $sudo ln 创建符号链接,导致权限被拒绝。我认为这是 Azure Web 应用程序的一个已知限制。
我尝试在 etc/odbcinst.ini 中查找驱动程序以查看驱动程序名称是否存在,但是,我对 Azure / VS Code 非常陌生,所以我什至不知道如何打开等/中的文件文件夹。当我导航到 etc/ 文件夹时,我确实在 BASH 命令提示符中看到它,但不确定如何打开该文件。
BASH我在安装中运行了以下命令PYODBC,但这并没有解决问题。
python -m pip install pyodbc
Run Code Online (Sandbox Code Playgroud)
结果来自odbcinst -j
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/a49d42b0d7b8ce200a4f7e74/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Run Code Online (Sandbox Code Playgroud)
我的docker文件:
# Pull a pre-built alpine docker image with nginx and python3 installed
FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
ENV LISTEN_PORT=8000
EXPOSE 8000
COPY /app /app
# Uncomment to install additional requirements from a requirements.txt file
COPY requirements.txt /
RUN pip install --no-cache-dir -U pip
RUN pip install --no-cache-dir -r /requirements.txt
RUN apk add g++
RUN apk add unixodbc-dev
RUN pip install pyodbc
Run Code Online (Sandbox Code Playgroud)
我的要求.txt。我注释掉了pyodbc;我认为这没问题,因为我将其安装在 docker 文件中。
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1
#pyodbc==4.0.28
Run Code Online (Sandbox Code Playgroud)
附加问题:
odbcinst.ini网络应用程序上的文件?首先,如果你想知道你的docker中是什么os版本,你可以通过命令cat /etc/os-release来获取它,比如在我的ubuntu中运行它,如下图。
在这里,根据 docker 文件的第一行,我确定您的网络应用程序操作系统是 Alpine Linux FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7。您的基础镜像基于 Alpine 3.7。
其次,根据您的错误信息Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")和 docker 文件的内容requirements.txt,我认为问题是由于缺少适用于 Linux 的 MS SQL Server ODBC 驱动程序引起的,该驱动程序未安装在您的 docker 映像中,但pyodbc需要它连接 Azure SQL 数据库。
然而,对于Alpine ODBC Driver 17 for SQL Server,官方文档Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS显示还没有发布v17包。因此,解决方法是将 DockerHub 基础映像从 更改为tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7使用tiangolo/uwsgi-nginx-flask:python3.6debian 作为操作系统,然后您可以轻松地在其中安装适用于 SQL Server 的 MS ODBC 驱动程序 17。
对于您的其他问题,如下。
除了使用之外pyodbc,pymssql另一种是Python SQL Driver,请看官方文档Python SQL Driver,但是The Pymssql Project is Being Discontinued。而SQLAlchemy由于 ORM 框架可以用来连接 Azure SQL 数据库,这也需要pyodbc或pymssql.
使用 MySQL 或 Azure SQL 数据库,由您决定。我认为唯一的区别是 MySQL 可能比 Alpine 中的 Azure SQL DB 更容易安装。
在 webapp 上打开文件的方法odbcinst.ini是使用 vim 通过 SSH 连接到你的 docker 操作系统。Enable SSH考虑到您使用的自定义 docker 镜像,请参阅官方文档的部分Configure a custom Linux container for Azure App Service,并将命令替换apk为aptDebian Linux。
小智 5
官方网站上的以下说明帮助我解决了我的问题:Install the Microsoft ODBC driver for SQL Server (Linux)
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
#Debian 8 (only supported up to driver version 17.6)
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Debian 9
curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Debian 10
curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
# optional: kerberos library for debian-slim distributions
sudo apt-get install -y libgssapi-krb5-2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34472 次 |
| 最近记录: |