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

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");
    bool ok =;
    qDebug() << ok;

    return a.exec();


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:


(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)


/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 inside.

If 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 ./

What generates in my environment is this: =>  (0x00007fff457d9000) => not found => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007ff9ad66b000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007ff9acfe2000) => /usr/lib/x86_64-linux-gnu/ (0x00007ff9accde000) => /lib/x86_64-linux-gnu/ (0x00007ff9ac915000) => /lib/x86_64-linux-gnu/ (0x00007ff9ac6f8000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007ff9ac2df000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007ff9abf58000) => /lib/x86_64-linux-gnu/ (0x00007ff9abd54000) => /usr/lib/x86_64-linux-gnu/ (0x00007ff9abb52000) => /lib/x86_64-linux-gnu/ (0x00007ff9ab949000) => /lib/x86_64-linux-gnu/ (0x00007ff9ab64d000) => /lib/x86_64-linux-gnu/ (0x00007ff9ab348000) => /lib/x86_64-linux-gnu/ (0x00007ff9ab131000)
/lib64/ (0x00007ff9adaec000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007ff9a99e8000) => /lib/x86_64-linux-gnu/ (0x00007ff9a97a8000)

You now may see the problem: => 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)


What generates in my environment is this: =>  (0x00007fff5d3fe000) => /usr/lib/x86_64-linux-gnu/ (0x00007f98f988d000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007f98f964d000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007f98f8fc4000) => /usr/lib/x86_64-linux-gnu/ (0x00007f98f8cc0000) => /lib/x86_64-linux-gnu/ (0x00007f98f88ca000) => /lib/x86_64-linux-gnu/ (0x00007f98f86b3000) => /lib/x86_64-linux-gnu/ (0x00007f98f84af000) => /lib/x86_64-linux-gnu/ (0x00007f98f8291000) => /lib/x86_64-linux-gnu/ (0x00007f98f7f8c000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007f98f7b73000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007f98f77ec000) => /usr/lib/x86_64-linux-gnu/ (0x00007f98f75ea000) => /lib/x86_64-linux-gnu/ (0x00007f98f73e2000) => /lib/x86_64-linux-gnu/ (0x00007f98f70e5000) => /lib/x86_64-linux-gnu/ (0x00007f98f6ecf000)
/lib64/ (0x00007f98f9dda000) => /opt/Qt5.1/5.1.0/gcc_64/lib/ (0x00007f98f5785000) => /lib/x86_64-linux-gnu/ (0x00007f98f5546000)

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

> Copy back the library

Copy back the correct to the place it should be in:



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


/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:


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 powered by Disqus