最近在使用qtcreate连接数据库的时候,出现了下图的错误,针对该错误,我在此给出解决方案,希望可以帮到大家。
① 分析错误原因
由QSqlDataBase:availbase drivers:QSQLITE QMYSQLQMYSQL3 QPSQL QPSQL7这句话可以看出,没有正确的加载驱动并不是由于缺少驱动导致,因此可能是由于驱动不是最新的导致,下面验证是不是真是这样。
首先使用命令cd /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers进到该目录下,然后使用lddlibqsqlmysql.so命令查看动态链接库的依赖关系,终端给出如下反馈:
linux-vdso.so.1 => (0x00007fff809fe000)
libmysqlclient_r.so.16 => not found
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fadbfc3f000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fadbfa06000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fadbf7ec000)
libssl.so.10 => not found
libcrypto.so.10 => not found
libQt5Sql.so.5 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5
(0x00007fadbf5a7000)
libQt5Core.so.5 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5
(0x00007fadbee94000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fadbec76000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fadbe972000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fadbe66b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fadbe455000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fadbe08f000)
libicui18n.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56
(0x00007fadbdbf4000)
libicuuc.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56
(0x00007fadbd83c000)
libicudata.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56
(0x00007fadbbe59000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fadbbc54000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fadbba52000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fadbb84a000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fadbb541000)
/lib64/ld-linux-x86-64.so.2 (0x00007fadc0083000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fadbb303000)
可以看到libmysqlclient_r.so.16 => not found,查看自己所有的libmysqlclient.so,在终端键入locate libmysqlclient.so命令,终端反馈如下:
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0
可知到我们的依赖有这些,可以确定就是因为mysql驱动的动态链接库的版本不一致导致。
② 重现编译
ok,咱们重新编译一下这个库。首先到qt的安装路径下找到源码的存放位置:/opt/Qt5.6.0/5.6/Src/qtbase/src/plugins/sqldrivers/mysql,然后右键在终端打开,接着使用su命令进入超级管理员身份,然后输入qmake命令,生成Makefile文件,接着输入make命令进行编译。编译过程中可能会出现以下问题:rm -f libqsqlmysql.so
g++ -Wl,--no-undefined -Wl,-O1 -Wl,--enable-new-dtags -Wl,-z,origin -Wl,-rpath,\$ORIGIN -Wl,-rpath,\$ORIGIN
-Wl,-rpath,\$ORIGIN/../../lib -shared -o libqsqlmysql.so .obj/main.o .obj/qsql_mysql.o
.obj/moc_qsql_mysql_p.o -rdynamic -L/usr/lib64/mysql -lmysqlclient_r -lz -lcrypt -lnsl -lm -lssl -lcrypto
-L/opt/Qt5.6.0/5.6/gcc_64/lib -lQt5Sql -lQt5Core -lpthread
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status
make: *** [../../../../plugins/sqldrivers/libqsqlmysql.so] 错误 1
出现该问题说明缺少必要的动态链接库,使用以下命令获取安装:
sudo apt-get install libssl-dev
sudo apt-get install libcrypto++-dev
安装结束之后,再使用make进行编译,终端反馈如下:rm -f libqsqlmysql.so
g++ -Wl,--no-undefined -Wl,-O1 -Wl,--enable-new-dtags -Wl,-z,origin -Wl,-rpath,\$ORIGIN -Wl,-rpath,\$ORIGIN
-Wl,-rpath,\$ORIGIN/../../lib -shared -o libqsqlmysql.so .obj/main.o .obj/qsql_mysql.o
.obj/moc_qsql_mysql_p.o -rdynamic -L/usr/lib64/mysql -lmysqlclient_r -lz -lcrypt -lnsl -lm -lssl -lcrypto
-L/opt/Qt5.6.0/5.6/gcc_64/lib -lQt5Sql -lQt5Core -lpthread
mv -f libqsqlmysql.so ../../../../plugins/sqldrivers/
说明已经编译成功,编译成功之后记得清理一下编译的中间文件:sudo make clean
再次键入命令cd /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers定位到驱动所在文件夹,输入命令ldd libqsqlmysql.so查看动态库依赖关系,终端反馈如下: linux-vdso.so.1 => (0x00007fff1a77d000)
libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007fabac30f000)
libQt5Sql.so.5 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5
(0x00007fabac0ca000)
libQt5Core.so.5 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5
(0x00007fabab9b8000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fabab6b4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fabab2ed000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fabab0d4000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fabaaed0000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fabaacb1000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fabaa9ab000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fabaa795000)
libicui18n.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56
(0x00007fabaa2fa000)
libicuuc.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56
(0x00007faba9f42000)
libicudata.so.56 => /opt/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56
(0x00007faba855f000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007faba835c000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007faba8154000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007faba7e4c000)
/lib64/ld-linux-x86-64.so.2 (0x00007fabaca73000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007faba7c0d000)
libmysqlclient.so.18 =>/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007fabac30f000)证明已经是最新的动态链接库了。最后再次运行代码,显示链接成功。