【问题标题】:Unable to connect to Sql Server from Docker Django无法从 Docker Django 连接到 Sql Server
【发布时间】:2021-10-21 12:05:45
【问题描述】:

我正在尝试将我的 Django 连接到现有的旧版 MS SQL 数据库。

当我尝试在数据库上运行 sql 时工作正常:

sql = 'SELECT * FROM ' + db_name + '.INFORMATION_SCHEMA.TABLES'
connection_string = "driver=FreeTDS;server={};PORT={} database={};UID={};PWD={};TDS_Version=8.0;".format(db_host, db_port, db_name, db_user, db_password)
conn = pyodbc.connect(connection_string, autocommit=True)
for row in cursor.fetchall():
  print(row)

我可以看到所有表格,但是当尝试从运行此命令的数据库生成模型时:

python manage.py inspectdb --database pirineos > pirineos_models.py

我得到错误:

django.db.utils.Error: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

这是我的项目文件。

Dockerfile:

FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update -y && apt-get install -y unixodbc unixodbc-dev tdsodbc freetds-common freetds-bin freetds-dev && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN mkdir /djangonoguero
COPY ./project /djangonoguero/
COPY ./requirements.txt /djangonoguero/
ADD odbcinst.ini /etc/
WORKDIR /djangonoguero
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000

requirements.txt:

pyodbc==3.0.10
django-mssql-backend==2.8.1

settings.py:

DATABASES = {
  'pirineos': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': DB_PIRINEOS_NAME,
    'USER': DB_PIRINEOS_USER,
    'PASSWORD': DB_PIRINEOS_PASSWORD,
    'HOST': DB_PIRINEOS_HOST,
    'PORT': DB_PIRINEOS_PORT,
    'OPTIONS': {
      'driver': 'FreeTDS',
      'unicode_results': True,
      'driver_supports_utf8' : True,
      'extra_params': 'tds_version=8.0;',
    },
  },
}

odbcinst.ini:

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL on Win32
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
UsageCount=1

【问题讨论】:

  • 好像打错了:PORT={} database={}; - 你试过用PORT={};database={}; 代替吗?
  • 第一个cli命令运行良好,问题出在inspectdb命令
  • 另外,你从哪里得到tds_version=8.0?最高 TDS 协议版本FreeTDS supports 为 7.4。
  • tds_version=7.4错误仍然存​​在。

标签: sql-server django docker


【解决方案1】:

已解决

Dockerfile:

FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
ADD odbcinst.ini /etc/
RUN apt-get update -y && apt-get install -y curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y && apt-get install -y unixodbc unixodbc-dev tdsodbc freetds-common freetds-bin freetds-dev postgresql python-scipy python-numpy python-pandas
RUN apt-get update && ACCEPT_EULA=Y apt-get -y install mssql-tools msodbcsql17
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN apt-get update
RUN mkdir /djangonoguero
COPY ./project /djangonoguero/
COPY ./requirements.txt /djangonoguero/
WORKDIR /djangonoguero
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000

odbcinst.ini:

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

settings.py:

'pirineos': {
  'ENGINE': 'sql_server.pyodbc',
  'NAME': DB_PIRINEOS_NAME,
  'USER': DB_PIRINEOS_USER,
  'PASSWORD': DB_PIRINEOS_PASSWORD,
  'HOST': DB_PIRINEOS_HOST,
  'PORT': DB_PIRINEOS_PORT,
  'OPTIONS': {
    'driver': 'FreeTDS',
    'unicode_results': True,
    'host_is_server': True,
    'driver_supports_utf8': True,
    'extra_params': 'tds_version=7.4',
  },
},

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 2016-12-26
    • 2020-10-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多