This section provides an in-depth guide on how to secure communication between a MySQL server and a client by using SSL (Secure Socket Layer) certificates. This ensures that data transmitted between the client and server is encrypted, protecting sensitive information from potential eavesdropping and man-in-the-middle attacks.
By default, MySQL sends traffic in plaintext, which can be intercepted easily. In this tutorial, we will show you how to enable SSL encryption, verify secure connections, and configure your PHP application to use SSL for secure communication with the MySQL database.
To enable SSL encryption on your MySQL server, we first need to configure MySQL to generate and use SSL certificates.
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following lines under the [mysqld] section to specify the locations of the SSL certificates:ssl-ca = /var/lib/mysql/ca.pem
ssl-cert = /var/lib/mysql/server-cert.pem
ssl-key = /var/lib/mysql/server-key.pem
These entries point MySQL to the generated certificates, which include the Certificate Authority (CA) certificate, the server’s certificate, and the server’s private key.
sudo systemctl restart mysql
SHOW VARIABLES LIKE '%ssl%';

To enable the client to communicate securely with the MySQL server, we need to transfer the server’s CA certificate to the client. This certificate is used to verify the authenticity of the MySQL server during the connection handshake.
sudo mkdir -p /etc/mysql/ssl
Then, copy the ca.pem file from the MySQL server to the client machine using scp (secure copy):
sudo cp /var/lib/mysql/ca.pem /home/sibou/ca.pem
sudo scp sibou@192.168.85.139:/home/sibou/ca.pem /etc/mysql/ssl/
Replace 192.168.85.139 with the actual IP address or hostname of your MySQL server.sudo chmod 644 /etc/mysql/ssl/ca.pem
This ensures that the client machine can read the certificate file when establishing the secure connection.Now that SSL is enabled on both the server and the client, we need to configure the MySQL client to use SSL when connecting to the server.
mysql -h mysql_server_ip -u ssl_user -p --ssl-ca=/etc/mysql/ssl/ca.pem --ssl-mode=REQUIRED
Replace mysql_server_ip with your MySQL server’s IP address and ssl_user with the MySQL username.\s
This will show detailed information about the current session, including SSL status. You should see a line similar to the following:
SSL: Cipher in use
This confirms that the connection is encrypted using SSL.
In your PHP application, you need to configure the MySQL connection to use SSL for secure communication. To do this, update your db.php (or equivalent) database connection file.
?php
session_start();
$host = "192.168.85.139"; // Remote MySQL server IP
$user = "todo_user"; // Remote MySQL user
$password = "password"; // User password
$dbname = "todo_db"; // Database name
// Path to SSL CA certificate (copied from MySQL server)
$ssl_ca = "/etc/mysql/ssl/ca.pem";
try {
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
// Add SSL configuration to options
$options = [
PDO::MYSQL_ATTR_SSL_CA => $ssl_ca,
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false, // Disable for self-signed certs
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO($dsn, $user, $password, $options);
} catch(PDOException $e) {
die("Database Connection Failed: " . $e->getMessage());
}
?>