QSqlDatabase: MYSQL driver not loaded appears in QT in Centos 7
System Version: Centos7
Qt version: 5.6
Test code:
# Include <iostream> using namespace std; # include <QApplication> # include "gui/mainwindow. h "# include <QSqlDatabase> # include <QDebug> # include <QSqlError> # include <QSqlQuery> using namespace std; int main (int argc, char ** argv) {// output available database QCoreApplication: addLibraryPath ("/home/njq/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers"); qDebug () <"available drivers: "; QStringList drivers = QSqlDatabase: driver S (); foreach (QString driver, drivers) qDebug () <driver; // open MySQL QSqlDatabase data_base = QSqlDatabase: addDatabase ("QMYSQL "); data_base.setHostName ("192.168.0.200"); // set the host address data_base.setPort (3306); // set the port data_base.setDatabaseName ("alarmcenter"); // set the database name data_base.setUserName ("root "); // set the username data_base.setPassword ("root"); // set the password if (! Data_base.open () qDebug () <"failed to connect to mysql" <data_base.lastError (). text (); else {qDebug () <"success"; QString strSQL = "SELECT alarm_id FROM alarm;"; QSqlQuery SQL _query; SQL _query.prepare (strSQL); SQL _query.exec (); while (SQL _query.next () {int id = SQL _query.value (0 ). toInt (); qDebug () <id ;}}}
The following error occurs:
QSqlDatabase: MYSQL driver not loadedQSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7QSqlDatabase: an instance of QCoreApplication is required for loading driver pluginsfailed to connect to mysql "Driver not loaded Driver not loaded"
View the installation directory of QT
~/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers
There is a file:
libqsqlmysql.so
Run the ldd command to check the dependency
[xxx@localhost sqldrivers]$ ldd libqsqlmysql.so linux-vdso.so.1 => (0x00007ffcf65b4000) libmysqlclient_r.so.16 => not found libz.so.1 => /lib64/libz.so.1 (0x00007f452c898000)
We can see that the so file libmysqlclient_r.so.16 is not found. By searching for information, we can find the following solution:
Re-compile the MySQL driver
[Njq @ localhost mysql] $ cd ~ /Dev_env/Qt5.6.0/5.6/Src/qtbase/src/plugins/sqldrivers/mysql [xxx @ localhost mysql] $ ~ /Dev_env/Qt5.6.0/5.6/gcc_64/bin/qmake # because the qmake path is not configured, the full path [xxx @ localhost mysql] $ make is used.
The last line after compilation is displayed.
mv -f libqsqlmysql.so ../../../../plugins/sqldrivers/
Overwrite the generated libqsqlmysql. so ....... /Qt5.6.0/5.6/gcc_64/plugins/sqldrivers
[xxx@localhost sqldrivers]$ mv libqsqlmysql.so ~/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/
Then, view the libqsqlmysql. so dependency.
[xxx@localhost sqldrivers]$ ldd libqsqlmysql.so linux-vdso.so.1 => (0x00007fffc437c000) libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f3de67bc000) libz.so.1 => /lib64/libz.so.1 (0x00007f3de65a5000) libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f3de636e000) libnsl.so.1 => /lib64/libnsl.so.1 (0x00007f3de6155000) libssl.so.10 => /lib64/libssl.so.10 (0x00007f3de5ee7000) libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007f3de5aff000) libQt5Sql.so.5 => not found libQt5Core.so.5 => not found libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3de58e2000) libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f3de55da000) libm.so.6 => /lib64/libm.so.6 (0x00007f3de52d7000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f3de50c1000) libc.so.6 => /lib64/libc.so.6 (0x00007f3de4d00000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f3de4afb000) libfreebl3.so => /lib64/libfreebl3.so (0x00007f3de48f8000) libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f3de46ac000) libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f3de43c6000) libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f3de41c2000) libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f3de3f90000) /lib64/ld-linux-x86-64.so.2 (0x00007f3de6ee3000) libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f3de3d80000) libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f3de3b7c000) libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f3de3962000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f3de373c000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f3de34db000) liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f3de32b5000)
The problem is fixed.
Run the program again. The following result is displayed:
available drivers:"QSQLITE""QMYSQL""QMYSQL3""QPSQL""QPSQL7"success171118171119171120171121171122171123171124171125171126171127171128171129171130171131171132171133171134171135171136