How to solve " Driver not loaded " error when connecting MySQL / MariaDB from a Qt application ?
MariaDB database started as a fork of MySQL database, So most frameworks still uses MySQL connectors to connect to MariaDB.
There are many reasons for this problem and they are the following.
- You are using MariaDB 10 or later.
- You need to add MySQL / MariaDB lib path to the path environment variables.
- Lake of QMYSQL driver or QMYSQL is found but incompatible with other libraries used in the application (The same driver is used for both MySQL and MariaDB database servers).
1- You are using MariaDB 10 or later.
Starting from MariaDB 10 there is no libmysql.dll library in the lib folder of MariaDB, libmariadb.dll library is a replacement for libmysql.dll .
- To solve this simply copy libmariadb.dll and rename the copy to libmysql.dll (do not rename the original libmariadb.dll this may prevent you from connecting to MariaDB using another application).
2- You need to add MySQL / MariaDB lib path to the path environment variables.
3- Lake of QMYSQL driver or QMYSQL is found but incompatible with other libraries used in the application (The same driver is used for both MySQL and MariaDB database servers).
In this case you need to build the driver to connect to MySQL / MariaDB.
Here is the instructions to build QMySQL Driver on Gnu / Linux OS (Redhat / Centos / Fedora) .
I have Fedora 28, MariaDB-10.2.16 and Qt 5.9.6
After searching for long time I didn't find any solutions, I decided to build the qt mysql driver although there was pre-built driver shipped with Qt 5 .
It works after replacing the pre-built Qt MySQL driver with the one I've built.
Instructions to build the mysql driver on Fedora / Redhat / Centos.
For me I've Qt installed on the following path /opt/Qt5.9.6 so don't forget to change the paths according to your installation.
Also make sure that you have the package mariadb-devel or mysql-devel (this package includes header files used to compile the driver) installed according to the database server you have installed on your system, If they are not installed you can install it by the following command as super user
dnf install mariadb-devel
Building the Qt MySQL driver
cd /opt/Qt5.9.6/5.9.6/Src/qtbase/src/plugins/sqldrivers/ /opt/Qt5.9.6/5.9.6/gcc_64/bin/qmake -- MYSQL_PREFIX=/usr/include/mysql make sub-mysql
After building the driver back the Qt pre-built libqsqlmysql.so up by the following command you may need it later.
mv /opt/Qt5.9.6/5.9.6/gcc_64/plugins/sqldrivers/libqsqlmysql.so /opt/Qt5.9.6/5.9.6/gcc_64/plugins/sqldrivers/libqsqlmysql.so.old
And the final step is copy our mysql driver to Qt plugin folder by the following command.
cp /opt/Qt5.9.6/5.9.6/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/libqsqlmysql.so /opt/Qt5.9.6/5.9.6/gcc_64/plugins/sqldrivers/
Now run your application and connect to mariadb (MySql).
Also I've the same issue on windows I'll try to build the driver on windows and I'll add the instructions later.