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

Prompt that the driver cannot be loaded

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

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

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 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 and generate a unified version based on the information on the Internet.

The 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 , 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:

After execution, a Makefile file will be generated

Then execute make

If it is successful, you can see where the 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/ /usr/lib/

After we execute make, there is no problem.

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

Insert picture description here

Then find the path corresponding to qt

Insert picture description here

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