Typical symptom when trying to connect to MySQL / MariaDB using Qt but without the necessary plugin is the runtime error
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QMYSQL
Since the Qt Company cannot provide qsqlmysql.dll / libqsqlmysql.so in binary form, you have to build it on your own, which can be a pain. Here is a build for various Qt versions. Get precompiled qsqlmysql.dll for Windows / libqsqlmysql.so for Linux from releases. Be sure to match your Qt version and compiler (Microsoft Visual C++/MSVC, MinGW) EXACTLY. For example if you use Qt 6.4.2, you cannot use driver version 6.4.1 or 6.4.3
This is helpful to you? Feel free to show a little appreciation by donating
Latest Qt6 version: Download for Qt 6.5.2 hereLatest open source Qt5 version: Download for Qt 5.15.7 here
For Android see 3rd party repository https://github.com/sayyyed/qt_android_mysql_driver/releases/tag/qt_mysql_driver_for_android
- copy qsqlmysql.dll (release build) / MSVC: qsqlmysqld.dll, MinGW: qsqlmysql.dll+qsqlmysql.debug (debug build) to subdirectory "sqldrivers" of application directory (or build directory while developing)
- copy libmysql.dll (MySQL library) and the libcrypto/libssl OpenSSL libraries from zip file (or from https://dev.mysql.com and https://kb.firedaemon.com/support/solutions/articles/4000121705 respectively) to application directory (or more generally, any directory that is registered in PATH environment variable)
for MSVC:
Prerequisites:
- Install MSVC Compiler (C++ Desktop Toolkit from Visual Studio 2022 Setup)
- Install Qt incl. Sources, CMake and Ninja from Maintenance Tool
- Install MySQL library, e.g. by using Oracle's Installer or use the zip file from here
set PATH=%PATH%;C:\Qt\Tools\CMake_64\bin;C:\Qt\Tools\Ninja
cd C:\Qt\6.4.0\Src\qtbase\src\plugins\sqldrivers
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
call C:\Qt\6.4.0\msvc2019_64\bin\qt-cmake.bat -G "Ninja" . -DMySQL_INCLUDE_DIR="C:\mysql_8.0.29\include" -DMySQL_LIBRARY="C:\mysql_8.0.29\lib\libmysql.lib" -DCMAKE_INSTALL_PREFIX="C:\Qt\6.4.0\msvc2019_64"
ninja
ninja install
call C:\Qt\6.4.0\msvc2019_64\bin\qt-cmake.bat -G "Ninja" . -DMySQL_INCLUDE_DIR="C:\mysql_8.0.29\include" -DMySQL_LIBRARY="C:\mysql_8.0.29\lib\libmysql.lib" -DCMAKE_INSTALL_PREFIX="C:\Qt\6.4.0\msvc2019_64" -DCMAKE_BUILD_TYPE=Release
ninja
ninja install
pause
for MinGW:
Prerequisites:
- Install Qt incl. Sources, CMake and MinGW from Maintenance Tool
- Install MySQL library, e.g. by using Oracle's Installer or use the zip file from here
set PATH=%PATH%;C:\Qt\Tools\mingw1120_64\bin;C:\Qt\Tools\CMake_64\bin
cd C:\Qt\6.4.0\Src\qtbase\src\plugins\sqldrivers
call C:\Qt\6.4.0\mingw_64\bin\qt-cmake.bat -G "MinGW Makefiles" . -DMySQL_INCLUDE_DIR="C:\mysql_8.0.29\include" -DMySQL_LIBRARY="C:\mysql_8.0.29\lib\libmysql.lib" -DCMAKE_INSTALL_PREFIX="C:\Qt\6.4.0\mingw_64" -DCMAKE_C_COMPILER="gcc.exe" -DCMAKE_CXX_COMPILER="g++.exe"
mingw32-make
mingw32-make install
pause