Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMYSQL driver not loaded on mac



  • Actually, I'm a nook on QT and MySQL.
    Platform: OS X 10.12.6```
    Qt 5.9.1
    MySQL 5.7.18
    Code:
    QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");
    data_base.setHostName("127.0.0.1");
    data_base.setPort(3306);
    data_base.setDatabaseName("student.db");
    data_base.setUserName("root@localhost");
    data_base.setPassword("lulu168168");

    ~.pro included QT+=sql

    but the result was:
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QP
    SQL7
    failed to connect to mysql

    I haved read lots of anwers on the net. However, I was so upset that I can not find /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib.
    What's more, otool -L libqsqlmysql.dylib led to /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: 'libqsqlmysql.dylib': No such file or directory.
    Who can help me T^T.
    Thanks very much!!!!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    MySQL is not installed by default on macOS, so you first have to install it. If you don't have any version of MySQL on your system you can use for example macports which from the looks of it was used for the dependencies when building the Qt package you installed.

    Your other error indicates that you don't have any file name libqsqlmysql.dylib in the folder were you called otool.



  • Thanks for your quick response very much !!!!
    I haved already installed MySQL. The version is 5.7.18. I can find MySQL file in /usr/local. There's only 2 folders in /opt: X11 and the other.
    I think I cannot express myself well.
    I'm using cellphone now.
    Maybe I can show my screenshots hours later.
    Thanks for your answer agian !!!


  • Lifetime Qt Champion

    Then you have to use otool install_name_tool to change the path from /opt/etc... to your own installation of MySQL.

    [edit: fixed tool name... SGaist]



  • Thanks a lot.
    I think I get your point.
    Could you tell me more details of otool? thanks!
    What's more, is there no problem that use tool to change that even /opt/etc not exist in my pc?


  • Lifetime Qt Champion

    Sorry, I meant install_name_tool.



  • Sorry, could you tell me the whole sentence of install_name_tool.
    I used install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/local/Cellar/mysql/5.7.19/lib/libmysqlclient.dylib libqsqlmysql.dylib for the Net. But it failed: error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool: can't open file: libqsqlmysql.dylib (No such file or directory)
    I can find libmysqlclient.20.dylib and libmysqlclient.dylib both in /mysql and /mysql-5.7.18-...
    What should I do?1_1503586014967_屏幕快照 2017-08-24 下午10.44.51.png 0_1503586014966_屏幕快照 2017-08-24 下午10.44.40.png



  • I tried install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/local/Cellar/mysql/5.7.19/lib/libmysqlclient.20.dylib

    and it showed me: Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool [-change old new] ... [-rpath old new] ... [-add_rpath new] ... [-delete_rpath old] ... [-id name] input

    What did it mean?


  • Lifetime Qt Champion

    From what you wrote, you seem to be somewhere on your filesystem and just expecting that libqsqlmysql.dylib is there.

    You have to give the full path to the Qt MySQL plugin if you're not in the folder where that file reside.



  • In fact, I used find ~ -iname "libqsqlmysql"*
    and found that
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib.dSYM
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Resources/DWARF/libqsqlmysql.dylib
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib.dSYM
    /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib.dSYM/Contents/Resources/DWARF/libqsqlmysql_debug.dylib
    /Users/lujinliang/Qt5.9.1/Qt Creator.app/Contents/PlugIns/sqldrivers/libqsqlmysql.dylib

    It seemed that 'libqsqlmysql' all in the /Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers
    Should I use
    install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
    to change the path
    Thanks.



  • I used install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.20.dylib /Users/lujinliang/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
    and finally success connected.
    I want to know whether it is the correct way.
    Thanks.


  • Lifetime Qt Champion

    Yes, it is.

    Note that when doing such a thing you have to do some testing, you are now using a client library which is from a different version that the one used to link the plugin. That may or may not work depending on the library. In this case, things look fine.



  • Thanks a lot.
    Things look good.
    Also, I want to know why QT need a strange way like /opt/local/myspl/lib/etc to connect MySQL. Is it the default way?
    P.S. it is not a problem, I just want to know.
    Thanks again.


  • Lifetime Qt Champion

    There's nothing strange here, the CI is likely using macports to get the dependencies it needs to build the plugins and macports installs its stuff by default in /opt.

    Setting up a build machine using a package manager will always be easier than having to grab tons of installers. It's usually also simpler to handle upgrades and parallel installations with these kind of tools.



  • I'm also using Qt 5.9 on macOS and initially had a similar problem with the MySQL driver not working, and solved it using install_name_tool very much as was described above. In my case, MySQL was installed on my Mac with Homebrew.

    However, now that I'm deploying the application I find that it works on my development Mac but not on other Macs. This makes sense: the libqsqlmysql.dylib in the app bundle, in the Plugins/sqldrivers directory depends on finding libmysqlclient.18.dylib in the location Homebrew uses. At the same time this doesn't make sense: how could this be the right way to do this?

    I've read a lot of posts about this issue, but haven't found anything that addresses this issue: how do I configure the plugin so that it will work on other Macs, not just ones that happen to have MySQL installed in the way I have it installed?

    Many users of my application don't even have MySQL installed. They are using my app to connect to a MySQL server on another server. I know this should be possible, because this app worked fine for my users for several years when I had it under Qt4. I'm just encountering this question because of a necessary upgrade to Qt5.


  • Lifetime Qt Champion

    Copy the library in your app bundle and use install_name_tool again to point the MySQL plugin to that library.



  • Hi guys,
    I have the same problem, but the above solutions and also all the solutions on the web do not work for me.
    I am using Mac OS X Sierra, Qt5.9.1 installed at Home directory, Mysql 5.7.19 at /usr/local/mysql.
    I would be pleased to help me to solve my problem on connection to QMSQL plugin.
    I am wonder while all the new users has the same problem why Qt does not fix this bug which is seen in most of versions and claimed by many users. Half of the users' questions are about connecting to QMYSQL which is a real part of programming.
    The error is:
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
    QSqlError("", "Driver not loaded", "Driver not loaded")

    The stuffs I have done:
    1- hasan-mac:/ hasan$ otool -L libqsqlmysql.dylib
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: 'libqsqlmysql.dylib': No such file or directory
    2- linking the /usr/local/mysql/lib/libmysqlclient.20.dylib(and libmysqlclient.dylib) to /usr/lib
    I think I could not run install_name_tool in the right way.
    need your help please as I waste 2days searching the web for creating plugins and installing libs and so on...

    regards,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    1. This won't work because you are calling otool in some random place where libqsqlmysql.dylib is not located. Either give the full path to the plugin or cd into the sql plugins folder in your Qt installation and call that command again.

    2. Likely for the same reason as number 1.



  • Hi SQaist,
    Thank you for your prompt reply.
    The result on otool is:
    MacBook-Pro:sqldrivers hasan$ otool -L /Users/hasan/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
    /Users/hasan/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib:
    libqsqlmysql.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/QtSql.framework/Versions/5/QtSql (compatibility version 5.9.0, current version 5.9.1)
    @rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.9.0, current version 5.9.1)
    /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0)
    /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
    /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
    0_1505932528428_Screen Shot 2017-09-20 at 11.03.42 PM.png



  • Tried to use the following command and problem solved.
    install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.20.dylib /Users/hasan/Qt5.9.1/5.9.1/clang_64/plugins/sqldrivers/libqsqlmysql.dylib

    The thing I am worry about is when I will face to problem as we tricked to Qt to use libqslmysql.20.dylib instead of the the one in version 18 (libmysqlclient.18.dylib).
    Mainly the problem is solved but it should not be OK. I am looking for a way to solve the problem in a correct way and do not want to use MACPORTS for installing the QMYSQL module. MACPORTS is a good installation package, but its size is more than 1GB in HDD and I'd prefer to use certified sourced like mysql.com website and qt.io website and so on...


  • Lifetime Qt Champion

    Then just install the package you want from whatever source suits you best and change the install_name_tool call accordingly.



  • OK man. Thank you for your attention :)



  • @SGaist Thanks for putting me on the right path. I solved my issue and just wanted to post what I learned for others to benefit from:

    I had used install_name_tool to tell my PlugIns/sqldrivers/libqsqlmysql.dylib to find the libmysqlclient.dylib that was on my Mac (in my case installed using Homebrew). That worked fine until I deployed to other Macs and then they got the "Driver Not Loaded" error. I needed libqsqlmysql.dylib to link to something inside the app bundle.

    It turns out that macdeployqt was copying libmysqlclient.dylib into my Frameworks directory (the output clearly shows this) but still, it didn't work!

    ls -l Frameworks/*dylib
     -rw-r--r--  1   1919816 22  libcrypto.1.0.0.dylib
     -rw-r--r--  1   3723040 22  libmysqlclient.dylib
     -rw-r--r--  1    375376 22  libssl.1.0.0.dylib
    

    The first thing I noticed about this was that the file size of libmysqlclient.dylib was smaller after the copy:

    /usr/local/Cellar/mysql/5.7.18/lib/libmysqlclient.dylib -> libmysqlclient.20.dylib
    /usr/local/Cellar/mysql/5.7.18/lib/libmysqlclient.20.dylib  3773144
    

    3723040 vs 3773144 bytes? I was convinced this was the problem and wasted hours trying to figure it out. I still don't understand this, but it isn't the problem. It was a red herring.

    The real problem is that libmysqlclient.dylib depends on those other 2 dylib's copied into the Frameworks directory by macdeployqt: libssl.1.0.0.dylib and libcrypto.1.0.0.dylib. I ran otool -L on each in turn, and found that libssl.1.0.0.dylib was depending on a copy of libcrypto.1.0.0.dylib outside the app bundle (again the homebrew location).

    So, I had to fix the dependency of two dylib's mid-way through my build steps:

    make clean
    qmake -config release
    make
    macdeployqt MyApp.app -dmg
    

    'make' creates MyApp.app but the Frameworks and Plugins aren't put inside it until the first execution of macdeployqt. After that, I go in and fix the dependencies:

    cd MyApp.app/Contents/Frameworks
    install_name_tool -change /usr/local/Cellar/openssl/1.0.2l/lib/libcrypto.1.0.0.dylib @executable_path/../Frameworks/libcrypto.1.0.0.dylib libssl.1.0.0.dylib
    cd ../PlugIns/sqldrivers
    install_name_tool -change /usr/local/mysql/lib/libmysqlclient.dylib @executable_path/../Frameworks/libmysqlclient.dylib libqsqlmysql.dylib
    cd ../../../..
    rm -rf MyApp.dmg
    macdeployqt MyApp.app -dmg
    

    After fixing the dependencies with install_name_tool, I come back to the top-level directory and delete the DMG created by that run of macdeployqt. The second time macdeployqt is executed it does not replace MyApp.app. It just complains that much of what it was going to do is done already. For example:

    File exists, skip copy: "MyApp.app/Contents/PlugIns/sqldrivers/libqsqlmysql.dylib"
    

    The second execution of macdeployqt creates the DMG with the correctly-linked dylibs.

    If someone else has a similar problem, don't just copy the above steps. Use the otool -L tool on the dylibs in your app bundle to see where the dependencies are expected to be, then check to see if they are actually there. If you are deploying your app bundle to somewhere else, consider which dylib's need to be copied into your app bundle and which you can count on being on the target Mac. Finally, use install_name_tool to fix your dependencies. Hope this helps!



  • im using qt 5.9 and isolated install on mysql 5.7.20 on mac this piece of code worked like charm for me

    brew install mysql-connector-c
    

    I already had MAMP installed.... and wanted to use the mysql from mamp running on port 8889 this code helped me to do that

    ```
    

    db.setHostName("localhost");
    db.setConnectOptions("UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock");
    db.setDatabaseName("xxxxxx");
    db.setUserName("root");
    db.setPassword("root");
    db.setPort(8889);



  • Hi, what i have done are as pervious.but it shows that

    //-bash: /Users/lucas/Qt5.9.8/5.9.8/clang_64/plugins/sqldrivers/libqsqlmysql.dylib: cannot execute binary file"
    

    how can i fix it? thx for your help:)


  • Qt Champions 2019

    @edwinxxxx said in QMYSQL driver not loaded on mac:

    /Users/lucas/Qt5.9.8/5.9.8/clang_64/plugins/sqldrivers/libqsqlmysql.dylib

    You can't execute a library.
    What are you trying to do?



  • @jsulm coz mysql driver can't be loaded in qt creator , i followed the directions above which has been proved that it was a successful way to solve the problem.but error happens.


  • Qt Champions 2019

    @edwinxxxx Please tell us the exact step your executing when you get that error message




  • Qt Champions 2019

    @edwinxxxx The last thig you did does not make sense: you're trying to execute a lib.
    The first one was not successful - you called it in a wrong way. Take a look at its output ("Usage:"), it explains how to call it.
    @SGaist - you're the Mac expert, can you take a look?



  • @jsulm thanks for your tips.i have wasted the whole day to solve it😂


  • Lifetime Qt Champion

    @edwinxxxx so it's now working correctly ?



  • @sgaist
    platform: macos 10.14
    mysql 8.0.17
    qt 5.9.8

    firstly i run

    //otool -L libqsqlmysql.dylib
    

    but it shows

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: 'libqsqlmysql.dylib': No such file or directory
    

    how can i find the path like "opt/local..." on my machine?
    thanks man.


  • Qt Champions 2019

    @edwinxxxx said in QMYSQL driver not loaded on mac:

    how can i find the path like "opt/local..." on my machine?

    find / -name libqsqlmysql.dylib
    


  • @jsulm sorry, i still cant find any folder whose name begin with "/opt/local..."

    0_1564726030510_屏幕快照 2019-08-02 14.07.02.png


  • Qt Champions 2019

    @edwinxxxx "/opt" is a system folder just under root /



  • @jsulm is that mean i cant locate the file "libmysqlclient.xx.dylib" in my pc?


  • Qt Champions 2019

    @edwinxxxx No, it doesn't mean that. If it is somewhere on your machine you can find it.
    Where did you install it?
    Did you try what I suggested?

    find / -name libmysqlclient.*.dylib
    


  • @jsulm Yes! it shows a lot of contents, and the related information is
    0_1564727625430_屏幕快照 2019-08-02 14.32.18.png


  • Qt Champions 2019

    @edwinxxxx So, it is in /usr/local, not /opt/local.


Log in to reply