Create MySQL driver for Qt5 on Windows

This tutorial explains how to create the MySQL driver for Qt5 on Windows using MinGW. If you are looking for the tutorial to do this with Qt4, click here.

Step 1

Download all the needed software.

  • Qt (libraries and source code)
    This are two separate files! E.g. qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe and qt-everywhere-opensource-src-5.0.1.zip
  • MySQL Community Server (E.g. mysql-5.5.30-win32.msi)
    I didn't use version 5.6 because the .msi only allowed me to install the 64-bit version of the Client C API Library, which apparently gives errors later in the tutorial. So make sure you get the 32-bit verison! If you really want version 5.6, an option might be to download the 32-bit version of the source files and use those. Note that I didn't test this option, so I can't help you with that.

Step 2

Install the downloaded software.

  • Install Qt, the components I installed are the default ones. (See image 1)
  • Install MySQL, you only need to install the Client C API Library the other components are optional for this tutorial. (See image 2)

    I installed it under C:\Program Files (x86)\MySQL\MySQL Server 5.5. Since the path contains spaces, you should use C:\PROGRA~2\MySQL\MYSQLS~1.5, else it won't work. 1

  • Extract the Qt Sources to a folder of your choosing. I placed it in the folder Qt was installed into. (C:\Qt\Qt5.0.1)
    I also renamed the extracted folder to Sources instead of the long qt-everywhere-opensource-src-5.0.1 name.
Image 1: Install QtImage 1: Install Qt
Image 2: Install MySQLImage 2: Install MySQL

Step 3

Open the Qt Command Prompt. (See image 3)
Start > All Programs > Qt 5.0.1 > 5.0.1 > MinGW 4.7 > Qt 5.0.1 for Desktop (MinGW 4.7)

Image 3: Qt Command PromptImage 3: Qt
Command Prompt

Step 4

Run the following commands (change them according to your environment):

  1. set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.5
  2. cd C:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\
  3. qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro
  4. mingw32-make

Step 5

Copy qsqlmysql.dll and qsqlmysqld.dll from C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldrivers to C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers.

Step 6

Copy libmysql.dll from %mysql%\lib to C:\Windows

Finished

That's all, but don't forget to add QT += sql to your project file, else it won't work. To check which drivers are available run this program. You can also download the code as a Qt project in the attachments.

  1. #include <QtCore/QCoreApplication>
  2. #include <QtSQL>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QCoreApplication a(argc, argv);
  7.     qDebug() << QSqlDatabase::drivers();
  8.     return a.exec();
  9. }

1 Note: For 32-bit systems the install path of MySQL will be C:\Program Files\MySQL\MySQL Server 5.5 and the path without spaces will be C:\PROGRA~1\MySQL\MYSQLS~1.5.To determine a custom path without spaces, use the dir /x command on each of the folders.

Attachments: 

Comments

I followed this guide and was able to build the drivers and when i check for them i can see they are available, however when i try to add a database with the QMYSQL driver, i still get the driver not loaded error.

Are you sure you copied the libmysql.dll file to the Windows folder?

I copied it in the directory that contains my exe file. But I still get a "Driver not loaded" error.

They should be located in the sqldrivers folder.

I mean I have libmysql.dll in my exe directory (and C:/Windows as in step 6) and I have qsqlmysql.dll and qsqlmysqld.dll in the plugins\sqldrivers directory. According to visual, those dlls are found but an issue occurs with libmysql.dll while loading libmysql.dll. It crashes with a first chance exeption it cannot find IESHIMS.dll and WER.dll

I have the dlls in the place described in step 5 and 6. According to visual 2010 they are found. But I have a first chance exception while loading libmysql.dll because IESHIMS.dll and WER.dll can't be found.

Are you using the correct compiler? Since this is a tutorial for minGW and not the visual studio compiler. :)

I am receiving the following when I run mingw32-make:

In file included from ./mysql.h:72:0,
from ../../../sql/drivers/mysql/qsql_mysql.h:52,
from main.cpp:44:
./mysql_com.h: At global scope:
./mysql_com.h:455:31: error: 'size_t' has not been declared
./mysql_com.h:457:60: error: 'size_t' has not been declared
./mysql_com.h:459:35: error: 'size_t' has not been declared
./mysql_com.h:460:35: error: 'size_t' has not been declared
./mysql_com.h:461:58: error: 'size_t' has not been declared
In file included from ./typelib.h:20:0,
from ./mysql.h:131,
from ../../../sql/drivers/mysql/qsql_mysql.h:52,
from main.cpp:44:
./my_alloc.h:44:3: error: 'size_t' does not name a type
./my_alloc.h:45:3: error: 'size_t' does not name a type
Makefile.Release:733: recipe for target '.obj/release_shared/main.o' failed
mingw32-make[1]: *** [.obj/release_shared/main.o] Error 1
mingw32-make[1]: Leaving directory 'C:/Qt/Qt5.0.1/5.0.1/Src/qtbase/src/plugins/s
qldrivers/mysql'
makefile:38: recipe for target 'release-all' failed
mingw32-make: *** [release-all] Error 2

I am using the exact instructions you included, down to the MySQL install config. I was wondering if this was something to do with my MySQL environment variables, but that seems to check out. Just curious if you ran into anything like this?

You're probably right that you have a problem with the MySQL environment. Did you use the correct path for the %mysql variable? Remember that C:\\PROGRA~2\\MySQL\\MYSQLS~1.5 depends on your system and on the installation folder. More info about that can be found in the article.

I am sure I have a minor issue somewhere. Would you care to provide the QT related environment variables that you have set (MySQL, Qt, & MinGW). I would really like to compare what I am using to what you have set, thank you!

You can delete my previous comment, thank you very much for this very helpful tutorial! I will make sure to distribute this link to others that I know are having the same trouble. I was not including my mingw "lib" in my environment variables. After fixing that - we are good to go.

Hi, i follow your guide, but .dll files was not created. What should i do ?

Did you get any kind of error message? Make sure to adjust the paths in the tutorial to the actual install paths on your system.

spent too much time trying to compile this driver, I finally figure out that I needed to update mysql server to 5.5.30 (i had an older version of mysql server 5.5), I was getting many "undefined..." error.

Thanks a lot

Sir we really appreciate your work and really you have done a great work by giving a Comprehensive tutorial.
Thanks alot....

Hi, I follow your guide step by step but allways get the same error:

mingw32-make -f Makefile.Release all
mingw32-make[1]: Entering directory 'C:/Qt/Qt5.0.1/Sources/qtbase/src/plugins/sq
ldrivers/mysql'
g++ -Wl,-s -shared -Wl,--out-implib,C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldriv
ers\libqsqlmysql.a -o ..\..\..\..\plugins\sqldrivers\qsqlmysql.dll .obj/release_
shared/main.o .obj/release_shared/qsql_mysql.o .obj/release_shared/moc_qsql_mysq
l.o :C\PROGRA~2\MySQL\MYSQLS~1.5\lib\libmysql.lib -LC:\Qt\Qt5.0.1\5.0.1\mingw47
_32\lib -lQt5Sql -lQt5Core
g++: error: :C\PROGRA~2\MySQL\MYSQLS~1.5\lib\libmysql.lib: Invalid argument
Makefile.Release:80: recipe for target '..\..\..\..\plugins\sqldrivers\qsqlmysql
.dll' failed
mingw32-make[1]: *** [..\..\..\..\plugins\sqldrivers\qsqlmysql.dll] Error 1
mingw32-make[1]: Leaving directory 'C:/Qt/Qt5.0.1/Sources/qtbase/src/plugins/sql
drivers/mysql'
makefile:38: recipe for target 'release-all' failed
mingw32-make: *** [release-all] Error 2

What should I do ?

works fine ty.

ty!

Thanks for sharing the info.
There seems to be quite a mess in the msi MySQL installer, but then e got the version that worked.

The instructions worked very well for MySQL Server 5.6 too.
I am indeed very greatful for this tutorial.
I would never had managed to find out this procedure myself.

Hi, thanks for the excellent step-by-step. Worked like a charm. Here are some additional tips:

- On Step 1, at least for Qt 5.0.2, there's no need to download the sources separately. They are included in qt-windows-opensource-5.0.2-mingw47_32-x86-offline.exe. To install them, in the Qt 5.0.2 Setup Wizard, "Select Components" step, select the "Essentials" in Qt -> Qt 5.0.2 -> Source Components -> Essentials. (it's on the "Image 1" in this tutorial). The sources will be at C:\Qt\Qt5.0.2\5.0.2\Src.

- On Step 1, if you download the mysql zip (for example mysql-5.6.11-win32.zip) from the mysql site, then you can unzip it to any place you want, and have the 32bit libs in your 64bit OS (just compiled on Win8 64bits).

- On Step 2, to find out what is the "short" 8.3 name in a command line, use "dir /x"

- If the sources were installed by the Qt installer, replace the "C:\Qt\Qt5.0.1\Sources" in this tutorial for "C:\Qt\Qt5.0.2\5.0.2\Src" on all steps.

Thanks again!
Joao S Veiga

Thanks! The tips are appreciated! ;)

Cool thanks! Saved the day. :)

http://qt-project.org/forums/viewthread/27579

Under Win7 64 bit, cannot add mysql driver.
I use 32Bit qt and 32 bit mysql, build mysql driver ok, QSqlDatabase::drivers() can see “QMYSQL”, but QSqlDatabase::addDatabase(“QMYSQL”) failed.
I copied the libmysql.dll to C:\Windows\System32, still fail.

Anybody has SOLVED it already?

Best regards,

To use full path, you can do so

cd C:\Qt\Qt5.0.2\qt-everywhere-opensource-src-5.0.2\qtbase\src\plugins\sqldrivers\mysql\
qmake "INCLUDEPATH+=c:/Program\ Files/MySQL/MySQL\ Server\ 5.5/include" "LIBS+=c:/Program\ Files/MySQL/MySQL\ Server\ 5.5/lib/libmysql.lib" -o Makefile mysql.pro

Sorry, my last comment is wrong! Now I write a script. It change the path automaticaly:
@echo off
call C:\Qt\Qt5.0.2\5.0.2\mingw47_32\bin\qtenv2.bat

cd C:\Qt\Qt5.0.2\qt-everywhere-opensource-src-5.0.2\qtbase\src\plugins\sqldrivers\mysql\

for %%g in ("c:\Program Files\MySQL\MySQL Server 5.6\include") do set SQLINC=%%~fsg
for %%g in ("c:\Program Files\MySQL\MySQL Server 5.6\lib\libmysql.lib") do set SQLLIB=%%~fsg

qmake "INCLUDEPATH+=%SQLINC%" "LIBS+=%SQLLIB%" -o Makefile mysql.pro
mingw32-make
copy C:\Qt\Qt5.0.2\qt-everywhere-opensource-src-5.0.2\qtbase\plugins\sqldrivers\qsqlmysqld.dll C:\Qt\Qt5.0.2\5.0.2\mingw47_32\plugins\sqldrivers\
copy c:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.dll C:\Windows
pause

Yeah! This guide rocks! Now I got the right dll, after trying countless guides (which were probably made for Qt4). Yeah, and don't forget libmysql.dll, like I did! I liked that all the paths were default, exactly the same as where I had installed stuff in the first place.

For reference, my sizes
qsqlmysql.dll 80384
qsqlmysqld.dll 915614

Thank you!

Thanks very much for this effort and detail. I got it working and I'm obliged. Best advice to anyone else is to follow *exactly* what you have. Worked for me after a few tries. Cheers.

Thank you very much :)

Thanks a lot

finally a guide that works!! It gets old looking through dead end links and tuts that don't work. You are the man!!!!!!!!!

Hi, maybe I'm just look in the totally wrong place, but I cannot find the second Qt file you use. I can find qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe in the archived qt files (http://download.qt-project.org/archive/qt/5.0/5.0.1/) but I cannot seem to find qt-everywhere-opensource-src-5.0.1.zip. Is this located somewhere else? Thanks, this guide seems very helpful!

Hi. I'm having a lot of trouble finding the the second file you reference. I have found qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe in http://download.qt-project.org/archive/qt/5.0/5.0.1/, but I can't seem to find qt-everywhere-opensource-src-5.0.1.zip. Do you know of a place I can download that file?

Thanks for this tutorial, it seems to be great!

Just took a look at the URL you posted, it seems to be in the 'single' folder. Then you get the following URL: http://download.qt-project.org/archive/qt/5.0/5.0.1/single/qt-everywhere...

Thank you for this great HOWTO!!

Hello;
Instead of copy use :
mingw32-make install

All works fine! Thanks!

thank you very much.
I search so much page article about the issue, only your's solve my problem.

thank you very much.
I search so much page article about the issue, only your's solve my problem.

Thank you very much, I was blocked with all of the other tutorial and you have help me!!!!!

Hi,
I did all things step by step and all functions very well. Also the sample programm above got:
"("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")"

But if I add somthing like this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("TestDB");
db.setUserName("root");
db.setPassword("****");
if (!db.open())
qDebug() << "Database Error";

I get the following:
"("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
Database Error"

Please help. I must probably add that this is all done with Window 8.1
Thank you in advance.
regards,
Alex

Have you added sql to your project file?
QT += sql

You will probably also have to add to your code:

  1. db.setHostName(&quot;localhost&quot;);
  2. db.setPort(&quot;3306&quot;);

This happened to me when I installed a new version of Qt 5.1.1 (I had 5.0.2 previously). I had to recompile qsqlmysql, and it started working after that.

Once again, this tutorial saves the day, this time for 32-bit {Qt5.1.1 + MinGW 4.8 + MySQL Server 1.6} running on 64-bit Windows 7 SP1. GJ!

Hi, i have several question.
- At Step 4, after command : mingw32-make, is needed also mingw32make install ?
- At Step 5 can i move dll file instead of copy it ?
- At Step 5, in C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldrivers are two file .a.
Can i delete their?
- At Step 6, istead of copy libmysql.dll in C:\Windows, can i copy it in dir
- where is .exe build file ?
-If i want to launch my program in a windows pc that have only mysql server installed,
my exe program file and libmysql.dll are the unic file requested?
-I use mingw 32 bit version, and i build 32 bit qmysql driver that allow to me
to connect to mysql server 32 bit.
can i use it to connect to mysql server 64 bit ?
thank you!!!

Hi,

1) At Step 4, after command : mingw32-make, is needed also mingw32make install ?
2) At Step 5 can i move dll file instead of copy it ?
3) At Step 6, istead of copy libmysql.dll in C:\Windows, can i copy it in dir where is .exe build file ?
4) If i want to launch my program in a windows pc that have only mysql server installed, my exe program file and libmysql.dll are the unic file requested?
5) I use mingw 32 bit version, and i build 32 bit qmysql driver that allow to me to connect to mysql server 32 bit.

  1. You can do this, but it is not required.
  2. You can move them, I just say copy so you still have a backup if case you copy it to the wrong location and can't find it anymore.
  3. I don't know for sure, I tried that but it didn't work on all the systems I tried... It might have changed at this time, so feel free to try it.
  4. The MySQL server doesn't have to be on the same system, it's no problem if it is though. You will also need the qsqlmysql.dll or qsqlmysqld.dll. They need to be located in the sqldrivers folder, in the same directory as the exe.
  5. It might work, I'm not sure... Feel free to try it but I doubt it will work.

Hello,
first of all thanks for the tutorial. I still have problems because the .dll Files aren't created. Here is part of the output.

g++ -Wl,-s -shared -Wl,--out-implib,C:\Qt\Qt5.2.0\5.2.0\Src\qtbase\plugins\sqldr
ivers\libqsqlmysql.a -o ..\..\..\..\plugins\sqldrivers\qsqlmysql.dll .obj/releas
e/main.o .obj/release/qsql_mysql.o .obj/release/moc_qsql_mysql_p.o C:\Qt\mysql\
lib\libmysql.lib -LC:\Qt\Qt5.2.0\5.2.0\mingw48_32\lib -lQt5Sql -lQt5Core
mingw32-make[1]: Leaving directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/s
qldrivers/mysql'
mingw32-make -f Makefile.Debug all
mingw32-make[1]: Entering directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/
sqldrivers/mysql'
C:\Qt\Qt5.2.0\5.2.0\mingw48_32\bin\moc.exe -DUNICODE -DQT_NO_CAST_TO_ASCII -DQT_
NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -D_
_GNUC__ -DWIN32 -I. -I"C:\Qt\mysql\include" -I"C:\Qt\Qt5.2.0\5.2.0\mingw48_32\in
clude\QtSql\5.2.0" -I"C:\Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtSql\5.2.0\QtSql"
-I"C:\Qt\Qt5.2.0\5.2.0\mingw48_32\include" -I"C:\Qt\Qt5.2.0\5.2.0\mingw48_32\inc
lude\QtSql" -I"C:\Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtCore\5.2.0" -I"C:\Qt\Qt5
.2.0\5.2.0\mingw48_32\include\QtCore\5.2.0\QtCore" -I"C:\Qt\Qt5.2.0\5.2.0\mingw4
8_32\include\QtCore" -I".moc\debug" -I"..\..\..\..\mkspecs\win32-g++" main.cpp -
o .moc\debug\main.moc

Even the .a Files aren't created.

C:\Qt\Qt5.2.0\5.2.0\Src\qtbase\src\plugins\sqldrivers\mysql>mingw32-make install

mingw32-make -f Makefile.Release install
mingw32-make[1]: Entering directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/
sqldrivers/mysql'
copy /y "..\..\..\..\plugins\sqldrivers\qsqlmysql.dll" "C:\Qt\Qt5.2.0\5.2.0\ming
w48_32\plugins\sqldrivers\qsqlmysql.dll"
1 Datei(en) kopiert.
copy /y C:\Qt\Qt5.2.0\5.2.0\Src\qtbase\lib\cmake\Qt5Sql\Qt5Sql_QMYSQLDriverPlugi
n.cmake C:\Qt\Qt5.2.0\5.2.0\mingw48_32\lib\cmake\Qt5Sql
1 Datei(en) kopiert.
mingw32-make[1]: Leaving directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/s
qldrivers/mysql'
mingw32-make -f Makefile.Debug install
mingw32-make[1]: Entering directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/
sqldrivers/mysql'
copy /y "..\..\..\..\plugins\sqldrivers\qsqlmysqld.dll" "C:\Qt\Qt5.2.0\5.2.0\min
gw48_32\plugins\sqldrivers\qsqlmysqld.dll"
1 Datei(en) kopiert.
copy /y C:\Qt\Qt5.2.0\5.2.0\Src\qtbase\lib\cmake\Qt5Sql\Qt5Sql_QMYSQLDriverPlugi
n.cmake C:\Qt\Qt5.2.0\5.2.0\mingw48_32\lib\cmake\Qt5Sql
1 Datei(en) kopiert.
mingw32-make[1]: Leaving directory 'C:/Qt/Qt5.2.0/5.2.0/Src/qtbase/src/plugins/s
qldrivers/mysql'

Even calling the mingw32-make install doesn't throw any error but the files don't exist.

its possible ..

thanks

Thanks a lot!

Pages