Home > Mobile >  How to use mariadb python connector in Docker?
How to use mariadb python connector in Docker?

Time:12-01

I want to use the Python mariadb connector in a python container. Yet, until now, I've met only troubles and now I segfault when trying to pass arguments to my SQL queries. here are my dockerfile and a script that highlight the problem.

FROM python:3.11-buster

RUN apt-get install gcc wget

# https://stackoverflow.com/questions/74429209/mariadb-in-docker-mariadb-connector-python-requires-mariadb-connector-c-3-2
RUN wget https://dlm.mariadb.com/2678579/Connectors/c/connector-c-3.3.3/mariadb-connector-c-3.3.3-debian-buster-amd64.tar.gz -O - | tar -zxf - --strip-components=1 -C /usr

WORKDIR /appli

RUN pip3 install pynetdicom>=2.0 mariadb==1.1.4

COPY . .

CMD ["python3", "AEServer.py"]
import mariadb

conn = mariadb.connect(user='myself', password='mypass', host=ip, database='db') # Successfully created connection
cursor = conn.cursor()
data = ('1.2.392', 'NAME^SURNAME^', 'BDAY', 'DX', 1) # Placeholder
insert_stmt = ("INSERT Examens VALUES ('%s', '%s', '%s', '%s', %i, 'COMING')" % data)
segv_stmt = ("INSERT Examens VALUES (?, ?, ?, ?, ?, 'COMING')", data)
segv = "INSERT Examens VALUES (?, ?, ?, ?, ?, 'COMING')"

cursor.execute(insert_stmt) # GOOD
cursor.execute(segv, data) # Segmentation fault

First, I tried adding mariadb in my pip install in the dockerfile. It failed, because I didn't have the mariadb/c connector.

Then, I tried adding it by installing libmariadb3 and libmariadb-dev, according to this page of the documentation : https://mariadb.com/docs/ent/connect/programming-languages/c/install/ but I failed again, because the package fetched by the manager weren't up to date, so I had a version conflit when pip installing.

Then, I tried the following reference : Mariadb in Docker: MariaDB Connector/Python requires MariaDB Connector/C >= 3.2.4, found version 3.1.16, to wget the connector and compiling it myself in the Dockerfile. I succeeded to create my container and I could create mariadb.coonnection and cursor so I thought it was over.

Finally when trying to use insert statement with placeholder (either '%s' or '?'), I am met with a segfault. I tried various associations of sources images (3.11:bullseye, 3.11:buster), connectors (version 3.3.3, 3.2.7) and mariadb (version 1.1.0, 1.1.2, 1.1.4), but all of them segfault.

What I want is a container with python3, my dependancy pynetdicom, and the ability to interract safely with a mariadb Database. A Dockerfile fulfilling the prevous prerequisites should be enough for me to move forward, but I also wish to know why things did happen this way.

Edit : Posted some credentials used in dev, removed them.

CodePudding user response:

Even if you installed a newer MariaDB Connector/C version, the preinstalled 3.1.13 version from 3.11-buster is still installed.

After installation of MariaDB Connector/C 3.3.3 you have 2 versions installed:

  • /usr/lib/x86_64-linux-gnu/libmariadb.so.3
  • /usr/lib/mariadb/libmariadb.so.3

Now when running your python script, it crashes in /usr/lib/x86_64-linux-gnu/libmariadb.so.3.

To force Python to use the newer MariaDB Connector/C library just add

ENV LD_PRELOAD=/usr/lib/mariadb/libmariadb.so or ENV LD_LIBRARY_PATH=/usr/lib/mariadb

to your Dockerfile.

  • Related