Ubuntu 21.04
Sponsored Link

MariaDB 10.5 : SSL/TLS の設定
2021/05/18
 
MariaDB で SSL/TLS による暗号化通信の設定を有効にします。
[1]
事前に SSL/TLS 証明書を取得 または 自己署名の証明書を作成しておきます。
当例では自己署名の証明書で進めます。
[2] 作成した証明書をコピーして SSL/TLS の設定をします。
# 作成した証明書をコピー

root@www:~#
mkdir /var/lib/mysql/pki

root@www:~#
cp /etc/ssl/private/{server.crt,server.key} /etc/ssl/certs/ca-certificates.crt /var/lib/mysql/pki/

root@www:~#
openssl rsa -in /var/lib/mysql/pki/privkey.pem -out /var/lib/mysql/pki/priv.key

root@www:~#
chown -R mysql. /var/lib/mysql/pki
root@www:~#
vi /etc/mysql/mariadb.conf.d/50-server.cnf
# 86行目 : 取得した証明書を追記

ssl-ca = /var/lib/mysql/pki/ca-certificates.crt
ssl-cert = /var/lib/mysql/pki/server.crt
ssl-key = /var/lib/mysql/pki/server.key
root@www:~#
systemctl restart mariadb
# 設定確認

root@www:~#
mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.5.10-MariaDB-0ubuntu0.21.04.1 Ubuntu 21.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

# 以下のように表示されれば OK
MariaDB [(none)]> show variables like '%ssl%'; 
+---------------------+----------------------------------------+
| Variable_name       | Value                                  |
+---------------------+----------------------------------------+
| have_openssl        | YES                                    |
| have_ssl            | YES                                    |
| ssl_ca              | /var/lib/mysql/pki/ca-certificates.crt |
| ssl_capath          |                                        |
| ssl_cert            | /var/lib/mysql/pki/server.crt          |
| ssl_cipher          |                                        |
| ssl_crl             |                                        |
| ssl_crlpath         |                                        |
| ssl_key             | /var/lib/mysql/pki/server.key          |
| version_ssl_library | OpenSSL 1.1.1j  16 Feb 2021            |
+---------------------+----------------------------------------+
10 rows in set (0.001 sec)
[3] SSL/TLS で接続する場合は [ssl] オプションを指定して接続します。
root@www:~#
mysql --ssl

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.5.10-MariaDB-0ubuntu0.21.04.1 Ubuntu 21.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

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

MariaDB [(none)]> exit 
Bye

# 非暗号化通信の場合

root@www:~#
mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.5.10-MariaDB-0ubuntu0.21.04.1 Ubuntu 21.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

# 値はブランク
MariaDB [(none)]> show status like 'ssl_cipher'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Ssl_cipher    |       |
+---------------+-------+
1 row in set (0.001 sec)
[4] ユーザーに対して SSL/TLS アクセスを必須にする場合は、以下のように設定します。
root@www:~#
mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 33
Server version: 10.5.10-MariaDB-0ubuntu0.21.04.1 Ubuntu 21.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

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

# SSL/TLS 必須ユーザーは ssl_type が ANY
MariaDB [(none)]> select user,host,ssl_type from mysql.user; 
+-------------+-----------+----------+
| User        | Host      | ssl_type |
+-------------+-----------+----------+
| mariadb.sys | localhost |          |
| root        | localhost |          |
| mysql       | localhost |          |
| ubuntu      | %         |          |
| debian      | %         | ANY      |
+-------------+-----------+----------+
5 rows in set (0.001 sec)

# 既存ユーザーを SSL/TLS 必須にする
MariaDB [(none)]> grant usage on *.* to 'ubuntu'@'%' require ssl; 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host,ssl_type from mysql.user; 
+-------------+-----------+----------+
| User        | Host      | ssl_type |
+-------------+-----------+----------+
| mariadb.sys | localhost |          |
| root        | localhost |          |
| mysql       | localhost |          |
| ubuntu      | %         | ANY      |
| debian      | %         | ANY      |
+-------------+-----------+----------+
5 rows in set (0.001 sec)
関連コンテンツ