QSqlDatabase: QMYSQL driver not loaded



  • I run ubuntu 14.04 and i installed Qt4 and Qt5. I have created and implemented a project in Qt5 ( specifically : Based on Qt 5.4.1 (GCC 4.6.1, 32 bit) ).

    I try in my programm to make connection to a database ( ΜySql ) but I can not.

    When I run this code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("myTEST_db");
        db.setUserName("root");
        db.setPassword("123456789");
        if (!db.open())
        {
          QMessageBox::critical(0, QObject::tr("Database Error"),
                    db.lastError().text());
        }
    

    I get this error :

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

    When i run this code :

        QPluginLoader loader;
        loader.setFileName("/home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so");
        qDebug() << "PluginLoader worked =" << loader.load();
        qDebug() << "Plugin Loader error=" << loader.errorString();
    
        qDebug() << QCoreApplication::libraryPaths();
        QCoreApplication::addLibraryPath("/home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so");
        qDebug() << QSqlDatabase::drivers();
    

    I get this error :

    PluginLoader worked = false
    Plugin Loader error= "Cannot load library /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so: (libmysqlclient_r.so.16: cannot open shared object file: No such file or directory)"
    ("/home/myuser/Qt/5.4/gcc/plugins", "/home/myuser/Qt TestProject/build-TestProject-Desktop_Qt_5_4_1_GCC_32bit-Debug")
    ("QSQLITE", "QPSQL", "QPSQL7")
    

    I can not understand what is happening, please very much you can help me?
    I stress that I have installed and QT4 (maybe somewhere confused).

    Thank you in advance.


  • Moderators

    Did you check that /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so really exists? Is it 32bit?
    One note: if you add a directory to library path then do not pass the file name! It must be a directory.



  • @jsulm Firstly thank you very much friend for so prompt reply.
    Yes I have 32-bit.

    As for the file:

    ls Qt/5.4/gcc/plugins/sqldrivers/
    libqsqlite.so  libqsqlmysql.so  libqsqlpsql.so
    

    one more effort:

        qDebug() << QCoreApplication::libraryPaths();
        QCoreApplication::addLibraryPath("/home/myuser/Qt/5.4/gcc/plugins/sqldrivers/");
        qDebug() << QSqlDatabase::drivers();
    
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("myTEST_db");
        db.setUserName("root");
        db.setPassword("123456789");
        if (!db.open())
        {
          QMessageBox::critical(0, QObject::tr("Database Error"),
                    db.lastError().text());
    

    Error :

    ("/home/myuser/Qt/5.4/gcc/plugins", "/home/myuser/Qt TestProject/build-TestProject-Desktop_Qt_5_4_1_GCC_32bit-Debug")
    ("QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7")
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
    

  • Lifetime Qt Champion

    Hi,

    Do you have the MySQL client libraries installed on your system ?



  • @SGaist If i do not do wrong , yes.
    Extra now again I ran :

    sudo apt-get install mysql-client
    sudo apt-get install libmysqlclient-dev
    sudo apt-get install libqt5sql5-mysql
    sudo apt-get install libmysqlclient16-dev
    sudo apt-get install libmysql++-dev
    

    Because I despaired, that I have found of libraries i have installing.

    But absolutely nothing. I did not expect to be so hard. :(


  • Lifetime Qt Champion

    Can you run ldd on Qt's MySQL plugin ?



  • @SGaist
    this is what you mean;

    :~/Qt/5.4/gcc/plugins/sqldrivers$ ldd libqsqlmysql.so
    	linux-gate.so.1 =>  (0xb76e3000)
    	libmysqlclient_r.so.16 => not found
    	libQt5Sql.so.5 => /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0xb7672000)
    	libQt5Core.so.5 => /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0xb712d000)
    	libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb702b000)
    	libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb6e7d000)
    	libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb6e61000)
    	libicui18n.so.53 => /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/./../../lib/libicui18n.so.53 (0xb6c0a000)
    	libicuuc.so.53 => /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/./../../lib/libicuuc.so.53 (0xb6a8a000)
    	libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb6a85000)
    	libgthread-2.0.so.0 => /usr/lib/i386-linux-gnu/libgthread-2.0.so.0 (0xb6a82000)
    	librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xb6a79000)
    	libglib-2.0.so.0 => /lib/i386-linux-gnu/libglib-2.0.so.0 (0xb696d000)
    	libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb6926000)
    	libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb6908000)
    	/lib/ld-linux.so.2 (0xb76e4000)
    	libicudata.so.53 => /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/./../../lib/libicudata.so.53 (0xb547f000)
    	libpcre.so.3 => /lib/i386-linux-gnu/libpcre.so.3 (0xb5441000)
    

    I entered the forum to tell you , this problem have just i found. xaxa and I see that what you ask (if i do not do wrong)


  • Moderators

    That's probably the problem:
    libmysqlclient_r.so.16 => not found

    It looks like the MySQL lib is not installed.



  • what can I do to fix it?


  • Moderators

    Can you check whether you have libmysqlclient_r.so.16 on your system?
    On my Ubuntu 14.04 it is libmysqlclient_r.so.18



  • @jsulm
    Yes I have the same too :

    :/usr/lib/i386-linux-gnu$ ls | grep libmysql
    libmysqlclient.a
    libmysqlclient_r.a
    libmysqlclient_r.so
    libmysqlclient_r.so.18
    libmysqlclient_r.so.18.0.0
    libmysqlclient.so
    libmysqlclient.so.18
    libmysqlclient.so.18.0.0
    
    

  • Lifetime Qt Champion

    Then the easiest way is to rebuild the plugin against your installed MySQL client libraries. It's described here



  • @SGaist
    Could someone please guide me?
    This guide does not help that much.. :/

    There is no other way?



  • Hi, you could try to trick Qt you've got the correct file, by using the newer version as the older one (just a guess :-):
    cp libmysqlclient_r.so.18.0.0 libmysqlclient_r.so.16



  • @hskoglund
    I tried to shortcut and copy as you say now.
    but the same effect :

    code :

        QPluginLoader loader;
        loader.setFileName("/home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so");
        qDebug() << "PluginLoader worked =" << loader.load();
        qDebug() << "Plugin Loader error=" << loader.errorString();
    

    error :

    PluginLoader worked = false
    Plugin Loader error= "Cannot load library /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so: (/usr/lib/i386-linux-gnu/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so))"
    


  • Ok, maybe you need to go further tricking Qt also that the libmysqlclient.so.16 is fine and well, by also copying that file:
    cp libmysqlclient.so.18.0.0 libmysqlclient.so.16



  • @hskoglund
    oops you're right! I had not noticed the name. But now? I have this error :

    PluginLoader worked = false
    Plugin Loader error= "Cannot load library /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so: (/usr/lib/i386-linux-gnu/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by /home/myuser/Qt/5.4/gcc/plugins/sqldrivers/libqsqlmysql.so))"
    

    and these files :

    :/usr/lib/i386-linux-gnu$ ls | grep libmysql
    libmysqlclient.a
    libmysqlclient_r.a
    libmysqlclient_r.so
    libmysqlclient_r.so.16
    libmysqlclient_r.so.18
    libmysqlclient_r.so.18.0.0
    libmysqlclient.so
    libmysqlclient.so.16
    libmysqlclient.so.18
    libmysqlclient.so.18.0.0
    
    


  • Ahhh, sorry it seems the ruse didn't work all the way :-(
    It's probably a version resource or something simular in the libmysqlclient.so.18.0.0 file that says "version 18" and not "version 16".

    Anyway I think your next step is instead to try to download and install the kosher *.16, have you tried?
    sudo apt-get install libmysqlclient16


  • Lifetime Qt Champion

    You can't cheat like that on Linux ;)

    @Tas-sos what trouble did you have to rebuild the plugin ? It's really just grab Qt's source and follow the lines of the documentation.



  • @hskoglund

    Never mind my friend anyway thank you very much!
    Yes I have tried it and got this:

    E: Unable to locate the package libmysqlclient16
    

    or this :

    E: Unable to locate the package libmysqlclient16-dev
    

    @SGaist
    xaxaax my friend like your answer.
    "You can't cheat like that on Linux ;)"

    As for the guide (documentation) , basically confuses me a little and I do not know if I can follow him because for example he says to run this command :

    cd $QTDIR/qtbase/src/plugins/sqldrivers/mysql
    

    But me the file structure is :

    :~$ ls Qt 
    5.4
    components.xml
    Docs
    Examples
    Extras
    InstallationLog.txt
    Licenses
    MaintenanceTool
    MaintenanceTool.dat
    MaintenanceTool.ini
    network.xml
    Tools
    
    :~/Qt/5.4$ ls
    android_armv7  gcc
    
    :~/Qt/5.4/gcc$ ls
    bin
    icudtl.dat
    imports
    include
    lib
    libexec
    mkspecs
    phrasebooks
    plugins
    qml
    qtwebengine_resources.pak
    translations
    
    :~/Qt/5.4/gcc/plugins$ ls -l
    
    audio
    bearer
    designer
    generic
    geoservices
    iconengines
    imageformats
    mediaservice
    platforminputcontexts
    platforms
    platformthemes
    playlistformats
    position
    printsupport
    qml1tooling
    qmltooling
    qtwebengine
    sensorgestures
    sensors
    sqldrivers  <-----------
    
    :~/Qt/5.4/gcc/plugins$ tree sqldrivers/
    sqldrivers/
    ├── libqsqlite.so
    ├── libqsqlmysql.so
    └── libqsqlpsql.so
    

    the path that says, to me (as you see) does not exist. :/
    Extra searches:

    find  ~/Qt -name "sqldrivers"
    /home/myuser/Qt/Tools/QtCreator/bin/plugins/sqldrivers
    /home/myuser/Qt/5.4/gcc/plugins/sqldrivers
    
    $find  ~/Qt -name "qtbase"
    -nothing-
    
    $ find  ~/Qt -name "mysql"
    -nothing-
    

    In my system the folders with name "mysql" are only those :

    sudo find  / -name "mysql"
    /var/lib/mysql
    /var/lib/mysql/mysql
    /var/log/mysql
    /etc/apparmor.d/abstractions/mysql
    /etc/init.d/mysql
    /etc/mysql
    /usr/bin/mysql
    /usr/lib/perl5/auto/DBD/mysql
    /usr/lib/perl5/DBD/mysql
    /usr/lib/mysql
    /usr/share/maven-repo/mysql
    /usr/share/mysql
    /usr/share/bash-completion/completions/mysql
    /usr/include/mysql
    /usr/include/mysql/mysql
    

    and folder named "qtbase" does not exist.
    For this reason the documentation does not help.


  • Lifetime Qt Champion

    That's because you don't have the Qt sources installed. Use maintenancetool to download them.



  • @SGaist
    Unbelievable though it was in front of me I had no idea about the "maintenancetool". I am inexperienced with using Qt not know so much about this, but I hope now to learn.

    I ran as you said the "MaintenanceTool" located in the "Qt" folder and i installed the "sources".
    Then I have what I needed! :D

    The guide now is satisfactory and effective. ;)

    Simply the guide says this path:

    cd $QTDIR/qtbase/src/plugins/sqldrivers/mysql
    

    to me is this:

    /home/myuser/Qt/5.4/Src/qtbase/src/plugins/sqldrivers/mysql
    

    Then because I have and version "Qt4" this command:

    qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro
    

    i run in this way:

    /home/myuser/Qt/5.4/gcc/bin/qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro
    

    All now work perfectly! I'm very happy about that! I Struggled a lot with this issue and does not need nor expecting.
    Thank you very very much my friends! I am indebted to your with your efficient and fast help!

    THANK YOU!



  • This post is deleted!


  • @SGaist said in QSqlDatabase: QMYSQL driver not loaded:

    That's because you don't have the Qt sources installed. Use maintenancetool to download them.

    Can someone tell please me what are QT sources..


  • Lifetime Qt Champion

    Hi,

    As silly as it may sound: tt's the sources of the Qt library.



  • This post is deleted!


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.