I am unable to connect to the MS SQL server using Django (Version- 1.11.3)
Here is the error, I seem to be getting:
django.db.utils.OperationalError: ('08001', u'[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
This is the odbcinst.ini file:
[FreeTDS]
Description=TDS driver (Sybase/MS SQL)
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout=
CPReuse=
UsageCount=2
This is the django settings.py snippet:
DATABASES = {
'mssql': {
'ENGINE':'sql_server.pyodbc',
'NAME': '<NAME>',
'USER': '<USER>',
'PASSWORD':'<password>',
'HOST':'<host-id>',
'OPTIONS':
{
'driver':'FreeTDS'
}
}
}
A few things:
- You'll need to set the connection name as
default
instead ofmssql
- If you're using FreeTDS on Linux, I'd recommend using the
django-pyodbc-azure
Django DB engine:pip install 'django-pyodbc-azure>=1.11,<2'
- You'll need to specify the TDS Verison in your settings.
The upshot of this will be settings like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<NAME>',
'USER': '<USER>',
'PASSWORD':'<password>',
'HOST':'<host-id>',
'PORT': '1433',
'OPTIONS': {
'driver': 'FreeTDS',
'unicode_results': True,
'host_is_server': True,
'extra_params': 'tds_version=7.3',
}
}
}
For windows:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<NAME>',
'USER': '<USER>',
'PASSWORD':'<password>',
'HOST':'<host-id>',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
}
}
}
You can see the django-pyodbc-azure
docs for 1.11 here: https://github.com/michiya/django-pyodbc-azure/tree/azure-1.11
Good luck! These settings can be tricky for SQL Server, but once you get them right, it works well.
I had the same issue while working on Windows 10 and here's what worked for me: Download the driver from here: https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017 And then add 'driver': 'ODBC Driver 17 for SQL Server', to Options in settings.py I hope it helps.
来源:https://stackoverflow.com/questions/50370563/django-unable-to-connect-to-microsoft-sql-server