如何使用django-pyodbc(ubuntu 16.04)配置数据库设置Django-MSSQL? [英] How to configure the Database setting Django-MSSQL using django-pyodbc (ubuntu 16.04)?

查看:130
本文介绍了如何使用django-pyodbc(ubuntu 16.04)配置数据库设置Django-MSSQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,目前正在尝试使用其他数据库来保存我的模型(即 MS SQL )。我的数据库部署在docker容器中:

I'm new to Django and currently trying to use another database to save my model (i.e. MS SQL). My database is deployed in a docker container:

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

我还为登录SQL Server创建了一个新用户。

I also create a new user for my login to the SQL Server.

用户名='kucing',密码='xxxxx'

对于我的用户,我可以使用sqlcmd如下访问我的数据库:

With my user, I can use sqlcmd to access my DB as below:

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

因此,我更改了数据库的Django设置,如下所示:

Therefore, I change my Django setting for my DB as shown here:

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

但是当我运行 python manage.py migration 时,我得到了与身份验证相关的错误:

However when I run python manage.py migrate, I get an error related to authentication:

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?

推荐答案

我设法弄清了这个问题,因为我同时在运行Django应用程序和MS SQL Linux中的服务器,我需要将驱动程序更改为 FreeTDS
此链接很有用:如何在Linux中安装freetds?

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. This link is useful: How to install freetds in Linux?

在我完成FreeTDS驱动程序的安装后主机(Ubuntu),我更新了数据库设置,如下所示:

After I finish installing the FreeTDS driver on my host (Ubuntu), I updated the Databases setting as follow:

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

这篇关于如何使用django-pyodbc(ubuntu 16.04)配置数据库设置Django-MSSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆