Getting SSL connection error while connecting with database
-
I am trying to establish a connection with mysql database hosted on aiven platform, but is is failing due to SSL connection error.
I am adding my connection program below.
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
#include <QtSql/QSqlDriver>void connectToMySQL() {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("hostname");
db.setPort(portnumber);
db.setDatabaseName("dbname");
db.setUserName("username");
db.setPassword("password");db.setConnectOptions("SSL_CA=C:/Users/aditya/Downloads/ca.pem;MYSQL_OPT_SSL_MODE=REQUIRED"); qDebug() << "Driver is valid:" << db.isValid(); qDebug() << "Driver supports transactions:" << db.driver()->hasFeature(QSqlDriver::Transactions); if (!db.open()) { qDebug() << "Error: Failed to connect to database!"; qDebug() << db.lastError().text(); return; } qDebug() << "Connected to MySQL database successfully!"; // Example: Execute a simple query QSqlQuery query; if (query.exec("SELECT VERSION()")) { while (query.next()) { qDebug() << "MySQL Version:" << query.value(0).toString(); } } else { qDebug() << "Query error:" << query.lastError().text(); } db.close();
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);connectToMySQL(); return a.exec();
}
Kindly help me out with this issue.
-
What ssl error? Do you have the needed ssl libs in your path? How did you compile the QMysql plugin?
-
@Christian-Ehrlicher said in Getting SSL connection error while connecting with database:
ed ssl libs in your path? How did you compile the QMysql plugin
Below is the ssl error
Error: Failed to connect to database!
"SSL connection error: unknown error number QMYSQL: Unable to connect"Below build steps I used to build QMysql plugin
cd "C:\Qt\6.6.3\Src\qtbase\src\plugins\sqldrivers"
mkdir build_mysql
cd build_mysqlcall C:\Qt\6.6.3\mingw_64\bin\qt-cmake.bat -G "MinGW Makefiles" .. ^
-DMySQL_INCLUDE_DIR="C:\Program Files\MySQL\MySQL Server 8.0\include" ^
-DMySQL_LIBRARY="C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib" ^
-DCMAKE_INSTALL_PREFIX="C:\Qt\6.6.3\mingw_64"cd ..
mingw32-make
mingw32-make installcall C:\Qt\6.6.3\msvc2019_64\bin\qt-cmake.bat -G "MinGW Makefiles" . -DMySQL_INCLUDE_DIR="C:\Program Files\MySQL\MySQL Server 8.0\include" -DMySQL_LIBRARY="C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib" -DCMAKE_INSTALL_PREFIX="C:\Qt\6.6.3\msvc2019_64" -DCMAKE_C_COMPILER="gcc.exe" -DCMAKE_CXX_COMPILER="g++.exe"
qmake -- MYSQL_INCDIR="C:\Program Files\MySQL\MySQL Server 8.0\include" MYSQL_LIBDIR="C:\Program Files\MySQL\MySQL Server 8.0\lib"
-
Can you connect on the command line with these options?
Not relevant but please use MYSQL_OPT_SSL_CA instead of SSL_CA.
-
@Christian-Ehrlicher Command line is not a problem. I am able to connect with the connection details in mysql workbech and everything is working fine. But when establishing a connection through qt, it is causing the error. QT is not allowing to connect and failing with ssl connection error. Same issue is with MySQL installed on my machine. But for that I modified my.ini and added skip_ssl to bypass the ssl connection.
-
Hi,
Which exact SSL error are you getting ?
-
This works fine for me
MYSQL_OPT_SSL_CA=D:/mysqlca/ca-cert.pem;MYSQL_OPT_SSL_MODE=SSL_MODE_VERIFY_CA
But you have to make sure the correct libmysql.dll is loaded - I accidentally had an old from MySQL Connector C 6.1 in my PATH which was picked up... took me 2 hours to fiddle out what went wrong.
Tested with Qt6.10 but there were no changes wrt this since 6.6 iirc.
-
@SGaist said in Getting SSL connection error while connecting with database:
Which exact SSL error are you getting ?
"SSL connection error: unknown error number QMYSQL: Unable to connect"
-
@Christian-Ehrlicher Correct same is the case with me. For me also MySQL Connector C 6.1 lib path was present in PATH env variable. I changed it to MySql server lib and it is working now. Thank you
-
1/9