【问题标题】:Adding Linux ODBC drivers for SQL Server to a Kaggle/Python docker image将 SQL Server 的 Linux ODBC 驱动程序添加到 Kaggle/Python docker 映像
【发布时间】:2018-05-06 21:44:48
【问题描述】:

我正在尝试构建一个能够在生产环境中运行机器学习 python 程序的数据科学机器。

当前的业务案例数据需要从 SQL Server 中提取,使用 python 进行机器学习评分,然后推送回 SQL Server。

我想为 Linux 安装 ODBC 驱动程序。

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server

我的问题是要安装哪些驱动程序?

当我使用以下命令连接到容器时,它可以正常工作并映射到 poc 目录中的我的 python 程序。

“docker run -it --volume=c:\docker\poc:/poc kaggle/python /bin/bash”

当我尝试使用以下命令找出 Linux 的版本时,

“uname -a”

我看到它是来自 Docker 的 Moby Linux?

Linux 69982a00af21 4.9.49-moby #1 SMP Wed Sep 27 00:36:29 UTC 2017 x86_64 GNU/Linux

但是,构建的基础映像是什么? (Ubuntu、Debian、Fedora、Red Hat 等)

我需要一个破解者 Unix 管理员来帮助我。如何安装 ODBC 驱动程序?

任何接受者!

提前感谢您的帮助。

约翰

【问题讨论】:

    标签: python sql-server docker odbc kaggle


    【解决方案1】:

    我不确定我是否有您之前正在寻找的答案,因为您参考了 Microsoft ODBC 驱动程序 - 但是我将分享我为安装 UnixODBC 所做的工作(然后我将其与 PyODBC 模块一起使用与 MSSQL 数据库通信)。

    以下是我使用过的命令的摘要(警告:未经测试,因为它们是从生产 Dockerfile 中剪切和粘贴的,我无法完整分享-):

    假设您在基于 Debian/Ubuntu 的 Linux Docker 容器中

    $ apt-get update
    $ apt-get install python2.7 python-pip unixodbc unixodbc-dev freetds-bin freetds-dev tdsodbc nano
    
    # nano is a text editor; Ctrl + O to write out, Ctrl + X to exit
    
    $ nano /etc/odbcinst.ini
    
    [FreeTDS]
    Description = FreeTDS Driver for MSSQL
    Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
    Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
    
    $ nano /etc/freetds/freetds.conf
    
    [global]
    #   $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
    #
    # This file is installed by FreeTDS if no file by the same
    # name is found in the installation directory.
    #
    # For information about the layout of this file and its settings,
    # see the freetds.conf manpage "man freetds.conf".
    
    # Global settings are overridden by those in a database
    # server specific section
    [global]
    # TDS protocol version
    tds version = 8.0
    
    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
    ;dump file = /tmp/freetds.log
    ;debug flags = 0xffff
    
    # Command and connection timeouts
    ;timeout = 10
    ;connect timeout = 10
    
    # If you get out-of-memory errors, it may mean that your client
    # is trying to allocate a huge buffer for a TEXT field.
    # Try setting 'text size' to a more reasonable limit
    text size = 64512
    
    # If you experience TLS handshake errors and are using openssl,
    # try adjusting the cipher list (don't surround in double or single quotes)
    # openssl ciphers = HIGH:!SSLv2:!aNULL:-DH
    
    $ pip install pyodbc
    
    $ nano test_database.py
    
    import pyodbc
    
    host = 'some.host.org'
    port = 1433
    username = 'some_username'
    password = 'some_password'
    
    conn_str = """
                DRIVER={FreeTDS};
                TDS_VERSION=8.0;
                SERVER=%s;
                Port=%i;
                DATABASE=%s;
                UID=%s;
                PWD=%s;
            """ % (
        host, port, database, username, password
    )
    
    conn_str = conn_str.replace('\n', '').replace(' ', '')
    
    connection = pyodbc.connect(conn_str, timeout=self._timeout)
    
    connection.autocommit = True
    
    cursor = connection.cursor()
    
    cursor.execute('SELECT * FROM some_table;')
    
    rows = cursor.fetchall()
    
    for row in rows:
        print row
    
    cursor.close()
    
    connection.close()
    

    (上面的代码块是可滚动的)

    我必须再次警告,这都是从我们的代码库中剪切和粘贴的,我还没有测试它现在出现的样子。

    如果您无法访问 MSSQL 数据库进行测试,您可以使用以下命令将其启动为 Docker 容器(SQL Server Express Edition):

    docker run -d --name mssql-server -p 1433:1433 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=some_password' -e 'MSSQL_PID=Express' microsoft/mssql-server-linux:latest
    

    另外请注意,作为上面安装的软件包的一部分,您将拥有“tsql”命令行工具,可用于在 Python 之外仔细检查您的数据库内容。

    祝你好运!

    【讨论】:

      【解决方案2】:

      遇到了同样的问题,根据这个模板组装了一个Docker镜像:

      FROM python:3.7.7-slim-stretch  
      
      # Install pyodbc https://github.com/mkleehammer/pyodbc/wiki/Install
      RUN apt-get update && apt-get install -y --no-install-recommends g++ unixodbc-dev curl gnupg apt-transport-https apt-utils
      RUN pip install pyodbc
      
      # Install Microsoft ODBC 17 
      # https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
      ENV ACCEPT_EULA=Y
      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 && apt-get install -y --no-install-recommends msodbcsql17 mssql-tools
      

      组装此解决方案的最佳来源是pyodbc wikiMS Instructions for odic 17,这导致我使用FROM python:3.7.7-slim-stretch,因此这两个版本都是更新版本,但已记录在案并有明确的文档。

      【讨论】:

        猜你喜欢
        • 2023-01-13
        • 2017-03-06
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 2020-06-01
        相关资源
        最近更新 更多