QT / C ++ QSqlDatabase: QMYSQL driver not loaded on OS X
I am using OS X: 10.12.4
Qt Creator 4.0.2
MySQL 5.0.12 (looks like this, not sure)
C ++
According to QT, I am trying to connect to mysql database with the 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 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 have 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. I am really stuck.
source to share
I had this problem on macOS High Sierra (10.13.4) with:
- MySQL-5.6.40-macos10.13-x86_64.dmg
- Mysql-socket-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're in luck, I didn't receive this message at first. I had to enable additional debugging information by setting a new environment variable QT_DEBUG_PLUGINS
as 1
in Project Properties> Run . Executing my application again showed almost the same error message as yours.
To fix the problem, the first thing you need to do is find where libmysqlclient.18.dylib
on 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 in your terminal with this information to facilitate the next part:
$ export QT_PLUGIN_PATH=`qmake -query QT_INSTALL_PLUGINS`
Finally, go to the directory sqldrivers
inside the Qt plugins 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.
source to share