简体   繁体   中英

Qmysql driver not loaded but it's available

I want to connect my database(mysql) with Qt library on ubuntu 16.04.
But i faced this error:

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

Result of ldd /Qt-PATH/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/libqsqlmysql.so is:

linux-vdso.so.1 =>  (0x00007fff9d55a000)
    libmysqlclient_r.so.16 => not found
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007efc887eb000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007efc885b3000)
    libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007efc8839a000)
    libssl.so.10 => not found
    libcrypto.so.10 => not found
    libQt5Sql.so.5 => /home/hassan-setayesh/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007efc88154000)
    libQt5Core.so.5 => /home/hassan-setayesh/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007efc87a38000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007efc8781b000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007efc87499000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007efc8718f000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007efc86f79000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007efc86bb0000)
    libicui18n.so.56 => /home/hassan-setayesh/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007efc86715000)
    libicuuc.so.56 => /home/hassan-setayesh/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007efc8635d000)
    libicudata.so.56 => /home/hassan-setayesh/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007efc8497a000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007efc84775000)
    libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007efc84573000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007efc8436b000)
    libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007efc84059000)
    /lib64/ld-linux-x86-64.so.2 (0x0000564692881000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007efc83de9000)

And for solving this : libmysqlclient_r.so.16 => not found I link it to libmysqlclient.so.20.3.2 with these command:
cd /usr/lib/x86_64-linux-gnu/ sudo ln -s libmysqlclient.so.20.3.2 libmysqlclient_r.so.16

My Qt version is 5.7 offline mode.

what should i do?

Repeat 10 times:

  1. I will never use a symlink to solve a missing library problem due to a different soname
  2. I will never use a symlink to solve a missing library problem due to a different soname
  3. I will never use a symlink to solve a missing library problem due to a different soname
  4. I will never use a symlink to solve a missing library problem due to a different soname
  5. I will never use a symlink to solve a missing library problem due to a different soname
  6. I will never use a symlink to solve a missing library problem due to a different soname
  7. I will never use a symlink to solve a missing library problem due to a different soname
  8. I will never use a symlink to solve a missing library problem due to a different soname
  9. I will never use a symlink to solve a missing library problem due to a different soname
  10. I will never use a symlink to solve a missing library problem due to a different soname

Never ever solve that kind of problems via symlinking . If your system does not provide the exact soname required by a library or an executable, you'll need to recompile that library or executable. There is a reason why libraries have the soname version number in their file names, and a mismatching soname will result in a not found for the dynamic linker/loader. You're just breaking that process and your entire system by inserting a broken soname for a library.

So, first thing to do: get rid of the symlink you introduced. Go in /usr/lib/x86_64-linux-gnu/ and remove it. Do it now .


Then, how to recompile the plugin so that it works on Ubuntu?

(Or, actually, anywhere. Even Windows or Mac. Just adapt the instructions)

Step by step:

  1. Install the mysql development packages. On Ubuntu it should be the libmysqlclient-dev package, but double check in case the name changed on your particular Ubuntu edition. Go on https://packages.ubuntu.com and use the file-based search to look for mysql.h .
  2. Run the maintanance tool from the installer, and ask it to also install the Qt source components. You'll find the tool in your Qt installation directory.
  3. Go under INSTALL_DIR/Src/5.7/qtbase/src/plugins/sqldrivers/mysql (adjust INSTALL_DIR and 5.7 to your actual case).
  4. Run the right qmake . The right one is the one coming from the same installation of Qt, and whose version matches the sources. In your case it's likely to be in INSTALL_DIR/5.7/gcc_64/bin/qmake .
  5. Run make . If it fails to compile due to some library not found, install the required packages on your system. The Ubuntu package search linked above may be useful.
  6. Once make runs successfully, it will create a brand new libqsqlmysql.so . It should automatically overwrite the one in INSTALL_DIR/5.7/gcc_64/plugins/sqldrivers . If for any reason it's not automatically overwritten, move it manually there.

Done! Enjoy your MySQL database connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM