转载

CentOS7 (64位) 下QT5.5 连接MySQL数据库(driver not loaded)

用qt连接MySQL需要共享库 libqsqlmysql.so的驱动,路径在plugin/sqldrivers目录下,乍看已经可用了,其实不然。

用ldd命令分析一下,libmysqlclient_r.so.16 => not found ,后明显libmysqlclient_r.so.16 缺少另一个共享库,他们是依赖关系,解决了这个问题qt才能真的连接到mysql数据库。而我的/usr/lib64/下的共享库版本比较高是libmysqlclient.so.18,想到了一个比较投机的想法ln创建符号链接,然而这并没有什么卵用。

让我哭笑不得是QT5.5是我从官网下载的,居然依赖的是低版本,你们编译的时候能不能不偷懒啊,估计他们直接从低版本拷贝的libqsqlmysql.so,没办法了吗?显然不是,打开qt官网下载源码,自己编译。下载过程略过,非常好找 http://download.qt.io/official_releases/qt/5.4/5.4.2/submodules/ (暂时没有qt5.5的源码),找到qtbase那个大概60M。MySql驱动的路径为qtbase/src/plugins/sqldrivers/mysql/。

  • qmake
  • make

然而这还没有完,make的过程中又找不到动态库了:

/usr/bin/ld: cannot find -lz /usr/bin/ld: cannot find -lssl /usr/bin/ld: cannot find -lcrypto

第一个是没有安装zlib-devel,直接yum install zlib-devel搞定。

第二第三项找不到是没有道理的,解决的方法:/usr/lib64/中创建符号链接

ln -s libssl.so.1.0.1e libssl.so  ln -s libcrypto.so.1.0.1e libcrypto.so

再次make,成功。当然这还没有完,用新的libqsqlmysql.so去替换旧的,这就全部完成了。

再次查看:

[sqldrivers]ldd libqsqlmysql.so      linux-vdso.so.1 =>  (0x00007fff2d5fe000)     libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007feb4ea24000)     libz.so.1 => /lib64/libz.so.1 (0x00007feb4e80d000)     libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007feb4e5d6000)     libnsl.so.1 => /lib64/libnsl.so.1 (0x00007feb4e3bd000)     libssl.so.10 => /lib64/libssl.so.10 (0x00007feb4e150000)     libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007feb4dd6b000)     libQt5Sql.so.5 => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libQt5Sql.so.5 (0x00007feb4db2b000)     libQt5Core.so.5 => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libQt5Core.so.5 (0x00007feb4d3e3000)     libpthread.so.0 => /lib64/libpthread.so.0 (0x00007feb4d1c7000)     libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007feb4cec0000)     libm.so.6 => /lib64/libm.so.6 (0x00007feb4cbbd000)     libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007feb4c9a7000)     libc.so.6 => /lib64/libc.so.6 (0x00007feb4c5e6000)     libdl.so.2 => /lib64/libdl.so.2 (0x00007feb4c3e1000)     libfreebl3.so => /lib64/libfreebl3.so (0x00007feb4c164000)     libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007feb4bf1a000)     libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007feb4bc39000)     libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007feb4ba35000)     libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007feb4b800000)     libicui18n.so.54 => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicui18n.so.54 (0x00007feb4b391000)     libicuuc.so.54 => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicuuc.so.54 (0x00007feb4afe3000)     libicudata.so.54 => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicudata.so.54 (0x00007feb495b9000)     libgthread-2.0.so.0 => /lib64/libgthread-2.0.so.0 (0x00007feb493b6000)     librt.so.1 => /lib64/librt.so.1 (0x00007feb491ae000)     libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007feb48e85000)     /lib64/ld-linux-x86-64.so.2 (0x00007feb4f221000)     libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007feb48c76000)     libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007feb48a72000)     libresolv.so.2 => /lib64/libresolv.so.2 (0x00007feb48858000)     libselinux.so.1 => /lib64/libselinux.so.1 (0x00007feb48632000)     libpcre.so.1 => /lib64/libpcre.so.1 (0x00007feb483d1000)     liblzma.so.5 => /lib64/liblzma.so.5 (0x00007feb481ab000)
正文到此结束
Loading...