Azure RHEL 8 VM. pyodbc or django can't connect to SQL Server: pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib ... file not found (0) (SQLDriverConnect)")

Cliff 1 Reputation point
2022-08-15T18:23:52.4+00:00

I'm on an Azure RHEL 8 VM trying to connect to an Azure SQL Server using pyodbc and django, but got a "Can't open lib...file not found" error. The ODBC library files are in those locations.
However, the connection to SQL Server works with pymssql. Connecting using isql with the ODBC drivers also work.

I've done the same installation on an Azure Ubuntu 18.04 VM and the connection works for both pyodbc and django. I only have this problem with an Azure RHEL 8 VM

-------------------------

Red Hat Enterprise Linux release 8.6 (Ootpa)
Python 3.9

--------------------------

Installed Microsoft ODBC 17 and ODBC 18 based on instructions below. I tried installing each individually but the problem is the same:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16#redhat18
pip install pyodbc

-------------------------

$ odbcinst -j

unixODBC 2.3.9
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/xxxx/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

-------------------------------

$ cat /etc/odbcinst.ini

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.1.1
UsageCount=1

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1
UsageCount=1

-----------------

ODBC Driver files are present

$ ls -l /opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.1.1
-rwxr-xr-x. 1 root root 2079488 Jul 15 21:00 /opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.1.1

$ ls -l /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1
-rwxr-xr-x. 1 root root 2061248 Jun 23 21:23 /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1

-----------------------

Check driver dependencies. No errors

$ ldd /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1
linux-vdso.so.1 (0x00007ffd6f7cc000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fd417499000)
librt.so.1 => /lib64/librt.so.1 (0x00007fd417291000)
libodbcinst.so.2 => /lib64/libodbcinst.so.2 (0x00007fd41707b000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fd416d91000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fd416b3c000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd4167a7000)
libm.so.6 => /lib64/libm.so.6 (0x00007fd416425000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd41620d000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd415fed000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd415c28000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd417aa8000)
libltdl.so.7 => /lib64/libltdl.so.7 (0x00007fd415a1e000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fd415807000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fd415603000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fd4153f2000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fd4151ee000)
libcrypto.so.1.1 => /lib64/libcrypto.so.1.1 (0x00007fd414d05000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fd414aee000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd4148c4000)
libz.so.1 => /lib64/libz.so.1 (0x00007fd4146ac000)
libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007fd414428000)

-----------------------

$ pip freeze

asgiref==3.5.2
certifi @ file:///opt/conda/conda-bld/certifi_1655968806487/work/certifi
Django==4.0.7
mssql-django==1.1.3
pymssql==2.2.5
pyodbc==4.0.34
pytz==2022.1
sqlparse==0.4.2

---------------------

Can't open lib error when connecting using pyodbc

$ python
Python 3.9.0 (default, Nov 15 2020, 14:28:56)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=<server>;'
'Database=<database>;'
'UID=<user_name>;'
'PWD=<password>;'
'Trusted_Connection=no;')

Traceback (most recent call last):
File "<console>", line 1, in <module>
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1' : file not found (0) (SQLDriverConnect)")

---------------------------

django settings.py DATABASES. Actual credentials replaced with dummy tokens

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'trial_design': {
'ENGINE': 'mssql',
'NAME': <database>,
'HOST': <server>',
'POST': '1433',
'USER': <user_name>,
'PASSWORD': <password>,
"OPTIONS": {
"driver": "ODBC Driver 17 for SQL Server",
"extra_params": "Encrypt=yes;TrustServerCertificate=no",
},
},
}

------------------------

Can't open lib error when connecting using django

$ python manage.py shell

Python 3.9.0 (default, Nov 15 2020, 14:28:56)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
import django
print(django.db.connection.ensure_connection())
Traceback (most recent call last):
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
self.connect()
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/mssql/base.py", line 353, in get_new_connection
conn = Database.connect(connstr, **args)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1' : file not found (0) (SQLDriverConnect)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
self.connect()
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
self.connect()
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/cliff/anaconda3/envs/rwd/lib/python3.9/site-packages/mssql/base.py", line 353, in get_new_connection
conn = Database.connect(connstr, **args)
django.db.utils.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1' : file not found (0) (SQLDriverConnect)")

-------------------------------

pymssql connection gives no errors and works
import pymssql
conn = pymssql.connect(server=<server>, user=<user>, password=<password>, database=<database>)
cursor = conn.cursor()

--------------------------------

Connection using isql works

isql -v -k "DRIVER={ODBC Driver 17 for SQL Server};Server=<server>;Database=<database>;UID=<username>;PWD=<password>;Trusted_Connection=no;"

isql -v -k "DRIVER={ODBC Driver 18 for SQL Server};Server=<server>;Database=<database>;UID=<username>;PWD=<password>;Trusted_Connection=no;"

Azure SQL Database
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.