如何在Mac上正确设置Django + MS SQL + Docker?

时间:2019-03-17 04:04:24

标签: sql-server django docker

为什么要使用这种特殊组合?

  • 我需要创建一个与MS SQL数据库进行对话的网站以显示公司的清单(并且数据库是由公司使用的专有软件创建的,因此不能切换到另一种数据库)< / li>
  • 过去几个月来(通过官方文档,在线课程,个人项目)我一直在学习Django和Python,而且由于我开始了解事物,所以我不认为我应该突然改变到.NET或Node.JS,只是因为设置仍然是个谜
  • 我正在研究Macbook,所以Docker是安装MS SQL Server的唯一方法(据我所知)。我确实有一个朋友给我的一台像样的PC(只需要进行设置),如果有人认为这可以解决我的所有问题,我将继续进行切换。

到目前为止,我已经完成/完成的工作(也许这对经历相同过程的人很有帮助):


Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x105ff5158>
Traceback (most recent call last):
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 15, in <module>
    import pyodbc as Database
ImportError: dlopen(/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: image not found

During handling of the above exception, another exception occurred:

*Traceback (most recent call last):
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/models/base.py", line 101, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/models/base.py", line 305, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/models/options.py", line 203, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 17, in <module>
    raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/student8/Desktop/GEMROVER/django_site/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: image not found*

几天来我一直在搜索这些错误,并阅读/观看各种指南,但无济于事...似乎是在抱怨尚未安装pyodbc,但我已经仔细检查了一下,我有。 django-pyodbc-azure同样。

以防万一,这是我的 settings.py 文件中的内容:


    DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': '*********',
        'USER': 'sa',
        'PASSWORD': '********',
        'HOST': '192.168.1.97',
        'PORT': '1433',

        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}

用户名,密码(***出),主机IP和端口的特定组合在Azure Data Studio中可以完美地工作。

  • 我已经按照以下指南将Django安装到了另一个Docker容器中:https://docs.docker.com/compose/django/不幸的是,这显示了如何使用Postgres进行安装,并且不清楚如何更改 requirements.txt 文件使其与SQL Server 配合使用(为了使其与MS SQL配合使用,我该放置在什么地方而不是psycopg2> = 2.7,<3.0?) 。我想这样的事情会进入 docker-compose.yml

db:
    image: "mcr.microsoft.com/mssql/server"
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

(从https://docs.docker.com/compose/aspnet-mssql-compose/借来)

Dockerfile 可能与此处显示的相同:https://docs.docker.com/compose/django/

我还不清楚Django和SQL Server是在同一个Docker映像中还是在两个单独的Docker映像中?我猜一个也一样,因为那是他们的教程处理Django + Postgres的方式?而且,如果要部署网站,那么一切都会顺利进行吗,比如说Apache,或者也许我会选择Azure?

任何帮助将不胜感激!如果这一切都给您带来了极大的麻烦:是的,我是Web开发的新手...我已经尽力阅读了我可能在Stack和其他地方找到的所有内容,但是还没有做到尚未找到解决方案。

更新1:

哦,我取得了一些进步!通常问这个问题会有所帮助。首先我做了

brew install unixodbc

在这里推荐:https://github.com/mkleehammer/pyodbc/issues/87这解决了我之前遇到的错误。然后我又遇到了另一个错误:

Can't open lib 'ODBC Driver 13 for SQL Server'

但是以下内容解决了该问题:

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql mssql-tools

(从Rene对这个问题Can't open lib 'ODBC Driver 13 for SQL Server'? Sym linking issue?的回答中减去有关--no-sandbox的内容–这会带来更多错误,因此我将其删除了)

(通过python manage.py runserver加载了默认的Django网站,所以我认为我终于可以开始使用SQL数据库了!确保一切运行顺利后,我会报告。对于以前的问题,我将不胜感激(例如,是否应该在Docker容器中安装Django,如果是,是否应该与SQL Server共享该容器,还是将其放在单独的容器中?)。

0 个答案:

没有答案