Amazon Linux 2023 Section 2 Apache + mariadb + PHP 構成
Section 2.1 MariaDB
About
MariaDB のインストールと設定手順を残しています。 RDSを使うのでClientはインストールするにしても、Serverまでというケースは少ない気もします。
環境は、Section 1.1~Section 1.3で作成したvirtualboxの環境で実行しています。 起動後、rootアカウントになって作業を進めます。
バージョンの確認
リポジトリに登録されているものからパッケージ名とバージョンを指定してインストールすることになります。 まずは、提供されているかどうかを調べます。
[root@localhost ~]# dnf search mariadb*server
============================================== 名前 一致: mariadb*server ===============================================
mariadb1011-gssapi-server.x86_64 : GSSAPI authentication plugin for server
mariadb1011-server.x86_64 : The MariaDB server and related files
mariadb105-gssapi-server.x86_64 : GSSAPI authentication plugin for server
mariadb105-server.x86_64 : The MariaDB server and related files
mariadb114-gssapi-server.x86_64 : GSSAPI authentication plugin for server
mariadb114-server.x86_64 : The MariaDB server and related files
mariadb118-gssapi-server.x86_64 : GSSAPI authentication plugin for server
mariadb118-server.x86_64 : The MariaDB server and related files
mariadb123-gssapi-server.x86_64 : GSSAPI authentication plugin for server
mariadb123-server.x86_64 : The MariaDB server and related files
[root@localhost ~]#
いくつか登録されているのでバージョンを確認しておきます。
[root@localhost ~]# dnf repoquery mariadb1011
mariadb1011-3:10.11.11-1.amzn2023.0.1.x86_64
mariadb1011-3:10.11.11-1.amzn2023.0.2.x86_64
mariadb1011-3:10.11.13-1.amzn2023.0.1.x86_64
mariadb1011-3:10.11.15-1.amzn2023.0.1.x86_64
mariadb1011-3:10.11.18-1.amzn2023.0.1.x86_64
[root@localhost ~]# dnf repoquery mariadb123
mariadb123-3:12.3.2-2.amzn2023.0.1.x86_64
[root@localhost ~]#
すべての Amazon Linux 2023 パッケージ このページ内にある2023.12.YYYYMMDD リリースの Amazon Linux 2023 RPM パッケージ というところからサポート期間がわかるので、それを見てAmazon Linux 2023のサポート終了までサポートしてくれそうなものを選ぶのが良いでしょう。
今回は、12.3 があるのでこれをインストールします。
インストール
対象バージョンを指定してインストールします。
[root@localhost ~]# dnf -y install mariadb123 mariadb123-server
依存関係が解決しました。
========================================================================================================================
パッケージ Arch バージョン リポジトリー サイズ
========================================================================================================================
インストール:
mariadb123 x86_64 3:12.3.2-2.amzn2023.0.1 amazonlinux 2.1 M
mariadb123-server x86_64 3:12.3.2-2.amzn2023.0.1 amazonlinux 12 M
依存関係のインストール:
lzo x86_64 2.10-4.amzn2023.0.2 amazonlinux 76 k
mariadb-connector-c x86_64 3.3.10-1.amzn2023.0.2 amazonlinux 211 k
mariadb-connector-c-config noarch 3.3.10-1.amzn2023.0.2 amazonlinux 9.3 k
mariadb123-common noarch 3:12.3.2-2.amzn2023.0.1 amazonlinux 53 k
mariadb123-errmsg noarch 3:12.3.2-2.amzn2023.0.1 amazonlinux 218 k
mysql-selinux noarch 1.0.14-2.amzn2023.0.1 amazonlinux 36 k
perl-DBD-MariaDB x86_64 1.22-1.amzn2023.0.5 amazonlinux 152 k
perl-DBI x86_64 1.652-1.amzn2023.0.1 amazonlinux 811 k
policycoreutils-python-utils noarch 3.4-6.amzn2023.0.3 amazonlinux 69 k
snappy x86_64 1.1.8-5.amzn2023.0.2 amazonlinux 35 k
弱い依存関係のインストール:
mariadb123-backup x86_64 3:12.3.2-2.amzn2023.0.1 amazonlinux 7.1 M
mariadb123-client-utils noarch 3:12.3.2-2.amzn2023.0.1 amazonlinux 69 k
mariadb123-cracklib-password-check x86_64 3:12.3.2-2.amzn2023.0.1 amazonlinux 18 k
mariadb123-gssapi-server x86_64 3:12.3.2-2.amzn2023.0.1 amazonlinux 18 k
mariadb123-server-utils noarch 3:12.3.2-2.amzn2023.0.1 amazonlinux 27 k
トランザクションの概要
========================================================================================================================
インストール 17 パッケージ
(中略)
完了しました!
[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 を選択した場合、パスワード入力を行ってください
- anonymous ユーザを削除するかどうか?
- roorユーザはリモートからの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 ~]#
これで初期設定が完了しました。
サービスの有効化
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 ~]# 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 ~]# 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 n
Server version: 12.3.2-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 ~]$