无法从Docker Django连接Sql Server



我正在尝试连接我的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

让:

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

SOLVED

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',
},
},

我使用的是image python:3.9.13-slim-buster, Django 3.2和驱动程序mssql-django

Dockerfile

FROM python:3.9.13-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 pip install mssql-django
RUN mkdir /code
COPY . /code/
COPY ./requirements.txt /code/
WORKDIR /code
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

Django==3.2
django-environ==0.4.5
# psycopg2>=2.8.6,<2.9
uWSGI>=2.0.19.1,<2.1
Pillow >= 9.0.0,<10.0.0

settings.py

DATABASES = {
'default': {
'ENGINE': 'mssql',
'HOST': os.environ.get('DB_HOST_SITO'),
'NAME': os.environ.get('DB_NAME_SITO'),
'USER': os.environ.get('DB_USER_SITO'),
'PASSWORD': os.environ.get('DB_PASS_SITO'),
'PORT': os.environ.get('DB_PORT_SITO'),
'OPTIONS': {
'driver': 'FreeTDS',
'unicode_results': True,
'host_is_server': True,
'driver_supports_utf8': True,
'extra_params': 'tds_version=7.4',
}
},
}

最新更新