how to set cert for QSqlDatabase connection with SSL?
Hello,
I am trying to connect to MySQL db over SSL, but I get an error: “SSL connection error QMYSQL: Unable to connect”
I followed procedure from here [dev.mysql.com] (example 1) and set up my.cnf, server-side appearrs to work ok:
- mysql> show variables like '%ssl%';
- +---------------+-----------------------------+
- | Variable_name | Value |
- +---------------+-----------------------------+
- | have_openssl | YES |
- | have_ssl | YES |
- | ssl_ca | /root/certs/ca-cert.pem |
- | ssl_capath | |
- | ssl_cert | /root/certs/server-cert.pem |
- | ssl_cipher | |
- | ssl_key | /root/certs/server-key.pem |
- +---------------+-----------------------------+
I have “QT += core gui sql ssl network” in .pro and connect with following code:
- db.setHostName(iHostLineEdit->text());
- db.setDatabaseName("asterisk");
- db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
- db.setUserName(iUserLineEdit->text());
- db.setPassword(iPassLineEdit->text());
- bool ok = db.open();
My guess (based on this [dev.mysql.com] ) is that I have to set CA-cert.pem as certification authority cert. But how do I do that without any QSslSocket instance?
EDIT
in case: I work on windows with mingw; mysql connection without ssl works.
2 replies
According to the MySQL docs, the mysql_ssl_set() function needs to be called before opening the connection.
I suggest that you create a feature request at bugreports.qt.nokia.com stating your problem and requesting additional connection options for setting SSL certificates.
As a workaround, you have to call mysql_ssl_set() manually before calling QSqlDatabase::open(). QSqlDriver::handle() returns a QVariant containing the MYSQL* connection pointer. Pseudo code:
- mysql_ssl_set(qvariant_cast<MYSQL *>(mySqlDatabase->driver->handle()), "...", ...
You must log in to post a reply. Not a member yet? Register here!


