SSL and TLS with PurePerl MySQL API
Using the PurePerl MySQL API – Part 13
Foreword: In this part of the series, you learn how to use SSL/TLS for the PurePerl MySQL API client.
By: Date Published: 28 Aug 2017
Introduction
The web scheme for SSL operation is HTTPS and not HTTP, as in https://www.site.com .
To use SSL with PurePerl MySQL API client, you need a file called the Certification Authority file. You may also need two other files which are called, the certificate file and the key file. Search this site (or the web) for the meaning and how to obtain these three files.
You should use SSL with MySQL when the API client and the MySQL server are in different computers (in a network).
Syntax
The PurePerl MySQL API client indicates to the MySQL server that it wants SSL communication, when it connects to the MySQL server. The syntax is:
Mysql::connect("root", "secret", "localhost", 3306, undef, 'ssl', 'path/to/certification-authority/file', [, 'path/to/certificate/file', 'path/to/key/file'])
The last two files in the argument list are optional, depending on the setup. The fifth argument is undef instead of 'compress'. SSL also does compression, so if the fifth argument is 'compress', there will be an unnecessary second compression of data.
The following code should read rows from a table in a MySQL database, or inform you that the MySQL server does not support SSL:
#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;
if (!Mysql::connect("root", "secret", "localhost", 3306, undef, 'ssl', '/etc/ssl/ca.pem', 'cert.pem', 'key.pem'))
{
print "$Mysql::Error_msg";
}
else
{
if (Mysql::query("USE PetStore") != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
my $sel = "select * from pet";
if (Mysql::query($sel) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
for (my $i=0; $i<$Mysql::No_of_Rows; ++$i)
{
print $Mysql::Result[$i]{'name'}, ', ';
print $Mysql::Result[$i]{'owner'}, ', ';
print $Mysql::Result[$i]{'species'}, ', ';
print $Mysql::Result[$i]{'sex'}, ', ';
print $Mysql::Result[$i]{'birth'}, ', ';
print $Mysql::Result[$i]{'death'}, ', ';
print "\n";
}
}
}
}
Mysql::close();
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT