用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.soln -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)