使用PYODBC将Docker Python连接到SQL Server



我正在尝试连接在docker容器中运行的pyodbc python脚本以登录到MSSQL数据库,我尝试了各种docker文件,但无法建立连接(但是在燃烧Docker或Python试图连接时失败),是否有人使用PYODBC:

dockerfile:

# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Run app.py when the container launches
CMD ["python", "App.py"]

需求.txt

pyodbc

app.py

import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
                            'Server=xxxx;'
                            'Database=xxx;'
                            'UID=xxxx;'
                            'PWD=xxxx')
cursor = connection.cursor()
cursor.execute("SELECT [Id],[Name] FROM [DCMM].[config].[Models]")
for row in cursor.fetchall():
    print(row.Name)

connection.close()

燃烧容器 docker build -t sqltest。

输出:

Sending build context to Docker daemon  4.096kB
Step 1/5 : FROM python:2.7-slim
 ---> 426d65ab9a72
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 725f35122880
Step 3/5 : ADD . /app
 ---> 3feb8b7744f7
Removing intermediate container 4214091a111a
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 27aa4dcfe738
Collecting pyodbc (from -r requirements.txt (line 1))
  Downloading pyodbc-4.0.17.tar.gz (196kB)
Building wheels for collected packages: pyodbc
  Running setup.py bdist_wheel for pyodbc: started
  Running setup.py bdist_wheel for pyodbc: finished with status 'error'
  Failed building wheel for pyodbc
  Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpa3S13tpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_ext
  building 'pyodbc' extension
  creating build
  creating build/temp.linux-x86_64-2.7
  creating build/temp.linux-x86_64-2.7/src
  gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPYODBC_VERSION=4.0.17 -DSQL_WCHART_CONVERT=1 -I/usr/local/include/python2.7 -c src/cursor.cpp -o build/temp.linux-x86_64-2.7/src/cursor.o -Wno-write-strings
  unable to execute 'gcc': No such file or directory
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pyodbc
  Running setup.py install for pyodbc: started
    Running setup.py install for pyodbc: finished with status 'error'
    Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-BV4sRM-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_ext
    building 'pyodbc' extension
    creating build
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/src
    gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPYODBC_VERSION=4.0.17 -DSQL_WCHART_CONVERT=1 -I/usr/local/include/python2.7 -c src/cursor.cpp -o build/temp.linux-x86_64-2.7/src/cursor.o -Wno-write-strings
    unable to execute 'gcc': No such file or directory
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-BV4sRM-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-EfWsmy/pyodbc/
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

需要运行:

sudo apt-get install gcc

需要添加一个包含:

的odbcinst.ini文件
[FreeTDS]Description=FreeTDS Driver Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

需要添加foring for docker文件

ADD odbcinst.ini /etc/odbcinst.ini
RUN apt-get update
RUN apt-get install -y tdsodbc unixodbc-dev
RUN apt install unixodbc-bin -y
RUN apt-get clean -y

需要以.py将连接更改为

connection = pyodbc.connect('Driver={FreeTDS};'
                            'Server=xxxxx;'
                            'Database=DCMM;'
                            'UID=xxxxx;'
                            'PWD=xxxxx')

现在的容器编译,并从SQL Server获取数据

最近通过此操作,我发现有必要还包括以下行(请注意,没有此步骤,它没有构建):

RUN apt-get install --reinstall build-essential -y

完整的Dockerfile看起来如下:

# parent image
FROM python:3.7-slim
# install FreeTDS and dependencies
RUN apt-get update 
 && apt-get install unixodbc -y 
 && apt-get install unixodbc-dev -y 
 && apt-get install freetds-dev -y 
 && apt-get install freetds-bin -y 
 && apt-get install tdsodbc -y 
 && apt-get install --reinstall build-essential -y
# populate "ocbcinst.ini"
RUN echo "[FreeTDS]n
Description = FreeTDS unixODBC Drivern
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.son
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini
# install pyodbc (and, optionally, sqlalchemy)
RUN pip install --trusted-host pypi.python.org pyodbc==4.0.26 sqlalchemy==1.3.5
# run app.py upon container launch
CMD ["python", "app.py"]

这是通过sqlalchemy在app.py中实际建立连接的一种方法(并假设端口1433):

import sqlalchemy as sa
args = (username, password, server, database)
connstr = "mssql+pyodbc://{}:{}@{}/{}?driver=FreeTDS&port=1433&odbc_options='TDS_Version=8.0'"
engine = sa.create_engine(connstr.format(*args))

基于KåreRasmussen的答案,这是一个完整的Dockerfile,供进一步使用。

确保根据您的体系结构编辑最后两行!他们应该反映到libtdsodbc.so and libtdss.so.so.so.so.so.so.

的实际路径。

如果您不确定通往libtdsodbc.so和libtdss.s.so的路径,请尝试dpkg --search libtdsodbc.sodpkg --search libtdsS.so

FROM python:3
#Install FreeTDS and dependencies for PyODBC
RUN apt-get update && apt-get install -y tdsodbc unixodbc-dev 
 && apt install unixodbc-bin -y  
 && apt-get clean -y
RUN echo "[FreeTDS]n
Description = FreeTDS unixODBC Drivern
Driver = /usr/lib/arm-linux-gnueabi/odbc/libtdsodbc.son
Setup = /usr/lib/arm-linux-gnueabi/odbc/libtdsS.so" >> /etc/odbcinst.ini

之后,安装PyodBC,复制您的应用程序并运行它。

我无法使用上述所有分辨率,我一直在与PYODBC软件包有关的错误,尤其是:

Importerror:libodbc.so.2:无法打开共享对象文件:没有这样的文件或目录。

我最终获得了另一个分辨率,该分辨率专门针对Ubuntu 18.04 Docker Image定义了ODBC SQL Server驱动程序,在这种情况下,SQL Server ODBC驱动程序17。在我的特定用例中,我需要通过Blask Sqlalchemy在Azure上与MySQL数据库服务器进行连接,但是后者不需要Docker配置。

dockerfile,最重要的部分添加Microsoft存储库并安装 msodbcsql17 unixodbc-dev

# Ubuntu 18.04 base with Python runtime and pyodbc to connect to SQL Server
FROM ubuntu:18.04
WORKDIR /app
# apt-get and system utilities
RUN apt-get update && apt-get install -y 
    curl apt-utils apt-transport-https debconf-utils gcc build-essential g++-5
    && rm -rf /var/lib/apt/lists/*
# adding custom Microsoft repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
# install SQL Server drivers
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev
# install SQL Server tools
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN /bin/bash -c "source ~/.bashrc"
# python libraries
RUN apt-get update -y && 
    apt-get install -y python3-pip python3-dev
# install necessary locales, this prevents any locale errors related to Microsoft packages
RUN apt-get update && apt-get install -y locales 
    && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen 
    && locale-gen
# copy requirements and install packages, I added this for general use
COPY ./requirements.txt > ./requirements.txt
RUN pip3 install -r ./requirements.txt
# you can also use regular install of the packages
RUN pip3 install pyodbc SQLAlchemy
# and if you are also planning to use Flask and Flask-SQLAlchemy
Run pip3 install Flask Flask-SQLAlchemy
COPY ..
# run your app via entrypoint or change the CMD command to your regular command
COPY docker-entrypoint.sh wsgi.py ./
CMD ["./docker-entrypoint.sh"]

这应该在Docker中没有任何错误的情况下构建。

我的数据库URL看起来像这样:

import urllib.parse
# name the sepcific ODBC driver by version number, we installed msodbcsql17
params = urllib.parse.quote_plus("DRIVER={ODBC Driver 17 for SQL Server};SERVER=<your.database.windows.net>;DATABASE=<your-db-name>;UID=<username>;PWD=<password>")
db_uri = "mssql+pyodbc:///?odbc_connect={PARAMS}".format(PARAMS=params)

,对于奖金,如果您使用的是烧结式 - sqlalchemy,则您的应用程序配置应包含类似的内容:

app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri  # from above

快乐编码!

如何安装pyodbc的必要依赖项与Linux分布及其版本有关(在Docker Case中,这是Docker Image的基本图像)。如果以上都不适合您,则可以通过在Docker容器实例中尝试找出命令。

首先,执行到docker容器

docker exec -it <container id> bash

尝试各种方法来获取Linux的发行名称和版本。然后在安装SQL Server(Linux)

的Microsoft ODBC驱动程序时尝试不同的说明

这是基于Debian 9的图像的工作示例,完全按照文档说明得出。

# Install pyodbc dependencies
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql17
RUN apt-get -y install unixodbc-dev
RUN pip install pyodbc

我在github上创建了一个要点,介绍了如何做到这一点。我希望它有帮助。我不得不从不同资源上发现的东西拼凑在一起。

https://gist.github.com/joshatxantie/4BCF5D0243FBA63845FCE7CC40365A3A

goodluck!

对于我解决此问题,我还必须在Dockerfile中添加以下2行:

RUN echo MinProtocol = TLSv1.0 >> /etc/ssl/openssl.cnf
RUN echo CipherString = DEFAULT@SECLEVEL=1 >> /etc/ssl/openssl.cnf

对于那些想做官方Microsoft方法以安装ODBC驱动程序并使用python:slim Docker Image的人,您可以将其用作Dockerfile:

FROM python:3.9-slim
RUN apt-get -y update && apt-get install -y curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# download appropriate package for the OS version
# Debian 11
RUN curl https://packages.microsoft.com/config/debian/11/prod.list  
    > /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN apt-get -y update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql18

然后可以称为Sqlalchemy的此:

con_str = f"mssql+pyodbc://{username}:{password}@{host}/{db}?" 
              "driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
engine = create_engine(con_str)

我通过使用pypyodbc而不是pyodbc解决了这个问题。

pip install pypyodbc==1.3.5

https://pypi.org/project/pypyodbc/

在此处找到提示:https://github.com/azure/azure-functions-python-worker/issues/249

为了不再有问题,请使用python库pymssql这不需要安装驱动程序

pip install pymssql

import pymssql
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor(as_dict=True)
cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
for row in cursor:
    print("ID=%d, Name=%s" % (row['id'], row['name']))
conn.close()

并在Docker中工作

使PYODBC使用 odbc驱动程序17用于SQL Server 在我的容器中,我进行了以下操作:

  1. 获取Microsoft.asc键

这是可以从以下位置获取的地方:https://packages.microsoft.com/keys/microsoft.asc

  1. 更新Docker文件以安装并识别SQL Server和依赖项的ODBC驱动程序17
RUN apt-get update 
  && apt-get install unixodbc -y 
  && apt-get install unixodbc-dev -y 
  && apt-get install freetds-dev -y 
  && apt-get install freetds-bin -y 
  && apt-get install tdsodbc -y 
  && apt-get install --reinstall build-essential -y 
  && apt-get install -y libltdl7 
  && apt-get install -y libodbc1 
  && apt-get install -y odbcinst 
  && apt-get install -y odbcinst1debian2 
  && apt-get install -y locales-all
    
COPY microsoft.asc /usr/
RUN apt-get update
RUN apt-get update 
  && apt-get install -y curl apt-transport-https locales gnupg2 
  && apt-key add /usr/microsoft.asc
    
RUN curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN echo "[ODBC Driver 17 for SQL Server]n
  Description=Microsoft ODBC Driver 17 for SQL Servern
  Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1n
  UsageCount=1" >> /etc/odbcinst.ini

相关内容

  • 没有找到相关文章

最新更新