公開日:2026/6/1 0:00:00

AlmaLinux 10.2 セットアップメモ

Section 3.1 データベースサーバー構築編(MariaDB)

About

MariaDB のインストールと設定手順を残しています。

環境は、Section 1.1Section 1.3で作成したvirtualboxの環境で実行しています。 そのため、SELinuxは有効な状態で作業を進めています。

起動後、rootアカウントになって作業を進めます。

パッケージの確認

リポジトリに登録されているものからパッケージ名とバージョンを指定してインストールすることになります。 まずは、提供されているかどうかを調べます。

[root@localhost ~]# dnf search mariadb
============================================ Name Exactly Matched: mariadb =============================================
mariadb.x86_64 : A very fast and robust SQL database server
(略)
[root@localhost ~]# dnf repoquery mariadb
mariadb-3:10.11.15-2.el10_1.alma.1.x86_64
[root@localhost ~]#

時間がたつとセキュリティアップデートの関係で複数のバージョンが登録されます。

インストール

パッケージがあるのでインストールしていきましょう。

[root@localhost ~]# dnf -y install mariadb mariadb-server
========================================================================================================================
 Package                                Architecture     Version                              Repository           Size
========================================================================================================================
Installing:
 mariadb                                x86_64           3:10.11.15-2.el10_1.alma.1           appstream           1.6 M
 mariadb-server                         x86_64           3:10.11.15-2.el10_1.alma.1           appstream            10 M
Installing dependencies:
 checkpolicy                            x86_64           3.10-1.el10                          appstream           368 k
 lzo                                    x86_64           2.10-14.el10                         baseos               66 k
 mariadb-common                         noarch           3:10.11.15-2.el10_1.alma.1           appstream            29 k
 mariadb-connector-c                    x86_64           3.4.4-1.el10                         baseos              206 k
 mariadb-connector-c-config             noarch           3.4.4-1.el10                         baseos              8.9 k
 mariadb-errmsg                         noarch           3:10.11.15-2.el10_1.alma.1           appstream           255 k
 mysql-selinux                          noarch           1.0.14-1.el10_0                      appstream            37 k
 perl-DBD-MariaDB                       x86_64           1.23-10.el10                         appstream           153 k
 perl-DBI                               x86_64           1.643-26.el10                        appstream           713 k
 policycoreutils-python-utils           noarch           3.10-1.el10                          appstream            45 k
 python3-audit                          x86_64           4.0.3-5.el10                         appstream            68 k
 python3-distro                         noarch           1.9.0-5.el10                         appstream            51 k
 python3-libsemanage                    x86_64           3.10-1.el10                          appstream            80 k
 python3-policycoreutils                noarch           3.10-1.el10                          appstream           2.1 M
 python3-setools                        x86_64           4.6.0-3.el10                         baseos              724 k
 snappy                                 x86_64           1.1.10-7.el10                        baseos               37 k
Installing weak dependencies:
 mariadb-backup                         x86_64           3:10.11.15-2.el10_1.alma.1           appstream           6.5 M
 mariadb-client-utils                   x86_64           3:10.11.15-2.el10_1.alma.1           appstream            39 k
 mariadb-gssapi-server                  x86_64           3:10.11.15-2.el10_1.alma.1           appstream            17 k
 mariadb-server-utils                   x86_64           3:10.11.15-2.el10_1.alma.1           appstream           255 k

Transaction Summary
========================================================================================================================
Install  22 Packages

(略)

Complete!
[root@localhost ~]#

インストールが終わりました。

サービス起動とセキュリティ設定

サービスを起動してから、MySQL の mysql_secure_installation を使って設定します。

まずはサービスを起動します

[root@localhost ~]# systemctl start mariadb

次に、 mysql_secure_installation を使って設定します。 聞かれる項目は次の通り。

  • 現在の roor パスワード
    • 未設定なはずなので enter で良い
  • unix_socket authentication を使うか?
    • DBにアクセスするユーザ認証で、Linuxの認証が使えるようにするというもの
    • Y を選択した場合、Linuxユーザに存在するDBユーザを作ると認証スルー出来る
    • なので、root になっていれば、コンソールから簡単にアクセスできる
    • そういう設定
  • root ユーザのパスワードを変えるか?
    • Y を選択した場合、パスワード入力を行ってください
    • 開発アプリでrootユーザでアクセスすることはないと思うので vagrant にしておいてよいでしょう
  • anonymous ユーザを削除するかどうか?
  • rootユーザはリモートからのDBアクセスを禁止してよいか?
  • test database や アクセス権限を削除してよいか?
  • 権限の再読み込みを実施してよいか?

最初の unix_socket authentication のところだけ、人によっては気を付ける必要がある場所ですね。 組織環境下で行う場合はルールがどうなっているかは確認が必要でしょう。 また、apacheがアクセスするDBユーザはapacheにするなんてルールの場合、サービス乗っ取られたらどうなるのか? などのリスク判断は必要になると思います。

[root@localhost ~]# 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...

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] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!


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!


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!

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!

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!

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!
[root@localhost ~]#

これで初期設定が完了しました。

この手順も裏ではSQLを実行しているだけなので、SQLに置き換えが可能です。 Linnux側のrootアカウントでmariadbコマンドを使ってSQLを実行します。 なお、上記の mysql_secure_installation を実行した場合は、実行しないでください。

echo "-- unix_socket authentication = yes"
mariadb -u root --execute "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';"
mariadb -u root --execute "FLUSH PRIVILEGES;"

echo "--- Change root password"
mariadb -u root --execute "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('vagrant')) WHERE User='root';"

echo "--- Remove anonymous users"
mariadb -u root --execute "DELETE FROM mysql.global_priv WHERE User='';"

echo "--- Disallow root login remotely"
mariadb -u root --execute "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"

echo "--- Drop and delete test database"
mariadb -u root --execute "DROP DATABASE IF EXISTS test;"
mariadb -u root --execute "DELETE FROM mysql.db WHERE Db='test' OR Db='test_%';"
mariadb -u root --execute "FLUSH PRIVILEGES;"

サービスの有効化

systemd を通じてサービスの有効化を行います。これでサーバー起動時に勝手にサービスがスタートします。

[root@localhost ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/syst//system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/syst//system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/syst//system/mariadb.service.
[root@localhost ~]#

別のサーバーからアクセスさせる場合の設定

初期設定では、外部からのアクセスが完全に禁止されています。 そのため許可するように設定を変更してみましょう。 ローカル内に閉じるのであれば設定は不要です。

[root@localhost ~]# cp --preserve=context /etc/my.cnf.d/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf.org
[root@localhost ~]# cat /etc/my.cnf.d/mariadb-server.cnf.org \
| sed -E "/^pid-file=.*$/a skip-networking = 0\nbind-address = 0.0.0.0" \
> /etc/my.cnf.d/mariadb-server.cnf
[root@localhost ~]# diff -cT /etc/my.cnf.d/mariadb-server.cnf.org /etc/my.cnf.d/mariadb-server.cnf
*** /etc/my.cnf.d/mariadb-server.cnf.org  YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /etc/my.cnf.d/mariadb-server.cnf      YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 18,23 ****
--- 18,25 ----
        socket=/var/lib/mysql/mysql.sock
        log-error=/var/log/mariadb/mariadb.log
        pid-file=/run/mariadb/mariadb.pid
+       skip-networking = 0
+       bind-address = 0.0.0.0


        #

MariaDBのマニュアル(Configuring MariaDB for Remote Client Access)を参考にしてください。 なお、bind-address=0.0.0.0 の代わりに skip-bind-address も使えます。

設定を変えたらサービスを再起動しましょう。

[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]#

ロールとデータベースを作成する

データベースにアクセスするためのユーザーと権限(ロール)とデータベースを作成します。

vagrant ユーザーがいるのでそのユーザー用のロールとを作成します。 phpMyAdminなどを使って作成するほうがわかりやすいので、初めて作成する場合はそちらをお勧め。

とりあえず、phpMyAdminでユーザーを作成した時のSQLを実行します。

まずは、ユーザーの作成。 vagrant ユーザーを作成して、ローカルとそれ以外からのアクセスを許可するまでを一本で。

[root@localhost src]# mariadb -u root --execute "\
CREATE USER 'vagrant'@'%' IDENTIFIED BY 'vagrant'; \
GRANT USAGE ON *.* TO 'vagrant'@'%' REQUIRE NONE WITH \
 MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;"

次にデータベースを作成しつつ、権限(ロール)設定。vagrant データベースと 頭に vagrant_ とついたデータベースにアクセスできるようにします。

[root@localhost src]# mariadb -u root --execute "\
CREATE DATABASE IF NOT EXISTS vagrant; \
GRANT ALL PRIVILEGES ON vagrant.* TO 'vagrant'@'%'; \
GRANT ALL PRIVILEGES ON \`vagrant_%\`.* TO 'vagrant'@'%';"

接続確認

mariadbコマンドを使ってアクセスします。

[vagrant@localhost ~]$ mariadb -u vagrant -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.11.15-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)]> \q
Bye
[vagrant@localhost ~]$

注意事項

Redhat 10 系では、MariaDBとMySQLのパッケージが競合する状態になっているため、どちらもインストールすることはできません。 おおよそ互換があるのでどちらかでいいと思いますが、どうしても同時に存在させたい場合はコンテナを使えというが回答となっています。

なお、本当に入らないのか確認したところ入りませんでした……

[root@localhost]# dnf install mysql8.4-server
Error:
 Problem: problem with installed package mysql8.4-server(略)
 (以下略)

こうなってしまうので、同居させたい場合は構成を再検討しましょう。