簡體   English   中英

無法使用 mssql-django 連接 django 以存在 MS SQL 服務器

[英]Can't connect django to exist MS SQL Server using mssql-django

嘗試執行 manage.py runserver 時出現錯誤。 我已經為 sql 服務器安裝了 mssql-django 和 odbc 驅動程序 17。 一般來說,我對 django 和 python 非常陌生,但我想使用現有的 ms sql 服務器而不是 Z786D348E95442121DC5B7387E 這是我在setting.py中的代碼:

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "eHospital_DKAG_NSTL",
        "USER": "sa",
        "PASSWORD": "passwod",
        "HOST": "172.16.2.20\sqlservertest",
        "PORT": "1433",
        "OPTIONS": {
            "driver": "ODBC Driver 17 for SQL Server", 
        },
    },
}

這是錯誤消息:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 353, in get_new_connection
    conn = Database.connect(connstr, **args)
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
    self.check_migrations()
  File "F:\Python\Django\fptdb\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
    self.build_graph()
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
    return self._cursor()
  File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 246, in _cursor
    conn = super()._cursor()
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
    self.ensure_connection()
  File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 243, in ensure_connection
    with self.wrap_database_errors:
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 353, in get_new_connection
    conn = Database.connect(connstr, **args)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')

任何幫助表示贊賞。 謝謝你。

“PORT”保留為空白或刪除“PORT” ,否則如果設置任何端口號都會出錯,例如“2244”“9877”甚至“1433” ,這是MSSQL的默認端口號

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "eHospital_DKAG_NSTL",
        "USER": "sa",
        "PASSWORD": "passwod",
        "HOST": "172.16.2.20\sqlservertest",
        "PORT": "",       # Keep it blank
        # "PORT": "1433", # Or remove it
        "OPTIONS": {
            "driver": "ODBC Driver 17 for SQL Server", 
        },
    },
}

您可以看到在mssql-django 的示例中“PORT”保持空白,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'mssql',
        'NAME': 'mydb',
        'USER': 'user@myserver',
        'PASSWORD': 'password',
        'HOST': 'myserver.database.windows.net',
        'PORT': '', # Kept blank

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

# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM