Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting SSL connection error while connecting with database
Servers for Qt installer are currently down

Getting SSL connection error while connecting with database

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 442 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aditya20
    wrote on 28 Mar 2025, 04:52 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 28 Mar 2025, 05:15 last edited by
      #2

      What ssl error? Do you have the needed ssl libs in your path? How did you compile the QMysql plugin?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply 28 Mar 2025, 05:26
      0
      • C Christian Ehrlicher
        28 Mar 2025, 05:15

        What ssl error? Do you have the needed ssl libs in your path? How did you compile the QMysql plugin?

        A Offline
        A Offline
        Aditya20
        wrote on 28 Mar 2025, 05:26 last edited by
        #3

        @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_mysql

        call 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 install

        call 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"

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 28 Mar 2025, 07:38 last edited by Christian Ehrlicher
          #4

          Can you connect on the command line with these options?

          Not relevant but please use MYSQL_OPT_SSL_CA instead of SSL_CA.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply 28 Mar 2025, 12:46
          0
          • C Christian Ehrlicher
            28 Mar 2025, 07:38

            Can you connect on the command line with these options?

            Not relevant but please use MYSQL_OPT_SSL_CA instead of SSL_CA.

            A Offline
            A Offline
            Aditya20
            wrote on 28 Mar 2025, 12:46 last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 28 Mar 2025, 20:38 last edited by
              #6

              Hi,

              Which exact SSL error are you getting ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply 29 Mar 2025, 03:00
              0
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 28 Mar 2025, 23:45 last edited by Christian Ehrlicher
                #7

                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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                A 1 Reply Last reply 29 Mar 2025, 03:18
                0
                • S SGaist
                  28 Mar 2025, 20:38

                  Hi,

                  Which exact SSL error are you getting ?

                  A Offline
                  A Offline
                  Aditya20
                  wrote on 29 Mar 2025, 03:00 last edited by
                  #8

                  @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"

                  1 Reply Last reply
                  0
                  • C Christian Ehrlicher
                    28 Mar 2025, 23:45

                    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.

                    A Offline
                    A Offline
                    Aditya20
                    wrote on 29 Mar 2025, 03:18 last edited by
                    #9

                    @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 Reply Last reply
                    1
                    • A Aditya20 has marked this topic as solved on 29 Mar 2025, 07:59

                    4/9

                    28 Mar 2025, 07:38

                    • Login

                    • Login or register to search.
                    4 out of 9
                    • First post
                      4/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved