Skip to main content

Qt 5.1.0 on Ubuntu Linux, MySQL database connection problem, driver not loaded problem

Problem description

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
false

Example Code to reproduce the error:

    #include <QCoreApplication>
    #include <QtSql>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("test");
        db.setUserName("test");
        db.setPassword("xxxxxxxxxx");
        bool ok = db.open();
        qDebug() << ok;
        db.close();
    
        return a.exec();
    }

WTF:

I saw available drivers right there -- f**king "QMYSQL" there, but why am I not be able to load it?!

Ok, ignore me.

Problem Explanation:

No matter it's there or not it still may cause an error -- if you know c++ well, you gotta know that a library dependency problem is a huge headache anyway. (well, some may argue that it's a problem not only within c++. ok, ok... you're right, so what?)

> Qt on Linux via SDK installed

(for those windows users, it should be the same)

If you're using Qt on Ubuntu Linux, and you're installing with the ".run" binary installer of Qt officially.

They might have already install the MySQL plugins for you inside the folder:

$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins

(my environment example is: /opt/Qt5.1/5.1.0/gcc_64/plugins/ folder).

> Qt on Linux via package-manager installed

If you're using apt-get to install the qt5-default package, the plugins where mysql uses is located at:

/usr/lib/i386-linux-gnu/$QT_VERSION/plugins/ (for X86 ubuntu)

or

/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/ (for X86_64 ubuntu)

How to check whether I have mysql driver, or have the right one

> Check Whether I Have One:

  1. Go to the plugins directory described above.
  2. Go inside sqldrivers directory.
  3. Check whether there is a library called libqsqlmysql.so inside.

If libqsqlmysql.so is inside, then you can get QMYSQL inside the QSqlDatabase: available drivers list described in the example code above.

However, the library is not granted to be working due to update of your mysql package or other misoperations.

> Check Whether the Right One:

Use the command below to check whether the library is under a library-dependency problem:

ldd ./libqsqlmysql.so       

What generates in my environment is this:

linux-vdso.so.1 =>  (0x00007fff457d9000)
libmysqlclient_r.so.16 => not found
libQt5Sql.so.5 => /opt/Qt5.1/5.1.0/gcc_64/lib/libQt5Sql.so.5 (0x00007ff9ad66b000)
libQt5Core.so.5 => /opt/Qt5.1/5.1.0/gcc_64/lib/libQt5Core.so.5 (0x00007ff9acfe2000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff9accde000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff9ac915000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff9ac6f8000)
libicui18n.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicui18n.so.51 (0x00007ff9ac2df000)
libicuuc.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicuuc.so.51 (0x00007ff9abf58000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff9abd54000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007ff9abb52000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff9ab949000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007ff9ab64d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff9ab348000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff9ab131000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff9adaec000)
libicudata.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicudata.so.51 (0x00007ff9a99e8000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007ff9a97a8000)

You now may see the problem: libmysqlclient_r.so.16 => not found ---- there's a library he can't find.

(Why cannot he find it? Because I updated the mysql package after I installed the Qt.)

Problem Solve

> Recompile the plugin

  1. Download the latest source code of Qt from qt-project (It should be named like "qt-everywhere-opensource-src-version".tar.gz)
  2. unpack it and go to qtbase/src/plugins/sqldrivers/mysql/ directory
  3. qmake
  4. make
  5. It will generate the right lib file for you in the qtbase/plugins/sqldrivers/ directory

> Check the recompiled the plugin (optional, but recommended)

ldd libqsqlmysql.so

What generates in my environment is this:

linux-vdso.so.1 =>  (0x00007fff5d3fe000)
libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007f98f988d000)
libQt5Sql.so.5 => /opt/Qt5.1/5.1.0/gcc_64/lib/libQt5Sql.so.5 (0x00007f98f964d000)
libQt5Core.so.5 => /opt/Qt5.1/5.1.0/gcc_64/lib/libQt5Core.so.5 (0x00007f98f8fc4000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f98f8cc0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f98f88ca000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f98f86b3000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f98f84af000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f98f8291000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f98f7f8c000)
libicui18n.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicui18n.so.51 (0x00007f98f7b73000)
libicuuc.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicuuc.so.51 (0x00007f98f77ec000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f98f75ea000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f98f73e2000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f98f70e5000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f98f6ecf000)
/lib64/ld-linux-x86-64.so.2 (0x00007f98f9dda000)
libicudata.so.51 => /opt/Qt5.1/5.1.0/gcc_64/lib/libicudata.so.51 (0x00007f98f5785000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f98f5546000)

With libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007f98f988d000) this library, it finally gets to the correct dependent library.

> Copy back the library

Copy back the correct libqsqlmysql.so to the place it should be in:

$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins (SDK installed)

or

/usr/lib/i386-linux-gnu/$QT_VERSION/plugins/ (X86 Ubuntu)

or

/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/ (X86-64 Ubuntu)

Nota Bene:

If you still cannot get to the right thing, you may have your environment set unproperly. Try this in your .bashrc:

export PATH="$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/bin/":$PATH
export LD_LIBRARY_PATH="$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/lib/":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins/":$LD_LIBRARY_PATH
export LIBRARY_PATH="$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/lib/":$LIBRARY_PATH
export LIBRARY_PATH="$QT_INSTALL_PLACE/$QT_VERSION_NUMBER/$COMPILE_TOOL_KIT_NAME/plugins/":$LIBRARY_PATH

My Environment Example:

export PATH="/opt/Qt5.1/5.1.0/gcc_64/bin/":$PATH
export LD_LIBRARY_PATH="/opt/Qt5.1/5.1.0/gcc_64/lib/":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH="/opt/Qt5.1/5.1.0/gcc_64/plugins/":$LD_LIBRARY_PATH
export LIBRARY_PATH="/opt/Qt5.1/5.1.0/gcc_64/lib/":$LIBRARY_PATH
export LIBRARY_PATH="/opt/Qt5.1/5.1.0/gcc_64/plugins/":$LIBRARY_PATH

This means you should have the plugins/ directory search available for the compiler.

Comments

Comments powered by Disqus