Connecting to MySQL Through SSL in Delphi
Security is very important when sending messages from the server to the client and vice versa. There are many data protection methods, including the use of SSL encryption to connect to a remote MySQL server from a Delphi application. MySQL supports data transfer via the TCP/IP protocol stack both using SSL encryption or without it.
Devart offers a solution called SecureBridge, which allows you to embed an SSL client into a Delphi or C++ Builder application to establish a secure connection to MySQL server. This tutorial demonstrates how to create a sample Delphi application that connects to MySQL using SSL as the encryption method.
Before connecting to MySQL via SSL, create SSL certificates as explained in the MySQL documentation and configure SSL parameters in the my.ini file.
Sample Delphi App That Connects to MySQL Using SSL
To create an SSL connection to MySQL, set the T:Devart.MyDac.TMyConnectionOptions.Protocol. property in Options to mpSSL. It forces the application to only connect through SSL - if a connection attempt fails, an exeption is raised.
MyConnection.Options.Protocol := mpSSL;
After installing MyDAC and SecureBridge in your system, install the TMySSLIOHandler component in RAD Studio to bind MyDAC with SecureBridge. The installation instructions are provided in the Readme.html, which is located by default in "My Documents\Devart\MyDAC for RAD Studio\Demos\TechnologySpecific\SecureBridge\DelphiXX".
- Run RAD Studio and select 'File -> New – > VCL Forms Application – Delphi'.
- Place the TMySSLIOHandler component, which allows MyDAC to connect to MySQL server through SSL, onto the form. Also add the TMyConnection, TMyQuery, TDataSource, TDBGrid, and TButton components to the form - they are required to create a sample application that connects to the MySQL server via SSL, runs a selection operation against the database, and displays the obtained rows in a data grid.
- Select the TMyConnection component and assign the TMySSLIOHandler object to the IOHandler property in the Object Inspector.
- In the Object Inspector, expand Options and set the Protocol property to mpSSL.
- Then expand SSLOptions and specify the server certificate in the CACert property, the client certificate in the Cert property, and the private client key in the Key property.
- Double-click TMyConnection and specify the server address, port, username, password, and, optionally, database name. Click Connect to test connection to the MySQL server.
- Select the TDataSource component and assign the MyQuery1 object to the DataSet property.
- Assign the DataSource1 object to the DataSource property in the TDBGrid component.
- Double-click the TMyQuery component and add a SQL query that will be run against the MySQL database.
- Select the TButton component and create the OnClick event. Add the code that will call the Open method in the TMyQuery component when you click the button.
- Press F9 to compile and run the application. Click the button on the form to execute the query and display data in the grid.
2. SSL Connection to MySQL in Delphi Using the OpenSSL Library
Another way to embed SSL client functionality into your Delphi app, which uses MyDAC components to access MySQL, is by using the OpenSSL library that implements the SSL protocol and enables servers to securely communicate with their clients. The description of the SSL connection features without the SecureBridge IOHandler usage:
The following options must be set for SSL connection:
- SSLCACert - the pathname to the certificate authority file.
- SSLCert - the pathname to the certificate file.
- SSLKey - the pathname to the key file.
- SSLCipherList - a list of allowable ciphers to use for SSL encryption.
Note:The ssleay32.dll and libeay32.dll files are required to use the SSL protocol with the OpenSSL library.