Important: Please read the Qt Code of Conduct -

I can't connect to my local database.

  • [ SOLVED ]

    I want to connect to my local database from QT but when I run my project it gives me this error from db.lasterror().texet() :
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

    My OS is Centos 7 64 Bit.
    Please help.

  • @leonidas_lolo said:

    QSqlDatabase: QMYSQL driver not loaded

    Take a look on this thread:
    QSqlDatabase: QMYSQL driver not loaded but available

  • I did what the thread said but it didn't worked. my code is :
    main.cpp file

    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QMYSQL");
        return a.exec();

    my .pro file have the followings :

    QT       += core
    QT       += sql
    QT       -= gui
    TARGET = DB_Programming
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp

    Please advise.

    Edited: Put code after ``` (3 backticks) and close with the same - p3c0

  • @leonidas_lolo Hi, normally everything should be working out of the box after installing Qt and the MySQL database.
    Just to double-check:

    • you added a line like qDebug() << "Plugins loaded from: " << QCoreApplication::libraryPaths(); to make sure the plugins path with the sqldrivers subdirectory is in there (it should, otherwise you won't get the list containing QMYSQL in "available drivers")
    • you ran ldd /usr/lib/qt/plugins/sqldrivers/ to check if there is an entry like => /usr/lib/ (0xb738d000)

    One other thing that just came to my mind: You don't have multiple versions of MySQL installed or the like (e.g. MySQL and MariaDB)?

  • I do not have experiance with MySQL but with PostrgeSQL the problem (every time) was that the PostgreSQL libs were not available to the application. We had to either copy the dlls into the same folder as the application or make it avaiable in the PATH (windows)

    So on Linux make sure that the libs are located in the LD_LIBRARY_PATH

    • I added the line to my .cpp file
      qDebug() << "Plugins loaded from: " << QCoreApplication::libraryPaths(); and it says :
      ("/opt/Qt/5.4/gcc_64/plugins", "/home/<username>/Documents/Qt Projects/<another folder>/build-DB_Programming-Desktop_Qt_5_4_0_GCC_64bit-Release")
      I searched in that directory and I do have the folder sqldrivers.

    • I don't have any file on the /usr/lib/qt4/plugins/sqldrivers.
      The only file i have is

    The file is locate at my installation directory /opt/Qt/5.4/gcc_64/plugins/sqldrivers.
    I make a copy and paste it to the previous direcotry but did not wokr either.

    When I ran the ldd on on the /opt/Qt/5.4/gcc_64/plugins/sqldrivers location it says: => (0x00007fffdfd11000) => not found => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/ (0x00007f4f00daf000) => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/ (0x00007f4f00673000) => /lib64/ (0x00007f4f0036c000) => /lib64/ (0x00007f4efffaa000) => /lib64/ (0x00007f4effd8e000) => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/ (0x00007f4eff942000) => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/ (0x00007f4eff5b6000) => /lib64/ (0x00007f4eff3b2000) => /lib64/ (0x00007f4eff1b0000) => /lib64/ (0x00007f4efefa7000) => /lib64/ (0x00007f4efec7e000) => /lib64/ (0x00007f4efe97c000) => /lib64/ (0x00007f4efe765000)
    /lib64/ (0x00007f4f0121d000) => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/ (0x00007f4efd0dd000)

  • Moderators

    @leonidas_lolo => not found

    install mysqlclient libraries.

  • I run the command locate mysqlclient and I have the libraries.

    [username@localhost ~]$ locate mysqlclient

  • Moderators

    So I think the only way would be to try to link to /usr/lib64/ Or install old version of mysql client libraries which will contain *.so.16

  • I fixed the problem by re-installing the QT Creator.

Log in to reply