Qt5.7不能加载MySql驱动问题.(需要重新编译驱动)

转自:http://blog.csdn.net/qq_28851503/article/details/52422302


首先贴上我遇到的问题,如下:

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


分析

1.首先确定自己是否安好装了MySql(没有安装则先安装)
安装方法:(该链接里面同时也包含卸载方法)

其次执行以下命令,看是否已安装以下选项:
sudo apt-get install libmysqlclient-dev

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 

显示如下:

[plain] view plain copy
 print?
  1. linux-vdso.so.1 =>  (0x00007ffcb64e2000)  
  2. libmysqlclient_r.so.16 => not found  
  3. libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fea06463000)  
  4. libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fea0622a000)  
  5. libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fea06011000)  
  6. libssl.so.10 => /usr/lib/x86_64-linux-gnu/libssl.so.10 (0x00007fea05dba000)  
  7. libQt5Sql.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007fea05b74000)  
  8. libQt5Core.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007fea05459000)  
  9. libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fea0523c000)  
  10. libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fea04eb9000)  
  11. libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea04bb0000)  
  12. libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fea0499a000)  
  13. libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea045d0000)  
  14. libnss3.so => /usr/lib/x86_64-linux-gnu/libnss3.so (0x00007fea0428a000)  
  15. libnssutil3.so => /usr/lib/x86_64-linux-gnu/libnssutil3.so (0x00007fea0405d000)  
  16. libplc4.so => /usr/lib/x86_64-linux-gnu/libplc4.so (0x00007fea03e57000)  
  17. libnspr4.so => /usr/lib/x86_64-linux-gnu/libnspr4.so (0x00007fea03c18000)  
  18. libicui18n.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007fea0377d000)  
  19. libicuuc.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007fea033c5000)  
  20. libicudata.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007fea019e2000)  
  21. libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fea017dd000)  
  22. libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fea015db000)  
  23. librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fea013d3000)  
  24. libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fea010c2000)  
  25. /lib64/ld-linux-x86-64.so.2 (0x0000560757219000)  
  26. libplds4.so => /usr/lib/x86_64-linux-gnu/libplds4.so (0x00007fea00ebd000)  
  27. 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


3. 使用证确方式(sudo make)执行后肯能会出现以下问题:

[plain] view plain copy
 print?
  1. -lQt5Sql -lQt5Core -lpthread    
  2. /usr/bin/ld: cannot find -lmysqlclient_r  
  3. collect2: error: ld returned 1 exit status  
  4. Makefile:114: recipe for target '../../../../plugins/sqldrivers/libqsqlmysql.so' failed  
  5. 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相关的文件;

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">cd </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">usr</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">lib</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">x86_64</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">linux</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">gnu</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">
ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20.3</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;"><span style="color: rgb(125, 39, 39);">0</span></span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20.3</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;"><span style="color: rgb(125, 39, 39);">0</span></span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">
ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">
ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span></code>

这样在去执行sudo make命令,则会成功.

----------------------------------------------------------------------------------------------------------------------

注:

大家在用这个篇文章的时候若不能解决问题,也希望能在评论中给出,我们一起讨论解决.

转载请注明出处!

----------------------------------------------------------------------------------------------------------------------



posted @ 2016-10-18 12:52  隔壁王叔叔a  阅读(560)  评论(0编辑  收藏  举报