Mac OS Catalina下编译QT MySQL驱动遇问题求助
Hey there, let's work through this MySQL driver issue step by step— I've been stuck on this exact problem before, so I know how frustrating it can be when you're just trying to learn and hit a roadblock like this. Let's break it down simply:
First, Let's Understand the Root Problem
Qt's online installer doesn't ship with the MySQL driver pre-compiled, so we need to build it ourselves. Your earlier attempts failed because you were working in the wrong directory and using incorrect qmake commands. Let's fix that.
Step 1: Install Qt's Source Code Component
First, you need the Qt source files to compile the driver. Since you used the online installer:
- Open the Qt Maintenance Tool (it's in your Qt installation folder:
/Users/Edward/Qt/MaintenanceTool.app) - Select "Add or Remove Components"
- Expand the Qt 5.14.2 section, find and check the Sources option, then click "Apply" to install the source code.
Step 2: Navigate to the Correct MySQL Driver Source Directory
Forget the /plugins/sqldrivers folder you were using earlier— that's where compiled drivers go, not where we build them. Head to the source code's MySQL driver folder instead:
cd /Users/Edward/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql
Step 3: Set Up Your Environment for Qt's qmake
Make sure you're using the qmake that comes with your Qt 5.14.2 installation (not a system-wide one). First, check which qmake your terminal is using:
which qmake
If it doesn't show /Users/Edward/Qt/5.14.2/clang_64/bin/qmake, add Qt's bin folder to your PATH temporarily:
export PATH=/Users/Edward/Qt/5.14.2/clang_64/bin:$PATH
Step 4: Run qmake with the Correct MySQL Path
Your earlier qmake command was missing two key things: pointing to the mysql.pro file, and using the exact path to your MySQL installation (not just /usr/local). Run this command:
qmake MYSQL_PREFIX=/usr/local/mysql-8.0.19-macos10.15-x86_64 mysql.pro
This tells qmake where your MySQL headers and libraries are located. If you get no errors here, you're on the right track!
Step 5: Compile and Install the Driver
Now compile the driver with make, then install it to Qt's plugin folder:
make make install
Wait for the compilation to finish— it might take a minute or two. Once done, the compiled libqsqlmysql.dylib file will automatically be copied to /Users/Edward/Qt/5.14.2/clang_64/plugins/sqldrivers.
Step 6: Verify the Driver Works
Restart Qt Creator, then create a simple test project to check if the driver loads:
#include <QCoreApplication> #include <QSqlDatabase> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "Available drivers:" << QSqlDatabase::drivers(); return a.exec(); }
Run this— you should now see QMYSQL and QMYSQL3 in the list of available drivers.
Quick Troubleshooting Tips
- If you get a "cannot verify developer" error when running your app: Go to System Preferences > Security & Privacy, and click "Allow Anyway" for the driver file.
- Double-check that your MySQL installation is x86_64 (same as Qt's clang_64 build)— your MySQL 8.0.19 version for macOS 10.15 is correct here.
- You don't need the MySQL Connector/C++ you installed earlier— Qt's driver uses MySQL's client library directly, so that component can be ignored for this process.
内容的提问来源于stack exchange,提问作者EdBatt

