当前位置: 代码迷 >> 综合 >> 彻底解决 linux Qt5.14 不能连接mysql 错误:QSqlDatabase: MYSQL driver not loaded
  详细解决方案

彻底解决 linux Qt5.14 不能连接mysql 错误:QSqlDatabase: MYSQL driver not loaded

热度:31   发布时间:2023-11-22 03:04:52.0

看了很多博客都没能解决问题:
一.问题
连接数据库报错如下:

QSqlDatabase: MYSQL driver not loadedQSqlDatabase: 
available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

二.环境:

Linux xxxxx-Veriton-E430 5.4.0-70-generic #78~18.04.1-Ubuntu SMP Sat Mar 20 14:10:07 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

三.前提:Qt安装时候安装了源码

  1. 安装QT源代码。

QT安装包提供了源代码,安装QT时可以选择安装源代码,也可以单独到QT官方网站下载源代码。

  1. 安装依赖库
sudo apt-get install mysql-client libmysqlclient-dev

编译过程中可能会遇到的问题,提前检查一下。

Cannot read /opt/Qt5.14.0/5.14.0/Src/qtbase/src/plugins/sqldrivers/qtsqldrivers-config.pri: No such file or directory
/opt/Qt5.14.0/5.14.0/gcc_64/mkspecs/features/qt_plugin.prf:69: Cannot touch /opt/Qt5.14.0/5.14.0/Src/qtbase/src/plugins/sqldrivers/mkspecs/modules/qt_plugin_qsqlmysql.pri: Operation not permitted.
Project ERROR: Library 'mysql' is not defined.

解决如上问题如下:
/opt/Qt5.14.0/5.14.0/Src/qtbase/src/plugins/sqldrivers/qsqldriverbase.pri

  1 QT  = core core-private sql-private2 3 # For QMAKE_USE in the parent projects.   #修改这个地方4 #include($$shadowed($$PWD)/qtsqldrivers-config.pri)5 include($$shadowed($$PWD)/configure.pri)                                                                                                                  6 7 PLUGIN_TYPE = sqldrivers8 load(qt_plugin)9 10 DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII

/opt/Qt5.14.0/5.14.0/Src/qtbase/src/plugins/sqldrivers/mysql/mysql.pro

1 TARGET = qsqlmysql2 3 HEADERS += $$PWD/qsql_mysql_p.h4 SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp5 6 #QMAKE_USE += mysql   #注释掉此项7 8 OTHER_FILES += mysql.json9 10 PLUGIN_CLASS_NAME = QMYSQLDriverPlugin11 include(../qsqldriverbase.pri)

编译

cd /opt/Qt5.14.0/5.14.0/Src/qtbase/src/plugins/sqldrivers/mysql
执行以下命令
sudo /opt/Qt5.14.0/5.14.0/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/x86_64-linux-gnu/ -lmysqlclient" mysql.prosudo makesudo make install

成功后查看:

xxxxxx@xxxxxx-Veriton-E430:/opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers$ ldd libqsqlmysql.solinux-vdso.so.1 (0x00007ffc8206f000)libmysqlclient.so.20 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 (0x00007f9b77e5e000)libQt5Sql.so.5 => /opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007f9b77c15000)libQt5Core.so.5 => /opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007f9b7743e000)libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9b770b5000)libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9b76cc4000)libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9b76ac0000)libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f9b768a3000)libssl.so.1.1 => /usr/lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007f9b76616000)libcrypto.so.1.1 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f9b7614b000)libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9b75f2c000)libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9b75d14000)libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9b75976000)libicui18n.so.56 => /opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007f9b754dd000)libicuuc.so.56 => /opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007f9b75125000)libicudata.so.56 => /opt/Qt5.14.0/5.14.0/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007f9b73742000)libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f9b73540000)libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f9b73229000)/lib64/ld-linux-x86-64.so.2 (0x00007f9b78629000)libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f9b72fb7000)

运行结果

"QSQLITE"
"QMYSQL"
"QMYSQL3"
"QODBC"
"QODBC3"
"QPSQL"
"QPSQL7"
--------------------------------
连接数据库ok
  相关解决方案