简体   繁体   中英

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/

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.

I have searched both GitHub and Stack Overflow for examples of mssql-django configurations, but not much is turning up.

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.

  • Should NAME be: avxdb or mxlaptop\\avxdb or something else?
  • Should USER be: pyadmin@mxlaptop or just pyadmin ?
  • Should HOST be: mxlaptop\SQLEXPRESS , mxlaptop\ss2019 , mxlaptop , or 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 :

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.

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 .

TCP/IP must be enabled via SQL Server Configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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