简体   繁体   English

如何使用django-pyodbc(ubuntu 16.04)配置数据库设置Django-MSSQL?

[英]How to configure the Database setting Django-MSSQL using django-pyodbc (ubuntu 16.04)?

I'm new to Django and currently trying to use another database to save my model (ie MS SQL ). 我是Django的新手,当前正在尝试使用另一个数据库来保存我的模型(即MS SQL )。 My database is deployed in a docker container: 我的数据库部署在Docker容器中:

903876e64b67        microsoft/mssql-server-linux   "/bin/sh -c /opt/mssq"   5 hours ago         Up 5 hours          0.0.0.0:8888->1433/tcp             nauseous_williams

I also create a new user for my login to the SQL Server. 我还为登录SQL Server创建了一个新用户。

Username='kucing', password='xxxxx'

With my user, I can use sqlcmd to access my DB as below: 对于我的用户,我可以使用sqlcmd如下访问我的数据库:

sqlcmd -S localhost,8888 -U kucing -P 'xxxxx'

Therefore, I change my Django setting for my DB as shown here: 因此,如下所示,我为数据库更改了Django设置:

DATABASES = {
    'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'videogame', #The DB name
    'USER': 'kucing',
    'PASSWORD': 'xxxxx',
    'HOST': 'localhost',
    'PORT': '8888',

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

However when I run python manage.py migrate , I get an error related to authentication: 但是,当我运行python manage.py migrate ,出现与身份验证相关的错误:

Traceback (most recent call last):
File "/home/luca/git/learnPython/DjangoTicTacToe/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  self.connect()
File "/home/luca/git/learnPython/DjangoTicTacToe/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
  self.connection = self.get_new_connection(conn_params)
File "/home/luca/git/learnPython/DjangoTicTacToe/lib/python3.5/site-packages/sql_server/pyodbc/base.py", line 302, in get_new_connection
  timeout=timeout)
  pyodbc.Error: ('28000', "[28000] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'kucing'. (18456) (SQLDriverConnect)")

Did I wrongly set up my configuration? 我是否错误设置了配置? Should I update my setting? 我应该更新我的设置吗?

I assume you're hosting on Windows, since you're attempting to connect to localhost . 我假设您正在Windows上托管,因为您正尝试连接到localhost I'd highly recommend using the django-pyodbc-azure engine ( https://github.com/michiya/django-pyodbc-azure ), as it can be used for both local SQL Server or on Azure, and is the best maintained Django SQL Server package I've seen over several years of use. 我强烈建议使用django-pyodbc-azure引擎( https://github.com/michiya/django-pyodbc-azure ),因为它可以用于本地SQL Server或在Azure上,并且维护得最好我已经看过Django SQL Server软件包多年了。 To install: 安装:

pip install django-pyodbc-azure

This will also install the pyodbc dependency. 这还将安装pyodbc依赖项。 Since you're running Django on Windows (otherwise, you wouldn't be connecting to localhost), you can use the native client. 由于您是在Windows上运行Django(否则,您将不会连接到本地主机),因此可以使用本机客户端。 Here's what I'd start with for your settings: 这是我要开始进行的设置:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'videogame',
        'USER': 'kucing',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': '8888',

        'OPTIONS': {
            'driver': 'SQL Server Native Client 13.0',
        },
    },
}

Also, you should never post your password on Stack Overflow! 另外,您永远不要将密码发布在Stack Overflow上! I would highly recommend changing it. 我强烈建议您更改它。 Typically, if you post your password as xxxxx or something like that, people will understand why. 通常,如果您以xxxxx或类似的密码发布密码,人们会理解原因。 Good luck! 祝好运!

I've manage to figure out the issue. 我设法找出问题所在。 Because, I'm running both the Django application and the MS SQL server in linux, I need to change my driver to FreeTDS . 因为我在linux中同时运行Django应用程序和MS SQL Server,所以需要将驱动程序更改为FreeTDS This link is useful: How to install freetds in Linux? 该链接很有用: 如何在Linux中安装freetds?

After I finish installing the FreeTDS driver on my host (Ubuntu), I updated the Databases setting as follow: 在主机(Ubuntu)上完成FreeTDS驱动程序的安装后,我如下更新了数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'videogame',
        'USER': 'sa',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': '8888',
        'OPTIONS' : {
            'driver': 'FreeTDS',
            'unicode_results': True,
            'host_is_server': True,
            'extra_params': 'tds_version=7.0;',
            }
    }
}

Then I create a superuser using this command: 然后,我使用以下命令创建超级用户:

python manage.py createsuperuser

And Lastly, I do the Database migration: 最后,我进行数据库迁移:

python manage.py makemigrations; python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM