Development

[Qt5] MySQL(MariaDB) Driver ์ธ์‹ ์‹œํ‚ค๊ธฐ ( ์œˆ๋„์šฐ ํ™˜๊ฒฝ )

JayKim๐Ÿ™‚ 2023. 7. 31. 15:58

QT์—์„œ SqlDatabase ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ DB๋ฅผ ์ง€์›ํ•œ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ SQLite ๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ MariaDB๋‚˜ MySQL ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
์ด ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” QT๋ฅผ ์„ค์น˜์‹œ Source ๋„ ํ•จ๊ป˜ ์„ค์น˜ ๋˜์–ด์•ผ๋งŒ ํ•œ๋‹ค.

MySQL ๋“œ๋ผ์ด๋ฒ„ ์ธ์‹ ๊ด€๋ จ QT ๋ฌธ์„œ ์ฃผ์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
https://doc.qt.io/qt-5/sql-driver.html

๋จผ์ € MySQL ๋˜๋Š” MariaDB ๋ฅผ ์„ค์น˜ ํ•œ๋‹ค.
https://dev.mysql.com/downloads/c-api/

๊ทธ๋ฆฌ๊ณ 
C:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers
ํด๋”์—์„œ qmake ์‹คํ–‰ ํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๋กœ์™€ mingw32-make ํด๋”์˜ ๊ฒฝ๋กœ๋Š” ์ด๋ฏธ ์„ค์ •์ด ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

C:\Qt\Tools\mingw810_64\bin // For mingw32-make ์„ค์ •
C:\Qt\5.15.2\mingw81_64\bin // qmake ๊ฒฝ๋กœ ์„ค์ •

qmake -- MYSQL_INCDIR="D:/work/mysql_x64/include" MYSQL_LIBDIR="D:/work/mysql_x64/lib"
์‹คํ–‰ ํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ์„ค์น˜ ํŒŒ์ผ์ด D:/mysql_x64 ํด๋”์ธ ๊ฒฝ์šฐ์ด๋‹ค.

์ด๋•Œ MySQL์— no ๊ฐ€ ๋‚˜์˜ค๋ฉด ํ•ด๋‹น ํด๋”์˜ config.cache ํŒŒ์ผ์„ ์ œ๊ฑฐ ํ•˜๊ณ  ๋‹ค์‹œ ์‹คํ–‰ ํ•œ๋‹ค.
์ฐธ๊ณ ๋กœ ๊ด€๋ จ ๋กœ๊ทธ๋Š” config.log ํŒŒ์ผ์„ ํ™•์ธ ํ•œ๋‹ค.

C:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers>qmake -- MYSQL_INCDIR="D:/work/mysql_x64/include" MYSQL_LIBDIR="D:/work/mysql_x64/lib"

Running configuration tests...
Checking for DB2 (IBM)... no
Checking for InterBase... no
Checking for MySQL... yes
Checking for OCI (Oracle)... no
Checking for ODBC... yes
Checking for PostgreSQL... no
Checking for SQLite (version 2)... no
Checking for TDS (Sybase)... no
Done running configuration tests.

Configure summary:

Qt Sql Drivers:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. yes
  OCI (Oracle) ........................... no
  ODBC ................................... yes
  PostgreSQL ............................. no
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... no

Qt is now configured for building. Just run 'mingw32-make'.
Once everything is built, you must run 'mingw32-make install'.
Qt will be installed into 'C:\Qt\5.15.2\mingw81_64'.

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹ํ–‰ ํ•œ๋‹ค.

mingw32-make
mingw32-make install

์‹ค์ œ mysql ๋“œ๋Ÿฌ์ด๋ธŒ ์ ์šฉ์€ C:\Qt\5.15.2\mingw81_64\plugins\sqldrivers ํด๋”์— qsqlmysql.dll ๊ณผ qsqlmysql.dll.debug ๋ฅผ ๋ณต์‚ฌ ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์„ค์น˜ํ•œ libmysql.dll ๊ณผ libmysql.lib ํŒŒ์ผ์„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ๋กœ์˜ bin ํด๋”์— ๋ณต์‚ฌํ•ด ์ค€๋‹ค.
C:\Qt\5.15.2\mingw81_64\bin ํด๋”์— ๋ณต์‚ฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์ ์šฉ์€ deploy ๋ช…๋ น์„ ์‹คํ–‰์‹œ sqldrivers ์— ๋ณต์‚ฌ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ค์น˜ ์ „์— deploy๋ฅผ ํ–ˆ์œผ๋ฉด ์ˆ˜๋™์œผ๋กœ ๋ณต์‚ฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Mysql ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์„ค์น˜๊ฐ€ ๋˜๋ฉด MariaDB ์™€ MySQL ๋ชจ๋‘ ๋“œ๋ผ์ด๋ฒ„ ๋ชฉ๋ก์— ๋‚˜ํƒ€๋‚œ๋‹ค.

MySQL ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜ํ›„ ๋ชฉ๋ก์ด๋‹ค ( QSqlDatabase::drivers() ์ด์šฉ )

0 : QSQLITE
1 : QMARIADB
2 : QMYSQL
3 : QMYSQL3
4 : QODBC
5 : QODBC3
6 : QPSQL
7 : QPSQL7

์ฐธ๊ณ ๋กœ QSqlDatabase ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ๋งŒ ์žˆ์œผ๋ฉด ์ธ์‹์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์‹คํ–‰ ํŒŒ์ผ์˜ sqldrivers ํด๋”์— qsqlmysql.dll ํŒŒ์ผ์„ ๊ทธ๋ƒฅ ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
๋ฌผ๋ก  qsqlmysql.dll ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” libmysql.dll ํŒŒ์ผ๋„ ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

MACOS MySQL ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜

Here is a short torurial:

  1. install mysql via homebrew: $ brew install mysql
  2. install Qt Source (I install Qt into default path -- my $HOME directory /Users/liuhao) https://doc.qt.io/qt-5/gettingstarted.html
  3. open Terminal and change path to /Users/liuhao/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers
  4. run /Users/liuhao/Qt/5.15.2/clang_64/bin/qmake -- MYSQL_PREFIX=/usr/local/Cellar/mysql (watch out for the spaces around --) and you will get some messages like:
$ /Users/liuhao/Qt/5.15.2/clang_64/bin/qmake  -- MYSQL_PREFIX=/usr/local/Cellar/mysql

Running configuration tests...
Done running configuration tests.

Configure summary:

Qt Sql Drivers:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. yes
  OCI (Oracle) ........................... no
  ODBC ................................... no
  PostgreSQL ............................. no
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... no

Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into '/Users/liuhao/Qt/5.15.2/clang_64'.

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.
  1. $ make sub-mysql and then you can find new mysql driver dylib files in /Users/liuhao/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers
$ ls -alh /Users/liuhao/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers
total 3360
drwxr-xr-x  6 liuhao a   192B Aug 20 18:29 .
drwxr-xr-x  3 liuhao a    96B Aug 20 16:17 ..
-rwxr-xr-x  1 liuhao a   1.5M Aug 20 18:24 libqsqlite.dylib
drwxr-xr-x  3 liuhao a    96B Aug 20 18:23 libqsqlite.dylib.dSYM
-rwxr-xr-x  1 liuhao a    94K Aug 20 18:29 libqsqlmysql.dylib
drwxr-xr-x  3 liuhao a    96B Aug 20 16:17 libqsqlmysql.dylib.dSYM

  1. copy it to /Users/liuhao/Qt/5.15.2/clang_64/plugins/sqldrivers/ and you will be able to use it in qt project.
foreach (QString driver, QSqlDatabase::drivers ())
{
    qDebug() << driver;
}
/*
available drivers
...
"QMARIADB"
"QMYSQL"
"QMYSQL3"
...
*/