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

Adam posted @ Tue, 20 Aug 2013 05:36:42 +0800 in C++ with tags mysql c++ Qt SQL , 32608 readers

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

  • 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).
 

  • 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 32bits ubuntu

or

/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/ for 64bits ubuntu


How to check whether I have mysql driver, or have the right 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.

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

 

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

or

/usr/lib/i386-linux-gnu/$QT_VERSION/plugins/

or

/usr/lib/x86_64-linux-gnu/$QT_VERSION/plugins/


Nota Bene:

  • If you still cannot get to the right thing, you may have your environment set unproperly. Try this:
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

qtsqlnewbie said:
Wed, 09 Oct 2013 20:19:12 +0800

Hello,
thanks for detailed description of what to do, I encountered identical problem (libmysqlclient_r.so.16 => not found)
However, I can't build MySQL plugin. After trying "qmake" in qtbase/src/plugins/sqldrivers/mysql/ directory I get a warning:

WARNING: (...)qtbase/src/plugins/sqldrivers/qsqldriverbase.pri:4: Unable to find file for inclusion qt_plugin
Project MESSAGE: Warning: unknown QT: core-private
Project MESSAGE: Warning: unknown QT: sql-private

And then "make" fails:

main.cpp:42:30: fatal error: qsqldriverplugin.h: No such file or directory
compilation terminated.

Do you perhaps know what I should do?

Avatar_small
Adam said:
Fri, 11 Oct 2013 00:53:54 +0800

U sure that you've got the qmake version right? `qmake --version` to check.

It'll show up like "Using Qt version 5.1.1 in /opt/Qt5.1.1/5.1.1/gcc_64/lib".
If your qt version is not compatible with the source code version, you'll not be able to get it right.

Make it sure... and download the corresponding qt source code version to your project. :P

Avatar_small
Adam said:
Fri, 11 Oct 2013 00:54:57 +0800

It seems to me that you're using qt4.

qtsqlnewbie said:
Fri, 18 Oct 2013 19:17:47 +0800

Some time ago I was using qt4, then changed to qt5. It appears that I hadn't removed qt4 completely because when calling: qmake in terminal it was calling qmake from qt4. I gave absolute path to qt5 qmake and QMYSQL plugin compiled.

Thank you very much :)

Shekhar said:
Tue, 12 Nov 2013 12:23:35 +0800

Hello everyone, The description given above is really helpful. Further I also faced the same problem as qtsqlnewbie..I installed qt5-qmake and libqt5sql5-mysql packages.

The qmake command is : sudo qmake "INCLUDEPATH+=/usr/include/mysql" "INCLUDEPATH+=/opt/qt-everywhere-opensource-src-5.1.1/qtbase/include" "INCLUDEPATH+=/opt/qt-everywhere-opensource-src-5.1.1/qtbase/include/QtCore/5.1.1" "INCLUDEPATH+=/opt/qt-everywhere-opensource-src-5.1.1/qtbase/include/QtSql/5.1.1" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro

It runs without any error but on using amke command I get following error.

In function `QMYSQLDriver::QMYSQLDriver(QObject*)':
/opt/qt-everywhere-opensource-src-5.1.1/qtbase/src/plugins/sqldrivers/mysql/../../../sql/drivers/mysql/qsql_mysql.cpp:1119: undefined reference to `QSqlDriver::QSqlDriver(QSqlDriverPrivate&, QObject*)'
.obj/release-shared/qsql_mysql.o: In function `QMYSQLDriver::QMYSQLDriver(st_mysql*, QObject*)':
/opt/qt-everywhere-opensource-src-5.1.1/qtbase/src/plugins/sqldrivers/mysql/../../../sql/drivers/mysql/qsql_mysql.cpp:1131: undefined reference to `QSqlDriver::QSqlDriver(QSqlDriverPrivate&, QObject*)'
collect2: error: ld returned 1 exit status
make: *** [../../../../plugins/sqldrivers/libqsqlmysql.so] Error 1
 
Please help...
Avatar_small
Adam said:
Thu, 14 Nov 2013 19:45:58 +0800

qmake will definitly not report any error for ya, cuz the 'mysql.pro' is with no error anyway.

I don't know why you added these things using qmake commandline but not to the environment (you may delete them after).

The problem you faced is because qmake did not make the sql kernel driver stuffs in dir `qtbase/src/sql/kernel/`

Maybe you should try setting them to the environment, and remain using a clean `sudo qmake`.

Levan said:
Wed, 27 Nov 2013 19:21:05 +0800

I downloaded "qt-everywhere-opensource-src-5.1.1", since I Qt5.1.1, but there was no folder "qtbase", how to be? Please give a link to jump. Thank you.

Levan said:
Wed, 27 Nov 2013 22:32:13 +0800

Sorry, folder "qtbase" there, but when you compile crash bug.

../../../sql/drivers/mysql/qsql_mysql.cpp:44:40: fatal error: QtSql/private/qsqldriver_p.h: No such file or directory
compilation terminated.
make: *** [.obj/release-shared/qsql_mysql.o] Error 1

Avatar_small
Adam said:
Wed, 04 Dec 2013 00:25:35 +0800

I quite don't understand why you're getting this kind of compile error. Try use `locate qsqldriver_p.h` to get the file path, and I don't believe that it's not there...

shawnan said:
Mon, 17 Mar 2014 19:38:29 +0800

great. thanks a lot!

shawnan said:
Mon, 17 Mar 2014 19:40:04 +0800

qtbase-opensource-src-5.1.1.tar.gz ?

Avatar_small
Adam said:
Tue, 18 Mar 2014 11:34:48 +0800

'qt-everywhere-opensource-src' should have contained everything for compiling, while getting from SVN will lead you to getting submodules.

Robert said:
Sun, 13 Apr 2014 23:13:53 +0800

Wow..this is very helpful! Thanks

tucu said:
Thu, 16 Oct 2014 12:51:22 +0800

Excellent! Bravo!

saurabh said:
Tue, 12 Jun 2018 15:45:49 +0800

Hello Adam,

Thank you very much for this article but When I do "qmake" after changing to "/src/plugins/sqldrivers/mysql/" directory then I get following error.

qmake

Project ERROR: Could not find feature framework.

Project ERROR: Could not find feature c++11.

I have downloaded qt-everywhere-opensource-src-5.8.0 on my Ubuntu machine. Please inform me if you need any other information from me.

Thanks and Regards Saurabh

saurabh said:
Tue, 12 Jun 2018 20:20:14 +0800

Hello Developer,

I did everything mentioned above successfully and also checked dependency by giving command

>> ldd libqsqlmysql.so

in qtbase/src/plugins/sqldrivers/mysql/ folder and found that there is no missing dependency.

But still I am getting same error

SqlDatabase: QSQLite driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3

after I try to open QSQLite database.

I have tried to open SQlite database using Qt version 5.4.1 and Qt5.5.1 but was not able to do it.

Please let me know if you need any other information from me and what should I do now to eliminate above problem.

Many thanks :)

Avatar_small
Adam said:
Wed, 13 Jun 2018 09:31:54 +0800

Hi Saurabh,

For comment #18, C++11 compiler is required which I think starting from g++-5 is required.

For comment #19, the `QSQLITE` should be natively included. Inferred from the comment, you might got the driver name wrong where `QSQLite` might not work since Qt requires all capital letters.

```
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
```


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter