Home > Software design >  Cannot connect to mssql database using Django (Mac OS)
Cannot connect to mssql database using Django (Mac OS)

Time:11-07

My mssql database is inside the docker container. When trying to connect to the database using azure data studio I don't get any errors, whereas when I'm trying to connect to the db using Django I get the following error:

Traceback (most recent call last):
File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check_migrations()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/core/management/base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/mssql/base.py", line 230, in _cursor
    conn = super()._cursor()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/aleksandr/Desktop/teplo/teplo_back/venv/lib/python3.9/site-packages/mssql/base.py", line 326, 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)')

Django db configuration:

DATABASES = {
     'default': {
        'ENGINE': 'mssql',
        'NAME': 'db_name',
        'HOST': 'localhost',
        'PORT': 1433,
        'USER': 'sa',
        'PASSWORD': 'db_pwd',
         'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        }
    }
}

Installed packages:

Django==3.2.9
mssql-django==1.0
pyodbc==4.0.30

CodePudding user response:

I eventually managed to solve the problem. The trouble was linked to SSL.

First, you have to open the folder /usr/local/opt. There, you can find the [email protected] folder. You have to grab all of the files from there and put them into the openssl folder.

After that, I managed to run Django server successfully.

  • Related