Qt – How to get|compile Mysql driver

I am trying to make a MySql-dependent app in Qt. After some time I came to know that my shared-build is not having MySql driver(by default). Can Anybody say how to get|compile it (both in shared and static).

Note: I am using Qt-4.7.2 in Windows platform

Thanks to “vrince”. This is the way he showed => I did.

 1)Open qt-command prompt 

 2)Goto (Qt's installation path)qtsrcpluginssqldriversmysql
   in my case:


   mingw32-make (provided your PATH variable contains "(Qt installation path)mingwbin")

   ("make" should work if you didn't mess up with path variables. It in turn
    invokes mingw32-make. I messed up this a little bit. So I invoked 
    mingw32-make directly.)

 5)In the above  command you have to indicate the mysql's "lib" path, 
   and "include" path through the compile flag options. Or Add those lines
   in the pro file like below

   INCLUDEPATH += "C:Program FilesMySQLMySQL Server 5.1include"
   LIBS += -L"C:Program FilesMySQLMySQL Server 5.1libopt"

That’s it. You can find the dlls in
(Qt-installation path)qtpluginssqldrivers


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you plan to rebuild Qt linked to MySQL you can stop now you don’t have to ! SQL drivers are plugins (by definition dynamically loaded at run time) and can be compiled independently.

Find the driver sources in the Qt source tree somthing like qt/src/plugins/sqldrivers/mysql then build it. The game here is to provide the proper MySQL development headers and libraries (client ones) so that the driver will build ! (Be aware if you are one windows it may be 32bits version of MySQL client you need even if you are running a 64bits OS).

You can provide MySQL path via the qmake command for that refer to the article given by Anton, personally I copy and change the .pro file to match my installation … easier to rebuild later if needed.

Once the build succeeded, you will have a nice qsqlmysql.dll you must copy into the Qt dir you use to run you apps basically something like qt/plugins/sqldrivers in the $QT_DIR.

Method 2

Building for QT5.13 using MinGW32
1. Download MySql C Connector v6.1.

>   Download the MySql Installer from:
>   <https://dev.mysql.com/downloads/installer/>
>   Install C Connector 6.1 (Note the location we will need it later)

2. Getting QT Source Ready

>    To build a plugin for QT u need to get its source. You can install it from Maintenance Tool or manually get it from github repository.
  1. Building Plugin
    1. Open MinGW CMD (Windows -> Start Menu -> Programs -> Qt 5.13.1 ->
      5.13.1-> MinGW 7.3.0 (32-bit) -> Qt 5.13.1 (MinGW 7.3.0 32-bit) )
    2. cd to sqldrivers path in qt source
    cd D:\QT\Qt5.13.1\5.13.1\Src\qtbase\src\plugins\sqldrivers
    1. Run qmake here. qmake sqldrivers.pro (to create qtsqldrivers-config.pri )
    2. cd to mysql.
    cd D:\QT\Qt5.13.1\5.13.1\Src\qtbase\src\plugins\sqldrivers\mysql
    1. Run qmake here. qmake mysql.pro
  2. Add the plugin to the list

    Once successfully build you will find qsqlmysql.dll and qsqlmysqld.dll in
    the following location

    Copy both qsqlmysql.dll and qsqlmysqld.dll and place them in the compiler’s
    plugin directory

Error in Building?

  1. Library ‘mysql’ is not defined.

    In file cd

    Commend the line QMAKE_USE += mysql

  2. Adding Library path and Include path.

    Add the following to mysql.pro at end

    LIBS += -L'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/lib/'
    INCLUDEPATH += 'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/include'
    DEPENDPATH += 'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/include'
  3. QSqlDatabase: QMYSQL driver not loaded

    Add the .dll files from C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/lib to D:QTQt5.

Method 3

At first mingw32 version must be installed list bellow

  • MariaDB connector 32 bit (this is important)
  • Mingw32 installed


  • open mingw32-make
  • go to “C:QtQt5.” (in
    my Drive)
  • in mingw32 terminal >qmake sqldrivers.pro
  • after that cd mysql
  • in mingw32 terminal > qmake "INCLUDEPATH+=D:\MariaDB\ConnectorC\include" "LIBS+=D:\MariaDB\ConnectorC\lib\libmariadb.lib" mysql.pro

If you get an error like “mysql” etc. change the mysql.pro file as below

TARGET = qsqlmysql

HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp
#QMAKE_USE += mysql
INCLUDEPATH +='D://MariaDB//include//mysql'(Check files if there isn't)
DEPENDPATH += 'D://MariaDB//include//mysql'
LIBS += -L'D://MariaDB//lib//libmysql.lib' 

OTHER_FILES += mysql.json

  • then make again in mingw32 terminal > qmake "INCLUDEPATH+=D:\MariaDB\ConnectorC\include" "LIBS+=D:\MariaDB\ConnectorC\lib\libmariadb.lib" mysql.pro

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x