Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Build and link MySQL plugin (Qt6 - Qt 6.0.0 + 5.15.2, MySQL 8.0.22, Visual Studio 2019 with Qt Visual Studio Tools 2.6.0.7) - step by step 64bit



  • Hey!
    Just a small note; this is my first post here and I'm very new to Qt.
    Just a small note2; I was trying to build whole Qt so a part of the steps might not be necessary, but if you get errors try doing every step (aka. random sh*t goes brrrrrrrrr).

    So, by following this guide, I managed to get Qt 5.15.2 working perfectly (for me) but barely and I seem to not be able to do the same with Qt 6.0.0. For Qt5 and Qt6, my steps are the same:
    I have installed MySQL (community, NOT web-community), Python2 (Windows x86-64 MSI installer), LLVM (I went with "Windows snapshot builds" - 64bit) and Perl64 (Active Perl 5.28).
    Python, Perl and LLVM are installed in "C:\"
    MySQL was installed in "C:\Program Files", so I copied "include" and "lib" folder from "C:\Program Files\MySQL\MySQL Server 8.0" to "C:\MySql".

    Using MaintenanceTool.exe I installed Qt 5.15.2 and 6.0.0 to "C:\Qt".

    System PATH variable contains following (important-ish parts, change Qt version as needed):

    C:\Python27
    C:\Python27\Scripts
    C:\Perl64\bin
    C:\Qt\6.0.0\msvc2019_64\bin
    C:\Qt\Tools\mingw810_64\bin
    C:\Qt\Tools\CMake_64\bin
    C:\LLVM\bin
    C:\MySql\lib
    C:\MySql\include
    C:\Qt\6.0.0\Src\qtbase\bin
    

    And a new variable LLVM_INSTALL_DIR:

    C:\LLVM
    

    Tip: you can find Environment Variables by going Settings>System>About>Advanced system settings and there you'll see a Environment Variables... button, or do a google search.

    To build the plugin I have these 3 scripts:

    • inject_vcvarsall.bat
    @echo off
    cd "c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build"
    vcvars64.bat
    echo on
    
    • vcvarsall-qt_src.bat
    start cmd /k "inject_vcvarsall.bat & cd /d "C:\Qt\6.0.0\Src" & start ."
    
    • vcvarsall-driver_src.bat
    start cmd /k "inject_vcvarsall.bat & cd /d "C:\Qt\6.0.0\Src\qtbase\src\plugins\sqldrivers" & start ."
    

    The inject_vcvarsall.bat will run vcvars64.bat script that will setup everything needed for building. vcvarsall-qt_src.bat and vcvarsall-driver_src.bat just open a cmd and targeted folder.
    The only thing that changes in those scripts are Qt versions (6.0.0 to 5.15.2).

    In "C:\Qt\15.5.2\Src" cmd (vcvarsall-qt_src.bat) (the picture shows 6.0.0 but you can just change it as I mentioned) you run following command:

    configure -confirm-license -opensource -sql-mysql MYSQL_INCDIR="C:/MySql/include" MYSQL_LIBDIR="C:/MySql/lib"
    

    Give it a bit of time and it should start checking for different things on your computer. To see if it has detected MySQL, after it's done checking, search for "Qt Sql Drivers:" under "Build options" section:

    If MySQL says yes (like in the picture above), congrats, it detected your MySQL.

    If it gives you error: ERROR: Feature 'sql-mysql' was enabled, but the pre-condition 'libs.mysql' failed., it probably couldn't detect MySQL. Make sure that MYSQL_INCDIR ("C:/MySql/include") folder contains mysql.h and that MYSQL_LIBDIR ("C:/MySql/lib") folder contains libmysql.lib and libmysql.dll (reference).
    Those files are in "C:\Program Files\MySQL\MySQL Server 8.0" and not in "C:\Program Files\MySQL\Connector C++ 8.0" which is weird because I would think that I need to connect to a database and not like run a server with my app or something.
    But nevertheless it works this way.

    Now you can close that window and run vcvarsall-driver_src.bat.
    In opened cmd, run following commands (reference):

    qmake -- MYSQL_INCDIR="C:/MySql/include" MYSQL_LIBDIR="C:/MySql/lib" MYSQL_PREFIX="C:/MySql"
    nmake sub-mysql
    nmake install
    

    After you ran the last command it should be done.

    Now. I went through all the steps from above for 5.15.2 and 6.0.0 version, but when I build my application for Qt 5.15.2, it runs ok, but when I target Qt 6.0.0, it gives me this error:

    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QPSQL
    QSqlDatabase::lastError(): Driver not loaded Driver not loaded
    

    The code of my simple application:

    #include <QtCore/QCoreApplication>
    #include <QtSql>
    #include <QtCore>
    
    #include <iostream>
    
    int main(int argc, char* argv[]) {
        QCoreApplication a(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("127.0.0.1");
        db.setUserName("root");
        db.setPassword("Root_Pass1023");
    
        db.setDatabaseName("users");
    
        if (!db.open())
            std::cout << "QSqlDatabase::lastError(): " << db.lastError().text().toStdString() << std::endl;
        else
            std::cout << "YAY" << std::endl;
    
        db.close();
        return a.exec();
    }
    

    NOTE:

    configure -confirm-license -opensource -sql-mysql MYSQL_INCDIR="C:/MySql/include" MYSQL_LIBDIR="C:/MySql/lib"
    

    Command for 6.0.0 is now:

    configure -confirm-license -opensource -sql-mysql