简体   繁体   English

Django 4.0.3 和 mssql-django 1.1.2 将无法连接到 SQL Server Express 2019

[英]Django 4.0.3 and mssql-django 1.1.2 will not connect to SQL Server Express 2019

I have been going by this tutorial for mssql-django https://docs.microsoft.com/en-us/samples/azure-samples/mssql-django-samples/mssql-django-samples/我一直在学习 mssql-django 的本教程https://docs.microsoft.com/en-us/samples/azure-samples/mssql-django-samples/mssql-django-samples/

My setup is Django 4.0.3 and mssql-django 1.1.2 will not connect to SQL Server Express 2019 on Windows 10, with the SQL Server Express 2019 running locally.我的设置是 Django 4.0.3,mssql-django 1.1.2 将无法连接到 Windows 10 上的 SQL Server Express 2019,而 SQL Server Express 2019 在本地运行。

I have searched both GitHub and Stack Overflow for examples of mssql-django configurations, but not much is turning up.我已经在 GitHub 和 Stack Overflow 上搜索了 mssql-django 配置的示例,但发现的并不多。

I am not sure what I am doing wrong.我不确定我做错了什么。

The main things I have been trying is betting on the NAME, USER or HOST parameter being incorrect, as the others seem to be correct.我一直在尝试的主要事情是押注 NAME、USER 或 HOST 参数不正确,因为其他参数似乎是正确的。

  • Should NAME be: avxdb or mxlaptop\\avxdb or something else? NAME应该是: avxdbmxlaptop\\avxdb还是其他?
  • Should USER be: pyadmin@mxlaptop or just pyadmin ? USER应该是: pyadmin@mxlaptop还是只是pyadmin
  • Should HOST be: mxlaptop\SQLEXPRESS , mxlaptop\ss2019 , mxlaptop , or 127.0.0.1 ? HOST应该是: mxlaptop\SQLEXPRESSmxlaptop\ss2019mxlaptop还是127.0.0.1
DATABASES = {
    'default': {
        "ENGINE": "mssql",
        "NAME": "avxdb",
        "USER": "pyadmin@mxlaptop",
        "PASSWORD": "Python",
        "HOST": "mxlaptop\SQLEXPRESS",
        "PORT": "1433",
        "OPTIONS": {"driver": "ODBC Driver 17 for SQL Server", 
        },
    }
}

Edit: sorry, I did not post the error:编辑:对不起,我没有发布错误:

Traceback (most recent call last):
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\mssql\base.py", line 329, in get_new_connection
    conn = Database.connect(connstr,
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (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. (10061)')

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

Traceback (most recent call last):
  File "C:\Users\metamuas\source\vscode\avx\avx_backend\manage.py", line 22, in <module>
    main()
  File "C:\Users\metamuas\source\vscode\avx\avx_backend\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\commands\inspectdb.py", line 46, in handle
    for line in self.handle_inspection(options):
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\core\management\commands\inspectdb.py", line 62, in handle_inspection
    with connection.cursor() as cursor:
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
    return self._cursor()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\mssql\base.py", line 230, in _cursor
    conn = super()._cursor()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
    self.ensure_connection()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 243, in ensure_connection
    with self.wrap_database_errors:
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\metamuas\.virtualenvs\avx-BMkvCBKN\lib\site-packages\mssql\base.py", line 329, in get_new_connection
    conn = Database.connect(connstr,
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (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. (10061)')

I have a normal Python script that can connect to the database via pyodbc :我有一个普通的 Python 脚本,可以通过pyodbc连接到数据库:

connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
                                'SERVER=mxlaptop;'
                                'DATABASE=avxdb;'
                                'UID=pyadmin;'
                                'PWD=Python;')

So it turns out that TCP/IP must be enabled via SQL Server Configuration. 所以事实证明,必须通过 SQL Server 配置启用 TCP/IP。

Also, here is the correct connection settings I discovered:此外,这是我发现的正确连接设置:

DATABASES = {
    'default': {
        "ENGINE": "mssql",
        "NAME": "avxdb",
        "USER": "pyadmin",
        "PASSWORD": "Python",
        "HOST": "mxlaptop",
        "PORT": "1433",
        "OPTIONS": {"driver": "ODBC Driver 17 for SQL Server", 
        },
    }
}

So I had the wrong USER and the wrong HOST .所以我有错误的USER和错误的HOST

TCP/IP must be enabled via SQL Server Configuration TCP/IP 必须通过 SQL Server 配置启用

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

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