Enable SSL for Secure Connections
In this step, you will enable SSL (Secure Sockets Layer) for secure connections to your MySQL server. SSL encrypts the data transmitted between the client and the server, protecting it from eavesdropping and tampering.
First, you need to generate the SSL certificates and keys. You will use the openssl
command-line tool for this.
Open your terminal in the LabEx VM. The default directory is ~/project
.
Navigate to the ssl
directory that was created during setup:
cd ssl
Now, generate the server key:
sudo openssl genrsa 2048 > server-key.pem
Next, generate the certificate signing request (CSR):
sudo openssl req -new -key server-key.pem -out server-req.pem
You will be prompted for information such as country name, state, city, organization name, etc. You can enter appropriate values or leave them blank. For the "Common Name", enter localhost
or the server's hostname/IP address.
Now, sign the certificate request to create the SSL certificate:
sudo openssl x509 -days 365 -in server-req.pem -signkey server-key.pem -out server-cert.pem
This command creates a self-signed certificate valid for 365 days.
Next, generate the client key:
sudo openssl genrsa 2048 > client-key.pem
Generate the client certificate signing request (CSR):
sudo openssl req -new -key client-key.pem -out client-req.pem
You will be prompted for information similar to the server CSR.
Now, sign the client certificate request:
sudo openssl x509 -days 365 -in client-req.pem -signkey client-key.pem -out client-cert.pem
Finally, generate the CA (Certificate Authority) key and certificate:
sudo openssl genrsa 2048 > ca-key.pem
sudo openssl req -new -x509 -days 365 -key ca-key.pem -out ca-cert.pem
Now you have the necessary SSL files in the ~/project/ssl
directory.
Next, you need to configure MySQL to use these SSL certificates. You will edit the my.cnf
file located in the ~/project
directory.
Navigate back to the ~/project
directory:
cd ~/project
Edit the my.cnf
file using nano
:
nano my.cnf
Add the following lines under the [mysqld]
section in the my.cnf
file:
ssl-cert=/var/lib/mysql/ssl/server-cert.pem
ssl-key=/var/lib/mysql/ssl/server-key.pem
ssl-ca=/var/lib/mysql/ssl/ca-cert.pem
#require_secure_transport=ON
Save the file and exit nano
(Press Ctrl + X
, then Y
, then Enter
).
Important: The paths in the my.cnf
file are relative to the MySQL container's filesystem. The setup script has already created the my.cnf
file and the ssl
directory in ~/project
. In a real-world scenario, you would need to copy the generated certificate files to the location specified in my.cnf
inside the MySQL server's data directory and restart the MySQL server. Due to the limitations of the LabEx environment, we will skip copying the files and restarting the server.
Now, let's connect to the MySQL server using SSL. You will need to provide the paths to the CA certificate, client certificate, and client key.
mysql --ssl-ca=~/project/ssl/ca-cert.pem --ssl-cert=~/project/ssl/client-cert.pem --ssl-key=~/project/ssl/client-key.pem -u root -p
Enter the root password you set in the previous step when prompted.
To verify that the connection is using SSL, execute the following SQL command in the MySQL shell:
SHOW STATUS LIKE 'Ssl_cipher';
If the Value
column shows a cipher, then the connection is using SSL.
+---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| Ssl_cipher | TLS_AES_256_GCM_SHA384 |
+---------------+-----------------------+
1 row in set (0.00 sec)
If the Value
column is empty, then the connection is not using SSL.
Exit the MySQL shell by typing:
exit