Search code examples
pythonmariadbmysql-pythonlibmysql

Build python-mysqlclient against mariadb-connector-c


I am trying to compile python-mysqlclient against mariadb-connector-c in a Conda environment. That means that the install prefix is not /usr/local but, for example, $HOME/conda/envs/test. I also want to use the auth_gssapi_client.so plugin.

Both packages build, but import MySQLdb raises the following exception:

Traceback (most recent call last):
  File "/opt/emsconda/conda-bld/env/test_tmp/run_test.py", line 2, in <module>
    import MySQLdb
  File "/opt/emsconda/conda-bld/env/lib/python3.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: libmariadb.so.3: cannot open shared object file: No such file or directory

The reason for this is that mysqlclient only searches lib/ but not lib/mariadb even though it was configured with the right path and sucessfully built. I can work around this issue by copying the *.so files to lib/ (or by creating a symlink), but then, it does not find the GSSAPI plugisn …

I build mariadb-connector-c 3.0.2 this way:

mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -DCMAKE_BUILD_TYPE=Release ..
make
make install

I can install it and run mariadb_config which gives this output:

Copyright 2011-2015 MariaDB Corporation AB
Get compiler flags for using the MariaDB Connector/C.
Usage: /opt/emsconda/conda-bld/mysqlclient_1510048680472/_h_env/bin/mariadb_config [OPTIONS]
  --cflags        [-I/opt/emsconda/conda-bld/env/include/mariadb -I/opt/emsconda/conda-bld/env/include/mariadb/mysql]
  --include       [-I/opt/emsconda/conda-bld/env/include/mariadb -I/opt/emsconda/conda-bld/env/include/mariadb/mysql]
  --libs          [-L/opt/emsconda/conda-bld/env/lib/mariadb/ -lmariadb -lpthread -ldl -lm -lssl -lcrypto]
  --libs_r        [-L/opt/emsconda/conda-bld/env/lib/mariadb/ -lmariadb -lpthread -ldl -lm -lssl -lcrypto]
  --libs_sys      [-lpthread -ldl -lm -lssl -lcrypto]
  --version       [10.2.6]
  --socket        [/tmp/mysql.sock]
  --port          [3306]
  --plugindir     [/opt/emsconda/conda-bld/env/lib/mariadb/plugin]
  --tlsinfo       [OpenSSL 1.0.2k]

I then build python-mysqlclient 1.3.12 this way:

MYSQL_CONFIG="$PREFIX/bin/mariadb_config"
echo "mysql_config = $PREFIX/bin/mariadb_config" >> site.cfg
$PYTHON -m pip install -I --no-deps .

There are two possible solutions for this problem:

  1. Configure mariadb-connector-c to directly put its stuff into lib/ – I have not found documentation on how to do this.

  2. Make python-mysqlclient respect the paths that mariadb_config returns – How?


Solution

  • You have to statically link the gssapi stuff into mariadb-connector-c and Python mysqlclient will work.

    This is how to build mariadb-connector-c:

    mkdir build
    cd build
    
    cmake \
        -DCMAKE_INSTALL_PREFIX=$PREFIX \
        -DINSTALL_LIBDIR=lib \
        -DINSTALL_PLUGINDIR=lib/plugin \
        -DWITH_MYSQLCOMPAT=ON \
        -DAUTH_GSSAPI=STATIC \
        -DCMAKE_BUILD_TYPE=Release \
        ..
    
    make
    make install
    
    # WITH_MYSQLCOMPAT only creates links for the libs, not for the binary:
    cd $PREFIX/bin
    ln -s mariadb_config mysql_config