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

时间:2022-12-10 07:22:11

  用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. => (0x00007fff2d5fe000)
libmysqlclient.so. => /usr/lib64/mysql/libmysqlclient.so. (0x00007feb4ea24000)
libz.so. => /lib64/libz.so. (0x00007feb4e80d000)
libcrypt.so. => /lib64/libcrypt.so. (0x00007feb4e5d6000)
libnsl.so. => /lib64/libnsl.so. (0x00007feb4e3bd000)
libssl.so. => /lib64/libssl.so. (0x00007feb4e150000)
libcrypto.so. => /lib64/libcrypto.so. (0x00007feb4dd6b000)
libQt5Sql.so. => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libQt5Sql.so. (0x00007feb4db2b000)
libQt5Core.so. => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libQt5Core.so. (0x00007feb4d3e3000)
libpthread.so. => /lib64/libpthread.so. (0x00007feb4d1c7000)
libstdc++.so. => /lib64/libstdc++.so. (0x00007feb4cec0000)
libm.so. => /lib64/libm.so. (0x00007feb4cbbd000)
libgcc_s.so. => /lib64/libgcc_s.so. (0x00007feb4c9a7000)
libc.so. => /lib64/libc.so. (0x00007feb4c5e6000)
libdl.so. => /lib64/libdl.so. (0x00007feb4c3e1000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007feb4c164000)
libgssapi_krb5.so. => /lib64/libgssapi_krb5.so. (0x00007feb4bf1a000)
libkrb5.so. => /lib64/libkrb5.so. (0x00007feb4bc39000)
libcom_err.so. => /lib64/libcom_err.so. (0x00007feb4ba35000)
libk5crypto.so. => /lib64/libk5crypto.so. (0x00007feb4b800000)
libicui18n.so. => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicui18n.so. (0x00007feb4b391000)
libicuuc.so. => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicuuc.so. (0x00007feb4afe3000)
libicudata.so. => /home/eric/Qt5.5.0/5.5/gcc_64/lib/libicudata.so. (0x00007feb495b9000)
libgthread-2.0.so. => /lib64/libgthread-2.0.so. (0x00007feb493b6000)
librt.so. => /lib64/librt.so. (0x00007feb491ae000)
libglib-2.0.so. => /lib64/libglib-2.0.so. (0x00007feb48e85000)
/lib64/ld-linux-x86-.so. (0x00007feb4f221000)
libkrb5support.so. => /lib64/libkrb5support.so. (0x00007feb48c76000)
libkeyutils.so. => /lib64/libkeyutils.so. (0x00007feb48a72000)
libresolv.so. => /lib64/libresolv.so. (0x00007feb48858000)
libselinux.so. => /lib64/libselinux.so. (0x00007feb48632000)
libpcre.so. => /lib64/libpcre.so. (0x00007feb483d1000)
liblzma.so. => /lib64/liblzma.so. (0x00007feb481ab000)