Error connecting to MariaDB from Linux Environment: ImportError: libmariadb.so.3: cannot open shared object file: No such file or directory

Question:

I am trying to connect to MariaDB using a python connector from a linux machine and while doing so I am getting the following error;

(venv) [admin@server connector_testing]$ python mariadb_connector.py
Traceback (most recent call last):
  File "mariadb_connector.py", line 15, in <module>
    engine = create_engine(CONNECTION_STRING)
  File "<string>", line 2, in create_engine
  File "/home/admin/Downloads/venv/lib/python3.7/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
    return fn(*args, **kwargs)
  File "/home/admin/Downloads/venv/lib/python3.7/site-packages/sqlalchemy/engine/create.py", line 560, in create_engine
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/admin/Downloads/venv/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mariadbconnector.py", line 106, in dbapi
    return __import__("mariadb")
  File "/home/admin/Downloads/venv/lib/python3.7/site-packages/mariadb/__init__.py", line 10, in <module>
    from ._mariadb import (
ImportError: libmariadb.so.3: cannot open shared object file: No such file or directory

Steps taken to Install mariadb from PYPI:
pip install sqlalchemy
pip install mariadb

Note: additionally to pip install mariadb from PYPI, in a linux systems, the pip script additionally searched for mariadb_config which has to be downloaded and installed from MariaDB website — [Connector/C]. I have installed Connector/C and was able to successfully install mariadb library from PYPI in the python environment.

Also, when I was installing MariaDB Connector-C I noticed that in "lib/mariadb" directory we have "libmariadb.so.3"

mariadb connector-c had these 3 directories:
(venv) [admin@server mariadb-connector-c-3.1.9-centos7-amd64]$ ls -rlt
drwx------. 4 admin admin 38 Jun 19  2020 lib
drwx------. 3 admin admin 21 Jun 19  2020 include
drwx------. 2 admin admin 28 Jun 19  2020 bin

(venv) [admin@server lib]$ ls -lrt
drwx------. 2 admin admin 27 Jun 19  2020 pkgconfig
drwx------. 3 admin admin 90 Jun 19  2020 mariadb

(venv) [admin@server mariadb]$ ls -lrt
-rwx------. 1 admin admin 1525776 Jun 19  2020 libmariadb.so.3
-rw-------. 1 admin admin 2677912 Jun 19  2020 libmariadbclient.a
drwx------. 2 admin admin     186 Jun 19  2020 plugin
lrwxrwxrwx. 1 admin admin      15 Jun 19  2020 libmariadb.so -> libmariadb.so.3

So I have copied it to the location where I am getting the error: /home/admin/Downloads/venv/lib/python3.7/site-packages/mariadb, but still I am getting the same error.

(venv) [admin@server mariadb]$ pwd
/home/admin/Downloads/venv/lib/python3.7/site-packages/mariadb

(venv) [admin@server connector_testing]$ cd /home/admin/Downloads/venv/lib/python3.7/site-packages/mariadb
(venv) [admin@server mariadb]$ ls -lrt
-rwx--x--x. 1 admin admin  465280 Nov 30 14:46 _mariadb.cpython-37m-x86_64-linux-gnu.so
-rw-------. 1 admin admin     928 Nov 30 14:46 __init__.py
drwx------. 2 admin admin      37 Nov 30 14:46 __pycache__
drwx------. 3 admin admin     119 Nov 30 14:46 constants
-rwx------. 1 admin admin 1525776 Dec  3 10:08 libmariadb.so.3
-rw-------. 1 admin admin 2677912 Dec  3 10:08 libmariadbclient.a
lrwxrwxrwx. 1 admin admin      15 Dec  3 10:08 libmariadb.so -> libmariadb.so.3
drwx------. 2 admin admin     186 Dec  3 10:08 plugin

If anyone has faced similar issues with mariadb connector, can you please point me in resolving the error.

Thanks in Advance, Nikhil I

Asked By: nikhil int

||

Answers:

As the error message says, MariaDB Connector/Python cannot find the shared library from Connector/C.

If you didn’t install Connector/C via package manager (usually package manager takes care of library paths) you have to specify where shared objects can be found.

Solution 1:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/pathof/libmariadbso
export LD_LIBRARY_PATH

Solution 2 (permanent):

Add the path to /etc/ld.so.conf and run ldconfig afterwards (as root)

Answered By: Georg Richter

I managed to solve the issue by using just another SQL – driver (pymysql) to connect to Maria DB.

Old:

CONNECTION_STRING = 'mariadb+mariadbconnector' + '://{}:{}@{}:{}/{}'.format(USERNAME, quote(PASSWORD), HOST, str(PORT), DB_NAME)

Modified to:

CONNECTION_STRING = 'mariadb+pymysql' + '://{}:{}@{}:{}/{}'.format(USERNAME, quote(PASSWORD), HOST, str(PORT), DB_NAME)

Thanks

Answered By: nikhil int

Just install yum install MariaDB-shared.

Answered By: Ram Krishna Dwivedi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.