Home > Mobile >  QMYSQL driver not loaded on Mac OS for Mac M1/M2 users
QMYSQL driver not loaded on Mac OS for Mac M1/M2 users

Time:10-03

When I run following code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("SecureChat");
    db.setUserName("root");
    db.setPassword("zTmUHsbEKZZlWhfofM");
    bool ok = db.open();

    qDebug() << db.lastError();

I receive error: QT/C QSqlDatabase: QMYSQL driver not loaded on OSx

How to fix it on Mac m1?

CodePudding user response:

The original solution I have found here thanks to the original author of the question and answer - chriam.

I will describe in this post some key points that are not mentioned in the original solution.

  1. You have to install MySQL from Oracle cloud

  2. Use QT maintenanceTool and choose the option Add or remove components. From the list, choose your current QT version and put a mark on Sources, then click next and wait for files to download.

  3. Follow insctruction here to install ninja

  4. cd to your Src folder in my case: cd /Users/lamens/Qt/6.3.2/Src

  5. Run the following command and wait for its complitaion ./configure -sql-mysql -- -DCMAKE_INCLUDE_PATH="/usr/local/mysql/include" -DCMAKE_LIBRARY_PATH="/usr/local/mysql/lib"

  6. cd to your sqldrivers folder in my case: cd /Users/lamens/Qt/6.3.2/macos/plugins/sqldrivers

  7. Run mkdir build_sqldrivers and then cd build_sqldrivers

  8. Run command: /Users/<user>/Qt/<qt_version>/macos/bin/qt-cmake -G Ninja /Users/<user>/Qt/<qt_version>/Src/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=/Users/<user>/Qt/<qt_version>/macos -DMySQL_INCLUDE_DIR="/usr/local/mysql/include" -DMySQL_LIBRARY="/usr/local/mysql/lib/libmysqlclient.dylib" -DCMAKE_OSX_ARCHITECTURES="arm64 Where <user> is your system user and <qt_version> is your QT version :D.

  9. sed -i -e 's/-arch x86_64/-arch arm64/g' /Users/<user>/Qt/<qt_version>/macos/plugins/sqldrivers/build_sqldrivers/build.ninja if this fails, change at the build.ninja (it is at the build_sqldrivers folder) file all occurrences of arch x86_64 to the arch arm64.

  10. Run at the build_sqldrivers folder cmake --build .

  11. Run at the build_sqldrivers folder cmake --install .

  12. Then locate your lib using: find ~/Qt -name libqsqlmysql.dylib and move newly generated libqsqlmysql.dylib to the /Users/<user>/Qt/<qt_version>/macos/plugins/sqldrivers folder.

  13. Voilaa!

  • Related