このセクションでは、MariaDBの初期セットアップについて紹介していきます。

MariaDB セキュリティの初期設定

MariaDB セキュリティの初期設定行います。

root ユーザーの現在のパスワードを聞かれるので、ここでは Enter を押し次へ進みます(初期は root パスワードが設定されていません)。

[user@pub-web ~]$ sudo 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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

unix_socket 認証をするかどうか要求されるので、ここでは n(いいえ)を指定します。

新しいバージョンのMariaDBでは、unix_socket 認証が採用されています(この時点では MariaDB のバージョンは10.5.16 です)。少し調べてみると、unix_socket 認証を使うと OS のログインユーザー名と DBの接続ユーザー名が同じであれば UnixSocketを使って認証することでパスワードなしで DB への接続を許可しましょうという仕組みになっているようです。

現時点では、unix_socket 認証の必要性もあまり感じられず、また unix_soket 認証による運用トラブルが何件かネット情報でも紹介されているので ここでは unix_socket 認証は使用しないことにしています。

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

root ユーザーのパスワード変更を聞かれるので、y(はい)で新しいパスワードを設定します。

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

匿名ユーザーを削除するかどうか聞かれるので、ここでは y(はい)で匿名ユーザーを削除しておきます。

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
 ... Success!

リモートから root ユーザーでのログインを拒否するかどうか聞かれるので、y(はい)で拒否する設定にします。

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
 ... Success!

test データベースとそのデータベースへのアクセスを削除するかどうか聞かれるので、y(はい)で削除しておきます。

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
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

権限テーブルを再読み込みするかどうか聞かれるので、y(はい)とします。ここで正常であれば、初期設定が終了します。

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

Reload privilege tables now? [Y/n] y
 ... 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!
[yuji@pub-web ~]$ 

MariaDB 初期設定

MariaDBのサーバー側の文字コードを設定します。

[user@pub-web ~]$ sudo vi /etc/my.cnf.d/mariadb-server.cnf 

[ server ] に character-set-server = utf8 を追記し保存します。

[server]

# this is only for the mysqld standalone daemon
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
character-set-server = utf8                 ← 追記

MariaDBのクライアント側の文字コードを設定します。

[user@pub-web ~]$ sudo vi /etc/my.cnf.d/client.cnf 

[ client ] に default-character-set = utf8 を追記し保存します。

[client]
default-character-set = utf8

サーバーを再起動して設定を反映します。

[user@pub-web ~]$ sudo systemctl restart mariadb

再起動後、MariaDB サービスが起動状態であることを確認します。

[yuji@pub-web ~]$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
     Active: active (running) since Tue 2023-08-15 22:38:38 EDT; 5s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 8994 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 9016 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
    Process: 9065 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 9051 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 11125)
     Memory: 64.0M
        CPU: 165ms
     CGroup: /system.slice/mariadb.service
             └─9051 /usr/libexec/mariadbd --basedir=/usr

Aug 15 22:38:38 pub-web systemd[1]: Starting MariaDB 10.5 database server...
Aug 15 22:38:38 pub-web mariadb-prepare-db-dir[9016]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing i>
Aug 15 22:38:38 pub-web mariadb-prepare-db-dir[9016]: If this is not the case, make sure the /var/lib/mysql is empty before running>
Aug 15 22:38:38 pub-web mariadbd[9051]: 2023-08-15 22:38:38 0 [Note] /usr/libexec/mariadbd (mysqld 10.5.16-MariaDB) starting as pro>
Aug 15 22:38:38 pub-web systemd[1]: Started MariaDB 10.5 database server.
lines 1-21/21 (END)