Django database connection with Host and Instances with SQL Server

Question:

I’m using SQL Server as the backend for the database. In the setting.py file, I need to use the host name with instance. Due to this, I got the error given below:

The above exception (('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.rn (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [08001] [Microsoft][SQL Server Native Client 11.0]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)')) was the direct cause of the following exception:
# "settings.py"

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'xxxxx',
        'USER': 'xxx',
        'PASSWORD': 'xxxx',
        'PORT': '1433',
        'HOST': 'aaabbb',(hostnameinstance)
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0',
        },
    }
}

How could I resolve this error and connect with my database?

Answers:

Since your settings.py looks fine,I think its a general issue that can be caused from multiple wrong configurations on the database side, such as:

  • TCP/IP Connections are disabled in the SQL Server Configuration Management
  • In TCP/IP properties in the “IP ALL” section port 1433 could not be configured, so it refuses to your requests
  • One of many SQL windows services suddenly could be stopped

I found this stack overflow post very useful with multiple answers that covers each of the points above.

Answered By: JimShapedCoding

Your post is near two years ago, but I faced the same issue now.
By looking on https://github.com/michiya/django-pyodbc-azure/issues/201

It seems that the problem comes from the way the port is included in the connection string. The issue will be solved when the port is specified in this way:

'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'xxxxx',
    'USER': 'xxx',
    'PASSWORD': 'xxxx',
    'HOST': 'aaabbb',(hostnameinstance)
    'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
    },
    'extra_params': 'PORT=1433'

I tried it, it works fine.

Answered By: Rapace-bleu

You can use the latest package mssql-django to connect Django to MSSQL(SQL Server) with SQL Server Authentication. *I use SQL Server 2019 Express.

So, try this code below. *"ENGINE" must be "mssql" and keep it blank for "PORT" because there will be error if setting any port number e.g. "2244", "9877" or even "1433" which is the default port number of MSSQL:

# "settings.py"

DATABASES = {
    'default': {
        'ENGINE': 'mssql',          # Must be "mssql"
        'NAME': 'xxxxx',
        'USER': 'xxx',
        'PASSWORD': 'xxxx',
        'HOST': 'aaabbb',(hostnameinstance)
        'PORT': '',                 # Keep it empty
        # 'PORT': '1433',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    },
}
Answered By: Kai – Kazuya Ito