Compiling qt program on Centos lacks mysql driver solution

I wrote a client-to-server communication before, and it has been running on windows. Today I want to hang the server on the cloud server to test and test. A mysql driver worked on me for a day. I found a lot of information and finally gave it to Solved it, record it, I hope it can help others behind.

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

Prompt that the driver cannot be loaded

I think you should know that it is the problem of libqsqlmysql.so. Regarding the information about compiling mysql driver, every final solution focuses on the file libqsqlmysql.so.

If you really clicked here for the first article, then this article may help you solve the problem.

No matter what we want to download, what we want to set up, the ultimate essence is that we want to get the file libqsqlmysql.so

So witty, I went to browse the resources of csdn, and I didn't expect it.

Insert picture description here


Isn't this the ready-made libqsqlmysql.so file? Just copy it directly.

Yes, the previous error that prompted that the database driver could not be loaded has disappeared, but don't be happy, the following error follows

Cannot mix incompatible Qt library (version 0x50xxx) with this library (version 0x50xxx).

So I started to solve this error again. After some tossing, I gave up. I knew this was caused by the wrong version, but it couldn’t be solved. I decided to delete this libqsqlmysql.so and generate a unified version based on the information on the Internet. libqsqlmysql.so.

The libqsqlmysql.so downloaded here is okay, it's just a version issue. If your qt version is the same as the version in the resource description, I think you can try it.

Let's look at the compilation of Zhengerbajing to generate this file.

The first situation on the Internet is that you have not installed qt. At this time, when you install qt, you can choose to install the source code. I think this method is the most ridiculous. I always start to compile the program to package this error. Do I reinstall qt again~~~~

The second is to download the source code from the official qt, I think this method is reliable.

This is the URL http://download.qt.io/archive/qt/ , find the source package corresponding to your qt version.

For example, I am 5.1.0, then click

Insert picture description here


Insert picture description here

Then install a mysql development package
yum install mysql-devel

After downloading the source code, remember to unzip, the command is as follows

tar -zxvf qt-everywhere-opensource-src-5.1.0.tar.gz

The decompressed folder and the compressed package are in the same place,

Insert picture description here

It contains four original files.

Insert picture description here


What we have to do is to compile this file. It should be familiar to see files with pro suffix.

How to compile it, execute qmake

If qmake has configured the environment variables, you can use the qmake command to compile the pro file. If it prompts that qmake is an invalid command, you can use the following path to compile, or under this folder directory, find the directory where you installed qt, such as My /opt/Qt5.1.0/5.1/gcc_64/bin, in this directory you can find qmake, use this path to compile pro.
The command is as follows:
/opt/Qt5.1.0/5.1/gcc_64/bin/qmake

After execution, a Makefile file will be generated

Then execute make

If it is successful, you can see where the libqsqlmysql.so file is in the final output line. For example,

Insert picture description here


if you are not familiar with this path, you can use pwd to view the path.

Insert picture description here


Another case is that make is unsuccessful, indicating that lmysqlclient_r cannot be found.

Use the find command
find /usr -name'*mysqlclient*'

Insert picture description here

You can see that there is this file, but I can’t find it. Let’s make a soft link

sudo ln -s /usr/lib64/mysql/libmysqlclient_r.so /usr/lib/libmysqlclient_r.so

After we execute make, there is no problem.

In fact, you can see that this file also points to libmysqlclient.so

Insert picture description here

Then find the path corresponding to qt

Insert picture description here

Replace the libqslqmysql.so that comes with qt with the newly generated libqsqlmysql.so to connect to the database normally.