Everything was working until I restarted my laptop.
I have this setup:
- python 3.8.8
- django 3.1.1
- pyodbc 4.0.30
pyodbc.drivers()shows this:['ODBC Driver 17 for SQL Server']- SQLServer
cat /etc/odbcinst.inireturns this:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib
UsageCount=10
cat /etc/odbc.inireturns this:
# [DSN name]
[MSSQL]
Driver = ODBC Driver 17 for SQL Server
Server = tcp:<my server>,1433
odbcinst -jreturns this:
unixODBC 2.3.9
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/sgalich/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
- in the django settings:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
I reinstalled the ODBC Driver with this official Microsoft instruction. But it didn't help. I'm still failing to start the django server and facing this error:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/mssql/base.py", line 320, in get_new_connection
conn = Database.connect(connstr,
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check_migrations()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/core/management/base.py", line 459, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/mssql/base.py", line 226, in _cursor
conn = super()._cursor()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/sgalich/Desktop/projects/gdp/env/lib/python3.8/site-packages/mssql/base.py", line 320, in get_new_connection
conn = Database.connect(connstr,
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')
That's pretty weird that everything was working for a long time until I restarted my laptop (no OS update, just restarting). May be this issue related to exporting some variables with path? Or may be after the ODBC Driver installation on macOS I need to set up this driver somehow?
P.S.: jdbc driver works without any issues with the same servername, port, user, and password.
UPD 1:
Every time when I'm trying to connect I can see these logs from the ODBC driver:
Exit:[SQL_SUCCESS]
<...>
Str In = [DRIVER=ODBC Driver 17 for SQL Server;SERVER=<my_server>,1433;UID=<my_username>;PWD=<my_password>;DATAB...][length = 160 (SQL_NTS)]
<...>
Exit:[SQL_SUCCESS]
<...>
Exit:[SQL_SUCCESS]
<...>
Exit:[SQL_ERROR]
<...>
DIAG [08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection
DIAG [01S00] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute
So there is a success message with my real password in it, and then follow the error message, with Invalid connection string attribute and ********** instead of my real password in it. Can some module remove my password with *? Maybe this is what prevent me from the connection?
UPD 2:
Well, with this answer it started to work, but python started to quit unexpectedly. I reinstalled brew, openssl, all the odbc libraries. And anyway isql -k -v returns this:
[S1T00][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
[08001][Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2AF9
[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.
[ISQL]ERROR: Could not SQLDriverConnect