[FreeTDS][SQL Server]用户登录失败

[FreeTDS][SQL Server]Login failed for user

我正在 windows 上开发并尝试 运行 使用 Gunicorn 和 Nginx 在 Linux 容器中的 Django 应用程序将其部署到生产中的 Linux 机器。

我主要使用这个 post 作为指南,但我想我已经尝试了网上找到的关于这个错误的所有解决方案。

如果我从容器中 ping DB 服务器,它会连接,因此端口 1433 已打开,一切都应该顺利进行。但由于某种原因,我收到错误 django.db.utils.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Login failed for user

Django settings.py

DATABASES = {
    'default': {
        'ENGINE': "sql_server.pyodbc",
        'NAME': 'database1',
        'HOST': '123.45.6.78',
        'PORT':'1433',
        'USER': "user",
        'PASSWORD': "pswd",
        'OPTIONS': {
            "driver": "FreeTDS",
            "host_is_server": True,
            "unicode_results": True,
            "extra_params": "tds_version=7.3",
        }
    }
}

Dockerfile

# start from an official image
FROM python:3

# arbitrary location choice: you can change the directory
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src

#Install FreeTDS and dependencies for PyODBC
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\
TDS_Version = '7.3'\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

# modify "freetds.conf"
RUN echo "[mssql]\n\
host = 172.30.2.18\n\
port = 1433\n\
tds version = 7.3" >> /etc/freetds/freetds.conf

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


# install our dependencies
# we use --system flag because we don't need an extra virtualenv
COPY Pipfile Pipfile.lock /opt/services/djangoapp/src/
RUN pip install pipenv && pipenv install --system

# copy our project code
COPY . /opt/services/djangoapp/src

# expose the port 8000
EXPOSE 8000

# define the default command to run when starting the container
CMD ["gunicorn", "--chdir", "app", "--bind", ":8000", "config.wsgi:application"]

docker-compose.yml

version: '3'

services:

  djangoapp:
    build: .
    volumes:
      - .:/opt/services/djangoapp/src
      - /static:/static
    networks:  # <-- here
      - nginx_network

  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - /static:/static
    depends_on:
      - djangoapp
    networks:  # <-- here
      - nginx_network

networks:  # <-- and here
  nginx_network:
    driver: bridge

Pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "==2.1.0"
pyodbc = "==4.0.28"
django-pyodbc-azure = "*"
django-datatables-view = "*"            
gunicorn = "*"
whitenoise = "*"


[requires]
python_version = "3.8"

终于自己解决了。必须在 .yml 文件中将端口 1433 添加到 nginx。

喜欢这个

  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
      - 1433:1433
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - /static:/static
    depends_on:
      - djangoapp
    networks:  # <-- here
      - nginx_network