Search code examples
c++mysqlqtqsqldatabase

QT/C++ QSqlDatabase: QMYSQL driver not loaded on OS X


I'm using
OS X: 10.12.4
Qt Creator 4.0.2
MySQL 5.0.12 (looks like that, not sure)
C++

Under from QT I am trying to connect to a mysql database by following code:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("sql104.rf.gd"); // 185.27.134.10
//db.setPort(3306);
db.setUserName("correctname");
db.setPassword("correctpw");
db.setDatabaseName("rfgd_19926673_shop");

if (db.open()){
   ui->label->setText("success");
   } else {
   i->label->setText("fail");
}

And it fails with

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

I've tried this with no result

QPluginLoader loader;
loader.setFileName("/Users/Ivan/Qt/5.7/clang_64/plugins/sqldrivers/libqsqlmysql.dylib");

It returns

Cannot load library 
/Users/Ivan/Qt/5.7/clang_64/plugins/sqldrivers/libqsqlmysql.dylib: (dlopen(/Users/Ivan/Qt/5.7/clang_64/plugins/sqldrivers/libqsqlmysql.dylib, 5): Library not loaded: /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib
Referenced from: /Users/Ivan/Qt/5.7/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
Reason: image not found)
/Users/Ivan/build-CourierHelperDesktop-Desktop_Qt_5_7_0_clang_64bit-Release/CourierHelperDesktop.app/Contents/MacOS

I've got only

/usr/local/mysql-5.7.17-macos10.12-x86_64/lib/lib/mysqlclient.20.dylib

Have tried

mkdir /opt/local/lib/mysql55/mysql/
cp /usr/local/mysql-5.7.17-macos10.12-x86_64/lib/lib/mysqlclient.20.dylib /opt/local/lib/mysql55/mysql

No help.

Please, somebody, help me out. I am really stuck.


Solution

  • I had this problem on macOS High Sierra (10.13.4) with:

    • mysql-5.6.40-macos10.13-x86_64.dmg
    • mysql-connector-c-6.1.11-macos10.12-x86_64.dmg
    • Qt 5.10.1
    • clang: Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-darwin17.5.0

    You were lucky, I didn't get this message at first. I had to enabled more debugging info by setting a new environment variable called QT_DEBUG_PLUGINS as 1 on Project Properties > Run. Executing my application again revealed pretty much the same error message as yours.

    To solve the problem, the first thing you need to do is find where libmysqlclient.18.dylib is located in the computer:

    $ find / -iname libmysqlclient.18.dylib
    /usr/local/mysql/lib/libmysqlclient.18.dylib
    

    Great, now find where Qt stores its plugins:

    $ qmake -query QT_INSTALL_PLUGINS
    /Users/karlphillip/Qt/5.10.1/clang_64/plugins
    

    and create a new environment variable on your Terminal with this information to make the next part easier:

    $ export QT_PLUGIN_PATH=`qmake -query QT_INSTALL_PLUGINS`
    

    Finally, go to the sqldrivers inside Qt plugins directory and update the shared library path with the information you found earlier:

    $ cd /Users/karlphillip/Qt/5.10.1/clang_64/plugins/sqldrivers
    $ install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib $QT_PLUGIN_PATH/sqldrivers/libqsqlmysql.dylib
    

    Done.