Connecting via SSL

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 TCRSSLIOHandler 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".

  1. Run RAD Studio and select 'File -> New – > VCL Forms Application – Delphi'.
  2. Place the TCRSSLIOHandler 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.
  3. SSL connection to MySQL in Delphi

  4. Select the TMyConnection component and asign the TCRSSLIOHandler object to the IOHandler property in the Object Inspector.
  5. SSL IOHandler in Delphi

  6. Expand Options and set the Protocol property to mpSSL.
  7. Expand SSLOptions in the Object Inspector 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.
  8. Double-click TMyConnection and specify the server address, port, username, password, and, optionally, database name. Click Connect to test connection to the MySQL server.
  9. Connection to MySQL in Delphi

  10. Select the TDataSource component and assign the MyQuery1 object to the DataSet property.
  11. SSL DataSource

  12. Assign the DataSource1 object to the DataSource property in the TDBGrid component.
  13. SSL TDBGrid

  14. Double-click the TMyQuery component and add a SQL query that will be run against the MySQL database.
  15. SSL SQL Query

  16. 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.
  17. SSL TButton

  18. Press F9 to compile and run the application. Click the button on the form to execute the query and display data in the grid.
  19. SSL Execute the query

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:

Note:The ssleay32.dll and libeay32.dll files are required to use the SSL protocol with the OpenSSL library.

© 1997-2022 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback