MariaDB(MySQL)初期設定時のセキュリティとutf8mb4化の手順

MariaDB(MySQL)初期設定時のセキュリティとutf8mb4化の手順 サーバー

MariaDB(MySQL)の初期設定時の「mysql_secure_installation」によるセキュリティ対策と、スマホの絵文字対策のために文字セット(charset)を「utf8mb4」へ変更する際の手順です。

MariaDB(MySQL)をインストールしてそのまま何もしないで利用するとセキュリティ的に問題があります。

文字セット(charset)も、スマホの絵文字対応の為に初期設定時にutf8mb4に変更しておくと後々何かと捗りますので初めからutf8mb4にしておきましょう。

※動作確認環境
CentOS 7.6
MariaDB 10.3

基本的な説明

MariaDBとは

MariaDB(マリアディービー)とは、MySQLから派生したオープンソースのRDBMS(リレーショナルデータベースシステム)です。基本的にMySQLの派生版なのでMySQLとの互換性がありますが、パフォーマンスがMariaDBのほうが上回っており、CentOS 7からはMySQLに変わってMariaDBが標準になっています。

utf8mb4とは

utf8mb4とはMySQLの文字セット(charset)の一種で、文字を1〜4バイトで表現します。

MySQLでの通常のutf8は文字を1〜3バイトで表現するのですが、utf8だとスマホなどの絵文字で使われる一部の4バイト文字が正常に識別出来ずに違う絵文字になってしまうという問題があり、通称「寿司ビール問題」などと呼ばれた経緯があります。

MariaDBのインストール

※ここから先はMariaDBの新規インストールの説明となります。既に利用しているMariaDBをアップグレードする場合は以下のページをご覧ください。

MariaDBのリポジトリ設定

最新のMariaDBのリポジトリをcurlで追加します。

curlを使わずに手動でMariaDBのリポジトリ(.repoファイル)を追加・作成する場合は MariaDB公式サイト にて各ディストリビューションごとのリポジトリ情報を確認できます。
まず、念のために追加するリポジトリの内容を確認します。
# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --help
Usage: curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- [OPTIONS]

    https://mariadb.com/kb/en/mariadb/mariadb-package-repository-setup-and-usage/

The MariaDB Repository supports these Linux OSs, on x86-64 only:
    * RHEL/CentOS 6 & 7
    * Ubuntu 14.04 LTS (trusty), 16.04 LTS (xenial), & 18.04 LTS (bionic)
    * Debian 8 (jessie) & 9 (stretch)
    * SLES 12 & 15

Options:
    --help                  Display this help and exit.

    --mariadb-server-version=<version>
                            Override the default MariaDB Server version.
                            By default, the script will use 'mariadb-10.3'.
    --mariadb-maxscale-version=<version>
                            Override the default MariaDB MaxScale version.
                            By default, the script will use '2.3'.

    --os-type=<type>        Override detection of OS type. Acceptable values include
                            'debian', 'ubuntu', 'rhel', and 'sles'.
    --os-version=<version>  Override detection of OS version. Acceptable values depend
                            on the OS type you specify.

    --skip-key-import       Skip importing GPG signing keys.

    --skip-maxscale         Skip the 'MaxScale' repository.
    --skip-server           Skip the 'MariaDB Server' repository.
    --skip-tools            Skip the 'Tools' repository.

    --write-to-stdout       Write output to stdout instead of to the OS's
                            repository configuration. This will also skip
                            importing GPG keys and updating the package 
                            cache on platforms where that behavior exists.

問題無いのでリポジトリを追加します。

# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo.
[info] Adding trusted package signing keys...
[info] Succeessfully added trusted package signing keys.

追加されたリポジトリを確認します。

# cat /etc/yum.repos.d/mariadb.repo

[mariadb-main]

name = MariaDB Server 
baseurl = https://downloads.mariadb.com/MariaDB/mariadb-10.3/yum/rhel/$releasever/$basearch 
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-Server-GPG-KEY 
gpgcheck = 1 
enabled = 1 

[mariadb-maxscale]

# To use the latest stable release of MaxScale, use “latest” as the version 
# To use the latest beta (or stable if no current beta) release of MaxScale, use “beta” as the version 
name = MariaDB MaxScale 
baseurl = https://downloads.mariadb.com/MaxScale/2.3/centos/$releasever/$basearch 
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-MaxScale-GPG-KEY 
gpgcheck = 1 
enabled = 1

[mariadb-tools]

name = MariaDB Tools 
baseurl = https://downloads.mariadb.com/Tools/rhel/$releasever/$basearch 
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-Enterprise-GPG-KEY 
gpgcheck = 1 
enabled = 1

正常に最新のMariaDBのリポジトリが追加されました。

MariaDBインストール

yumでMariaDBをインストールします。

# yum -y install MariaDB-devel MariaDB-client MariaDB-server
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * epel: ftp.iij.ad.jp
 * epel-debuginfo: ftp.iij.ad.jp
 * epel-source: ftp.iij.ad.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
mariadb-main                                                                                     | 2.9 kB  00:00:00     
mariadb-maxscale                                                                                 | 2.4 kB  00:00:00     
mariadb-tools                                                                                    | 2.9 kB  00:00:00     
(1/3): mariadb-tools/7/x86_64/primary_db                                                         |  11 kB  00:00:01     
(2/3): mariadb-maxscale/7/x86_64/primary_db                                                      | 6.7 kB  00:00:01     
(3/3): mariadb-main/7/x86_64/primary_db                                                          |  50 kB  00:00:01     
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ MariaDB-client.x86_64 0:10.3.12-1.el7.centos を インストール
--> 依存性の処理をしています: MariaDB-common のパッケージ: MariaDB-client-10.3.12-1.el7.centos.x86_64
---> パッケージ MariaDB-devel.x86_64 0:10.3.12-1.el7.centos を インストール
---> パッケージ MariaDB-server.x86_64 0:10.3.12-1.el7.centos を インストール
--> 依存性の処理をしています: perl(DBI) のパッケージ: MariaDB-server-10.3.12-1.el7.centos.x86_64
--> 依存性の処理をしています: galera のパッケージ: MariaDB-server-10.3.12-1.el7.centos.x86_64
--> 依存性の処理をしています: perl(Data::Dumper) のパッケージ: MariaDB-server-10.3.12-1.el7.centos.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ MariaDB-common.x86_64 0:10.3.12-1.el7.centos を インストール
--> 依存性の処理をしています: MariaDB-compat のパッケージ: MariaDB-common-10.3.12-1.el7.centos.x86_64
---> パッケージ galera.x86_64 0:25.3.25-1.rhel7.el7.centos を インストール
--> 依存性の処理をしています: libboost_program_options.so.1.53.0()(64bit) のパッケージ: galera-25.3.25-1.rhel7.el7.centos.x86_64
---> パッケージ perl-DBI.x86_64 0:1.627-4.el7 を インストール
--> 依存性の処理をしています: perl(RPC::PlServer) >= 0.2001 のパッケージ: perl-DBI-1.627-4.el7.x86_64
--> 依存性の処理をしています: perl(RPC::PlClient) >= 0.2000 のパッケージ: perl-DBI-1.627-4.el7.x86_64
---> パッケージ perl-Data-Dumper.x86_64 0:2.145-3.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ MariaDB-compat.x86_64 0:10.3.12-1.el7.centos を 非推奨
---> パッケージ boost-program-options.x86_64 0:1.53.0-27.el7 を インストール
---> パッケージ mariadb-libs.x86_64 1:5.5.60-1.el7_5 を 不要
---> パッケージ perl-PlRPC.noarch 0:0.2020-14.el7 を インストール
--> 依存性の処理をしています: perl(Net::Daemon) >= 0.13 のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Net::Daemon::Test) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Net::Daemon::Log) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Compress::Zlib) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ perl-IO-Compress.noarch 0:2.061-2.el7 を インストール
--> 依存性の処理をしています: perl(Compress::Raw::Zlib) >= 2.061 のパッケージ: perl-IO-Compress-2.061-2.el7.noarch
--> 依存性の処理をしています: perl(Compress::Raw::Bzip2) >= 2.061 のパッケージ: perl-IO-Compress-2.061-2.el7.noarch
---> パッケージ perl-Net-Daemon.noarch 0:0.48-5.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 を インストール
---> パッケージ perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

========================================================================================================================
 Package                           アーキテクチャー バージョン                             リポジトリー            容量
========================================================================================================================
インストール中:
 MariaDB-client                    x86_64           10.3.12-1.el7.centos                   mariadb-main            53 M
 MariaDB-compat                    x86_64           10.3.12-1.el7.centos                   mariadb-main           2.8 M
     mariadb-libs.x86_64 1:5.5.60-1.el7_5 を入れ替えます
 MariaDB-devel                     x86_64           10.3.12-1.el7.centos                   mariadb-main           7.0 M
 MariaDB-server                    x86_64           10.3.12-1.el7.centos                   mariadb-main           123 M
依存性関連でのインストールをします:
 MariaDB-common                    x86_64           10.3.12-1.el7.centos                   mariadb-main           166 k
 boost-program-options             x86_64           1.53.0-27.el7                          base                   156 k
 galera                            x86_64           25.3.25-1.rhel7.el7.centos             mariadb-main           8.1 M
 perl-Compress-Raw-Bzip2           x86_64           2.061-3.el7                            base                    32 k
 perl-Compress-Raw-Zlib            x86_64           1:2.061-4.el7                          base                    57 k
 perl-DBI                          x86_64           1.627-4.el7                            base                   802 k
 perl-Data-Dumper                  x86_64           2.145-3.el7                            base                    47 k
 perl-IO-Compress                  noarch           2.061-2.el7                            base                   260 k
 perl-Net-Daemon                   noarch           0.48-5.el7                             base                    51 k
 perl-PlRPC                        noarch           0.2020-14.el7                          base                    36 k

トランザクションの要約
========================================================================================================================
インストール  4 パッケージ (+10 個の依存関係のパッケージ)

総ダウンロード容量: 196 M
Downloading packages:
(1/14): MariaDB-10.3.12-centos73-x86_64-common.rpm                                               | 166 kB  00:00:02     
(2/14): MariaDB-10.3.12-centos73-x86_64-compat.rpm                                               | 2.8 MB  00:00:01     
(3/14): MariaDB-10.3.12-centos73-x86_64-devel.rpm                                                | 7.0 MB  00:00:01     
(4/14): boost-program-options-1.53.0-27.el7.x86_64.rpm                                           | 156 kB  00:00:00     
(5/14): MariaDB-10.3.12-centos73-x86_64-client.rpm                                               |  53 MB  00:00:12     
(6/14): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                           |  32 kB  00:00:00     
(7/14): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                                  |  47 kB  00:00:00     
(8/14): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                  | 260 kB  00:00:00     
(9/14): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                    |  51 kB  00:00:00     
(10/14): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                     |  36 kB  00:00:00     
(11/14): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                           |  57 kB  00:00:00     
(12/14): perl-DBI-1.627-4.el7.x86_64.rpm                                                         | 802 kB  00:00:00     
(13/14): galera-25.3.25-1.rhel7.el7.centos.x86_64.rpm                                            | 8.1 MB  00:00:01     
(14/14): MariaDB-10.3.12-centos73-x86_64-server.rpm                                              | 123 MB  00:00:24     
------------------------------------------------------------------------------------------------------------------------
合計                                                                                    6.6 MB/s | 196 MB  00:00:29     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : perl-Data-Dumper-2.145-3.el7.x86_64                                                    1/15 
  インストール中          : MariaDB-common-10.3.12-1.el7.centos.x86_64                                             2/15 
  インストール中          : MariaDB-compat-10.3.12-1.el7.centos.x86_64                                             3/15 
  インストール中          : MariaDB-client-10.3.12-1.el7.centos.x86_64                                             4/15 
  インストール中          : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                            5/15 
  インストール中          : boost-program-options-1.53.0-27.el7.x86_64                                             6/15 
  インストール中          : galera-25.3.25-1.rhel7.el7.centos.x86_64                                               7/15 
  インストール中          : perl-Net-Daemon-0.48-5.el7.noarch                                                      8/15 
  インストール中          : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                             9/15 
  インストール中          : perl-IO-Compress-2.061-2.el7.noarch                                                   10/15 
  インストール中          : perl-PlRPC-0.2020-14.el7.noarch                                                       11/15 
  インストール中          : perl-DBI-1.627-4.el7.x86_64                                                           12/15 
  インストール中          : MariaDB-server-10.3.12-1.el7.centos.x86_64                                            13/15 
chown: ユーザ指定が不正: `mysql'


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h 150-95-187-132 password 'new-password'

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:

Contribute - MariaDB.org
MariaDB Server is an Open Source project that thrives thanks to its community. MariaDB Foundation handles all community ...
インストール中 : MariaDB-devel-10.3.12-1.el7.centos.x86_64 14/15 削除中 : 1:mariadb-libs-5.5.60-1.el7_5.x86_64 15/15 検証中 : galera-25.3.25-1.rhel7.el7.centos.x86_64 1/15 検証中 : MariaDB-devel-10.3.12-1.el7.centos.x86_64 2/15 検証中 : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 3/15 検証中 : perl-Net-Daemon-0.48-5.el7.noarch 4/15 検証中 : MariaDB-client-10.3.12-1.el7.centos.x86_64 5/15 検証中 : MariaDB-compat-10.3.12-1.el7.centos.x86_64 6/15 検証中 : perl-Data-Dumper-2.145-3.el7.x86_64 7/15 検証中 : MariaDB-server-10.3.12-1.el7.centos.x86_64 8/15 検証中 : MariaDB-common-10.3.12-1.el7.centos.x86_64 9/15 検証中 : boost-program-options-1.53.0-27.el7.x86_64 10/15 検証中 : perl-DBI-1.627-4.el7.x86_64 11/15 検証中 : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 12/15 検証中 : perl-PlRPC-0.2020-14.el7.noarch 13/15 検証中 : perl-IO-Compress-2.061-2.el7.noarch 14/15 検証中 : 1:mariadb-libs-5.5.60-1.el7_5.x86_64 15/15 インストール: MariaDB-client.x86_64 0:10.3.12-1.el7.centos MariaDB-compat.x86_64 0:10.3.12-1.el7.centos MariaDB-devel.x86_64 0:10.3.12-1.el7.centos MariaDB-server.x86_64 0:10.3.12-1.el7.centos 依存性関連をインストールしました: MariaDB-common.x86_64 0:10.3.12-1.el7.centos boost-program-options.x86_64 0:1.53.0-27.el7 galera.x86_64 0:25.3.25-1.rhel7.el7.centos perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 置換: mariadb-libs.x86_64 1:5.5.60-1.el7_5 完了しました!

ちゃんとMariaDBがインストールされたか確認をします。

# yum list installed | grep mariadb
MariaDB-client.x86_64               10.3.12-1.el7.centos           @mariadb-main
MariaDB-common.x86_64               10.3.12-1.el7.centos           @mariadb-main
MariaDB-compat.x86_64               10.3.12-1.el7.centos           @mariadb-main
MariaDB-devel.x86_64                10.3.12-1.el7.centos           @mariadb-main
MariaDB-server.x86_64               10.3.12-1.el7.centos           @mariadb-main
galera.x86_64                       25.3.25-1.rhel7.el7.centos     @mariadb-main

OKです。最新のMariaDBのインストールが完了しました。

MariaDBの初期設定

MariaDBの起動及び自動起動設定

MariaDBを起動し、自動起動もオンにします。

# systemctl start mariadb
# systemctl enable mariadb

mysql_secure_installationの実行

MariaDBの初期設定時に不要なユーザーや不要なデータベースを削除してセキュリティを高める「mysql_secure_installation」を実行します。

mysql_secure_installationは対話式で進んでいきますので一つづつ解説していきます。

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): ■ここでEnterキーを押す。

rootパスワードがまだ未設定なので何も入力せずEnterキーを押します。

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] ■Yと記入してEnterキーを押す。

rootパスワードを新規に設定するので「Y」と記入してからEnterキーを押します。

New password: ■rootパスワードを入力してEnterキーを押す。
Re-enter new password: ■再度rootパスワードを入力してEnterキーを押す。

rootパスワードを二度記入してEnterキーを押します。

Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] ■Yと記入してEnterキーを押す。

匿名ユーザーを削除するか聞いてきますので「Y」と記入してからEnterキーを押します。

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] ■Yと記入してEnterキーを押す。

localhost以外からのrootアクセスを拒否するか聞いてきますので「Y」と記入してからEnterキーを押します。

 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] ■Yと記入してEnterキーを押す。

test用のデータベースを削除するか聞いてくるので「Y」と記入してからEnterキーを押します。

 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] ■Yと記入してEnterキーを押す。

特権テーブルをリロードしての今までの変更を今すぐ適用するか聞いてきますので「Y」と記入してからEnterキーを押します。

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

これでmysql_secure_installationは終了です。

不要なユーザーや不要なデータベースを削除してセキュリティが高まりました。

続いて「文字セット(charset)のutf8mb4化」を行います。

文字セット(charset)のutf8mb4化

現在のMariaDBの文字セット(charset)を確認

まず、現在のMariaDBの文字セット(charset)の状態を確認します。

「mysql -u root -p」でMariaDBにログインします。

# mysql -u root -p
Enter password: ■rootパスワードを入力
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.12-MariaDB MariaDB Server

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 variables like 'char%';」と入力してEnterキーで現在の文字セット(charset)の状態を表示します。

MariaDB [(none)]> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.003 sec)

MariaDB [(none)]>

「utf8」と「latin1」が混在しています。

show variables like 'coll%';」と入力してEnterキーで現在の照合順序(collation)の状態を表示します。

MariaDB [(none)]> show variables like 'coll%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.002 sec)

MariaDB [(none)]>

こちらも「utf8」と「latin1」が混在しています。

これらを「utf8mb4」に変更しますので、とりあえず「exit;EnterキーでMariaDBからログアウトします。

MariaDB [(none)]> exit;
Bye

文字セット(charset)の変更

まず、デフォルトのMariaDBの設定ファイルをバックアップします。

# cp /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf.bak

次に、MariaDBの設定ファイル(server.cnf)をvi等で編集し、[mariadb]という行の下に下記の3行を追加して上書き保存します。

character-set-server = utf8mb4
[client-mariadb]
default-character-set = utf8mb4

# vi /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers

[server]

# this is only for the mysqld standalone daemon

[mysqld]

# 
# * Galera-related settings 
#

[galera]

# Mandatory settings 
#wsrep_on=ON 
#wsrep_provider= 
#wsrep_cluster_address= 
#binlog_format=row 
#default_storage_engine=InnoDB 
#innodb_autoinc_lock_mode=2 
# 
# Allow server to accept connections on all interfaces. 
# #bind-address=0.0.0.0 
# 
# Optional setting 
#wsrep_slave_threads=1 
#innodb_flush_log_at_trx_commit=0 
# this is only for embedded server

[embedded]

# This group is only read by MariaDB servers, not by MySQL. 
# If you use the same .cnf file for MySQL and MariaDB, # you can put MariaDB-only options here

[mariadb]

character-set-server = utf8mb4 #■追加 
[client-mariadb] #■追加 
default-character-set = utf8mb4 #■追加 

# This group is only read by MariaDB-10.3 servers. 
# If you use the same .cnf file for MariaDB of different versions, 
# use this group for options that older servers don’t understand

[mariadb-10.3]

MariaDB 10.3.12でのテストでは[client-mariadb]という行が無かったので追加していますが、他のバージョンで既に[client-mariadb]という行がある場合は、その下に「default-character-set = utf8mb4」を追加してください。

設定ファイル(server.cnf)の編集が完了したらMariaDBを再起動します。

# systemctl restart mariadb

文字セット(charset)が変更されたかの確認

MariaDBの文字セット(charset)がちゃんとutf8mb4に変更されているか、もう一度MariaDBにログインして「show variables like 'char%';」と「show variables like 'coll%';」で確認します。

# mysql -u root -p
Enter password: ■rootパスワードを入力
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.12-MariaDB MariaDB Server

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 variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.002 sec)

MariaDB [(none)]> show variables like 'coll%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> exit;
Bye

OKです。ちゃんと変更されました。

「character_set_system」が「utf8」のままですがこれが正常です。

まとめ

昔は、MySQLの初期設定時に手動で不要なユーザー及びデータベースの削除を行ったり、手動でその他のセキュリティ設定をしていましたが、今は「mysql_secure_installation」で簡単にセキュアな初期設定が可能です。

文字セットのutf8mb4化も、運用中に後から変更する場合は何かと気を遣うので初めからやっておきましょう。

以上で解決です。

タイトルとURLをコピーしました