0

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.\r\n (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': 'aaa\bbb',(hostname\instance)
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0',
        },
    }
}

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

3
  • Did you check that port 1433 is open on your host? Commented Dec 16, 2019 at 8:25
  • Yes. It is open Commented Dec 16, 2019 at 9:08
  • There is a new answer which solves this directly, you can choose to accept it or just ignore it/upvote Commented Aug 7, 2021 at 17:22

3 Answers 3

1

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': 'aaa\bbb',(hostname\instance)
    'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
    },
    'extra_params': 'PORT=1433'

I tried it, it works fine.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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': 'aaa\bbb',(hostname\instance)
        'PORT': '',                 # Keep it empty
        # 'PORT': '1433',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    },
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.