2.4. Setting up TLS encryption on a MySQL server
By default, MySQL uses unencrypted connections. For secure connections, enable TLS support on the MySQL server and configure your clients to establish encrypted connections.
2.4.1. Placing the CA certificate, server certificate, and private key on the MySQL server 링크 복사링크가 클립보드에 복사되었습니다!
Before you can enable TLS encryption on the MySQL server, store the certificate authority (CA) certificate, the server certificate, and the private key on the MySQL server.
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format have been copied to the server:
-
The private key of the server:
server.example.com.key.pem -
The server certificate:
server.example.com.crt.pem -
The Certificate Authority (CA) certificate:
ca.crt.pem
For details about creating a private key and certificate signing request (CSR), as well as about requesting a certificate from a CA, see your CA’s documentation.
-
The private key of the server:
Procedure
Store the CA and server certificates in the
/etc/pki/tls/certs/directory:# mv <path>/server.example.com.crt.pem /etc/pki/tls/certs/ # mv <path>/ca.crt.pem /etc/pki/tls/certs/Set permissions on the CA and server certificate that enable the MySQL server to read the files:
# chmod 644 /etc/pki/tls/certs/server.example.com.crt.pem /etc/pki/tls/certs/ca.crt.pemBecause certificates are part of the communication before a secure connection is established, any client can retrieve them without authentication. Therefore, you do not need to set strict permissions on the CA and server certificate files.
Store the server’s private key in the
/etc/pki/tls/private/directory:# mv <path>/server.example.com.key.pem /etc/pki/tls/private/Set secure permissions on the server’s private key:
# chmod 640 /etc/pki/tls/private/server.example.com.key.pem # chgrp mysql /etc/pki/tls/private/server.example.com.key.pemIf unauthorized users have access to the private key, connections to the MySQL server are no longer secure.
Restore the SELinux context:
# restorecon -Rv /etc/pki/tls/
2.4.2. Configuring TLS encryption on a MySQL server 링크 복사링크가 클립보드에 복사되었습니다!
By default, MySQL uses unencrypted connections. For more secure connections, you can enable Transport Layer Security (TLS) support on the MySQL server and configure your clients to establish encrypted connections.
Prerequisites
- You installed the MySQL server.
-
The
mysqldservice is running. The following files in Privacy Enhanced Mail (PEM) format exist on the server and are readable by the
mysqluser:-
The private key of the server:
/etc/pki/tls/private/server.example.com.key.pem -
The server certificate:
/etc/pki/tls/certs/server.example.com.crt.pem -
The Certificate Authority (CA) certificate
/etc/pki/tls/certs/ca.crt.pem
-
The private key of the server:
- The subject distinguished name (DN) or the subject alternative name (SAN) field in the server certificate matches the server’s host name.
Procedure
Create the
/etc/my.cnf.d/mysql-server-tls.cnffile:Add the following content to configure the paths to the private key, server and CA certificate:
[mysqld] ssl_key = /etc/pki/tls/private/server.example.com.key.pem ssl_cert = /etc/pki/tls/certs/server.example.com.crt.pem ssl_ca = /etc/pki/tls/certs/ca.crt.pemIf you have a Certificate Revocation List (CRL), configure the MySQL server to use it:
ssl_crl = /etc/pki/tls/certs/example.crl.pemOptional: Reject connection attempts without encryption. To enable this feature, append:
require_secure_transport = onOptional: Set the TLS versions the server should support. For example, to support only TLS 1.3, append:
tls_version = TLSv1.3By default, the server supports TLS 1.2 and TLS 1.3.
Restart the
mysqldservice:# systemctl restart mysqld
Verification
To simplify troubleshooting, perform the following steps on the MySQL server before you configure the local client to use TLS encryption:
Verify that MySQL now has TLS encryption enabled:
# mysql -u root -p -h <MySQL_server_hostname> -e "SHOW session status LIKE 'Ssl_cipher';" +---------------+------------------------+ | Variable_name | Value | +---------------+------------------------+ | Ssl_cipher | TLS_AES_256_GCM_SHA384 | +---------------+------------------------+If you configured the MySQL server to only support specific TLS versions, display the
tls_versionvariable:# mysql -u root -p -e "SHOW GLOBAL VARIABLES LIKE 'tls_version';" +---------------+---------+ | Variable_name | Value | +---------------+---------+ | tls_version | TLSv1.3 | +---------------+---------+Verify that the server uses the correct CA certificate, server certificate, and private key files:
# mysql -u root -e "SHOW GLOBAL VARIABLES WHERE Variable_name REGEXP '{caret}ssl_ca|{caret}ssl_cert|{caret}ssl_key';" +-----------------+-------------------------------------------------+ | Variable_name | Value | +-----------------+-------------------------------------------------+ | ssl_ca | /etc/pki/tls/certs/ca.crt.pem | | ssl_capath | | | ssl_cert | /etc/pki/tls/certs/server.example.com.crt.pem | | ssl_key | /etc/pki/tls/private/server.example.com.key.pem | +-----------------+-------------------------------------------------+
2.4.3. Requiring TLS encrypted connections for specific user accounts on a MySQL server 링크 복사링크가 클립보드에 복사되었습니다!
You can configure specific MySQL user accounts to require TLS-encrypted connections to protect sensitive data transmission.
If you cannot configure on the server that a secure transport is required for all connections (require_secure_transport = on), configure individual user accounts to require TLS encryption.
Prerequisites
- The MySQL server has TLS support enabled.
- The user you configure to require secure transport exists.
- The CA certificate is stored on the client.
Procedure
Connect as an administrative user to the MySQL server:
# mysql -u root -p -h server.example.comIf your administrative user has no permissions to access the server remotely, perform the command on the MySQL server and connect to
localhost.Use the
REQUIRE SSLclause to enforce that a user must connect by using a TLS-encrypted connection:MySQL [(none)]> ALTER USER 'example'@'%' REQUIRE SSL;
Verification
Connect to the server as the
exampleuser by using TLS encryption:# mysql -u example -p -h server.example.com ... MySQL [(none)]>If no error is shown and you have access to the interactive MySQL console, the connection with TLS succeeds.
By default, the client automatically uses TLS encryption if the server provides it. Therefore, the
--ssl-ca=ca.crt.pemand--ssl-mode=VERIFY_IDENTITYoptions are not required, but improve the security because, with these options, the client verifies the identity of the server.Attempt to connect as the
exampleuser with TLS disabled:# mysql -u example -p -h server.example.com --ssl-mode=DISABLED ERROR 1045 (28000): Access denied for user 'example'@'server.example.com' (using password: YES)The server rejected the login attempt because TLS is required for this user but disabled (
--ssl-mode=DISABLED).