FreeBSD 14
Sponsored Link

MySQL 8.0 : SSL/TLS の設定2024/09/03

 

MySQL で SSL/TLS による暗号化通信の設定を有効にします。

[1] サーバー側は、デフォルトで自己署名の証明書が自動生成されて設定が適用されているため、特別な要件がない限りは、設定変更せずとも TLS 接続可能となっています。
# mysqld が起動時に生成

root@dlp:~ #
ls -l /var/db/mysql/*.pem

-rw-------  1 mysql mysql uarch 1705 Sep  3 12:36 /var/db/mysql/ca-key.pem
-rw-r--r--  1 mysql mysql uarch 1112 Sep  3 12:36 /var/db/mysql/ca.pem
-rw-r--r--  1 mysql mysql uarch 1112 Sep  3 12:36 /var/db/mysql/client-cert.pem
-rw-------  1 mysql mysql uarch 1709 Sep  3 12:36 /var/db/mysql/client-key.pem
-rw-------  1 mysql mysql uarch 1705 Sep  3 12:36 /var/db/mysql/private_key.pem
-rw-r--r--  1 mysql mysql uarch  452 Sep  3 12:36 /var/db/mysql/public_key.pem
-rw-r--r--  1 mysql mysql uarch 1112 Sep  3 12:36 /var/db/mysql/server-cert.pem
-rw-------  1 mysql mysql uarch 1705 Sep  3 12:36 /var/db/mysql/server-key.pem

root@dlp:~ #
mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.36-2ubuntu3 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# SSL は有効な状態
root@localhost [(none)]> show variables like '%ssl%'; 
+-------------------------------------+-----------------+
| Variable_name                       | Value           |
+-------------------------------------+-----------------+
| admin_ssl_ca                        |                 |
| admin_ssl_capath                    |                 |
| admin_ssl_cert                      |                 |
| admin_ssl_cipher                    |                 |
| admin_ssl_crl                       |                 |
| admin_ssl_crlpath                   |                 |
| admin_ssl_key                       |                 |
| have_openssl                        | YES             |
| have_ssl                            | YES             |
| mysqlx_ssl_ca                       |                 |
| mysqlx_ssl_capath                   |                 |
| mysqlx_ssl_cert                     |                 |
| mysqlx_ssl_cipher                   |                 |
| mysqlx_ssl_crl                      |                 |
| mysqlx_ssl_crlpath                  |                 |
| mysqlx_ssl_key                      |                 |
| performance_schema_show_processlist | OFF             |
| ssl_ca                              | ca.pem          |
| ssl_capath                          |                 |
| ssl_cert                            | server-cert.pem |
| ssl_cipher                          |                 |
| ssl_crl                             |                 |
| ssl_crlpath                         |                 |
| ssl_fips_mode                       | OFF             |
| ssl_key                             | server-key.pem  |
| ssl_session_cache_mode              | ON              |
| ssl_session_cache_timeout           | 300             |
+-------------------------------------+-----------------+
27 rows in set (0.00 sec)

# mysqld が生成した証明書の有効期限は 10年
root@localhost [(none)]> show status like 'Ssl_server_not%'; 
+-----------------------+--------------------------+
| Variable_name         | Value                    |
+-----------------------+--------------------------+
| Ssl_server_not_after  | Sep  1 03:36:19 2034 GMT |
| Ssl_server_not_before | Sep  3 03:36:19 2024 GMT |
+-----------------------+--------------------------+
2 rows in set (0.00 sec)
[2] ユーザーに対して SSL/TLS アクセスを必須にする場合は、以下のように設定します。
root@dlp:~ #
mysql

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# SSL/TLS 必須ユーザーを新規作成
root@localhost [(none)]> create user freebsd identified by 'password' require ssl; 
Query OK, 0 rows affected (0.01 sec)

# SSL/TLS 必須ユーザーは ssl_type が ANY
root@localhost [(none)]> select user,host,ssl_type,plugin from mysql.user; 
+------------------+-----------+----------+-----------------------+
| user             | host      | ssl_type | plugin                |
+------------------+-----------+----------+-----------------------+
| freebsd          | %         | ANY      | caching_sha2_password |
| openbsd          | %         |          | caching_sha2_password |
| mysql.infoschema | localhost |          | caching_sha2_password |
| mysql.session    | localhost |          | caching_sha2_password |
| mysql.sys        | localhost |          | caching_sha2_password |
| root             | localhost |          | caching_sha2_password |
+------------------+-----------+----------+-----------------------+
6 rows in set (0.00 sec)

# 既存ユーザーを SSL/TLS 必須にする
root@localhost [(none)]> alter user 'openbsd'@'%' require ssl; 
Query OK, 0 rows affected (0.01 sec)

root@localhost [(none)]> select user,host,ssl_type,plugin from mysql.user; 
+------------------+-----------+----------+-----------------------+
| user             | host      | ssl_type | plugin                |
+------------------+-----------+----------+-----------------------+
| freebsd          | %         | ANY      | caching_sha2_password |
| openbsd          | %         | ANY      | caching_sha2_password |
| mysql.infoschema | localhost |          | caching_sha2_password |
| mysql.session    | localhost |          | caching_sha2_password |
| mysql.sys        | localhost |          | caching_sha2_password |
| root             | localhost |          | caching_sha2_password |
+------------------+-----------+----------+-----------------------+
6 rows in set (0.00 sec)
[3] TCP 接続する場合は、SSL/TLS が自動で有効となります。
root@dlp:~ #
mysql -u freebsd -p --protocol=tcp

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.35 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 接続状況確認
freebsd@localhost [(none)]> show status like 'ssl_cipher'; 
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| Ssl_cipher    | TLS_AES_256_GCM_SHA384 |
+---------------+------------------------+
1 row in set (0.00 sec)

freebsd@localhost [(none)]> exit 
Bye
関連コンテンツ