This answer quotes ChatGPT
If you have compiled the MySQL driver and put the mysql file in the bin directory of the Qt compiler, but still cannot connect to the MySQL database, try the following solution:
1, verify that the connection string is correct
When connecting to the MySQL database, you need to specify the correct connection string, for example:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("password");
2. Make sure that the host name, database name, username, and password in the connection string are correct.
3. Verify that libmysql.dll loads correctly.
To connect to MySQL database in Qt application, you need to use libmysql.dll dynamic link library. Check that the libmysql.dll file exists in the bin directory of the Qt compiler and ensure that the application can load the file correctly.
4. You can use the Dependency Walker tool to check that libmysql.dll loads correctly. If a red error appears in the dependency list, the libmysql.dll file will not load correctly. At this point, you need to make sure you have all the required DLL files installed on your system and add them to the PATH environment variable.
5, verify that the compiler and MySQL bits match
If your compiler is 64-bit, but MySQL is 32-bit, you will have a connection failure problem. Make sure that the compiler and MySQL match the number of bits, either 32 or 64.
6. Check whether the MySQL installation path is correct.
Ensure that the libmysql.dll file is stored in the bin directory of the MySQL installation directory. If you copy the libmysql.dll file into the bin directory of the Qt compiler, make sure you copy the correct version of the libmysql.dll file.
If the above methods do not solve the problem, you may consider recompiling the MySQL driver and using debug mode to view error messages, or using a third-party MySQL C++ connection library to connect to the MySQL database.