首先贴上我遇到的问题,如下:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
Unable to open database
在解决这个问题的时候,首先在网上查询了各种解决方案,都尝试过,但是其中遇到的一些小问题,总是找不到解决方法,最终在"Stack Overflow"上提问得到了答案,故此记录一下,一遍以后备用,也希望能帮助到一些人.
环境: ubantu16.04 + Qt5.7 + MySql5.7
分析
安装方法:(该链接里面同时也包含卸载方法)
2. 进入驱动所在目录:
cd /ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers
3. ls 查看目录里面的文件:
cc@1whispers:~/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers$ ls
libqsqlite.so libqsqlmysql.so libqsqlpsql.so
4. 执行命令查看 libqsqlmysql.so 的依赖:
ldd libqsqlmysql.so
显示如下:
linux-vdso.so.1 => (0x00007ffcb64e2000)
libmysqlclient_r.so.16 => not found
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fea06463000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fea0622a000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fea06011000)
libssl.so.10 => /usr/lib/x86_64-linux-gnu/libssl.so.10 (0x00007fea05dba000)
libQt5Sql.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007fea05b74000)
libQt5Core.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007fea05459000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fea0523c000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fea04eb9000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea04bb0000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fea0499a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea045d0000)
libnss3.so => /usr/lib/x86_64-linux-gnu/libnss3.so (0x00007fea0428a000)
libnssutil3.so => /usr/lib/x86_64-linux-gnu/libnssutil3.so (0x00007fea0405d000)
libplc4.so => /usr/lib/x86_64-linux-gnu/libplc4.so (0x00007fea03e57000)
libnspr4.so => /usr/lib/x86_64-linux-gnu/libnspr4.so (0x00007fea03c18000)
libicui18n.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007fea0377d000)
libicuuc.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007fea033c5000)
libicudata.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007fea019e2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fea017dd000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fea015db000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fea013d3000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fea010c2000)
/lib64/ld-linux-x86-64.so.2 (0x0000560757219000)
libplds4.so => /usr/lib/x86_64-linux-gnu/libplds4.so (0x00007fea00ebd000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fea00c4d000)
在此会发现一个问题:
libmysqlclient_r.so.16 => not found,说明没有这个依赖项,所以现在我们要解决的是这个依赖.
5.查看自己所有的 libmysqlclient.so
cc@1whispers:~$ locate libmysqlclient.so
/home/cc/Desktop/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.0
可知到我们的依赖有这些.
编译MySql驱动
1.首先要确定自己在安装Qt的时候安装了源码,如果没有安装源码,则需要重装Qt,切记装的时候记得选上源码那一项.
2.进入到源码中MySql驱动的源码中
cd /ProgramFiles/Qt5.7.0/5.7/Src/qtbase/src/plugins/sqldrivers/mysql
路径是各自安装qt的路径而来.
3. 执行编译:
qmake->make->make install
执行过程:
sudo /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
sudo make
sudo make install
在查看依赖 ldd libqsqlmysql.so ,则会看到已有依赖库.
再去运行程序,则会显示成功连接数据库.
Database connection established
这是记得要sudo make clean清理一下之前编译的中间文件.
可能出现的问题
1.执行qmake的时候: qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro这样的命令是用的默认的qmake文件,这样如果没有Qt4的SDK,则会出错,所以需要用我们自己安装Qt时路劲下的qmake.
sudo /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
需要加上sudo,不然会出现 not permit的错误,即没有权限,所以需要用root权限.
2.在执行make的时候,也需要用到root权限.不然会出现以下错误:
注: 我本人就是死在这里,因为没有用root权限,但是出现错误又不懂什么原因,迷茫了2天,终于解决.
home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/moc -DQT_NO_MTDEV -DQT_NO_LIBUDEV -DQT_NO_TSLIB -DQT_NO_LIBINPUT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I/home/cc/ProgramFiles/Qt5.7.0/5.7/Src/qtbase/mkspecs/linux-g++ -I/home/cc/ProgramFiles/Qt5.7.0/5.7/Src/qtbase/src/plugins/sqldrivers/mysql -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql/5.7.0 -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql/5.7.0/QtSql -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore/5.7.0 -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore/5.7.0/QtCore -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore -I/usr/include/c++/5 -I/usr/include/x86_64-linux-gnu/c++/5 -I/usr/include/c++/5/backward -I/usr/lib/gcc/x86_64-linux-gnu/5/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include main.cpp -o .moc/main.moc
moc: Cannot create .moc/main.moc
Makefile:652: recipe for target '.moc/main.moc' failed
make: *** [.moc/main.moc] Error 1
-lQt5Sql -lQt5Core -lpthread
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: error: ld returned 1 exit status
Makefile:114: recipe for target '../../../../plugins/sqldrivers/libqsqlmysql.so' failed
make: *** [../../../../plugins/sqldrivers/libqsqlmysql.so] Error 1
可知,我们没有了lmysqlclient_r相关的文件:即没有libmysqlclient_r.so等文件;但是在之前分析的时候,知道我们有
/home/cc/Desktop/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.0
所以要去链接出lmysqlclient_r相关的文件;
cd /usr/lib/x86_64-linux-gnu/
ln -s libmysqlclient.so.20.3.0 libmysqlclient_r.so.20.3.0
ln -s libmysqlclient.so.20 libmysqlclient_r.so.20
ln -s libmysqlclient.so libmysqlclient_r.so
这样在去执行sudo make命令,则会成功.
----------------------------------------------------------------------------------------------------------------------
注:
大家在用这个篇文章的时候若不能解决问题,也希望能在评论中给出,我们一起讨论解决.
转载请注明出处!
----------------------------------------------------------------------------------------------------------------------