0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AlmaLinuxで構築するWeb3層構造:DBサーバ編

Posted at

今回は、Webアプリケーションに欠かせないデータベースサーバを構築します。
MySQLをインストールし、映画データを扱えるサンプルデータベース「sakila」を用いて、実用的な構成を目指します。

1.前提環境

ホスト名 役割 IPアドレス
webserver Webサーバ 192.168.56.10
apserver APサーバ 192.168.56.20
dbserver DBサーバ 192.168.56.30
  • OS:AlmaLinux 9.x
  • SELinux:無効化済み
  • 実行ユーザー:root または sudo権限を持つユーザー

2.名前解決の設定

2.1 /etc/nsswitch.conf の編集

パラメータ 設定値
passwd: files
shadow: files
group: files
hosts: files dns myhostname
services: files
netgroup: files
automount: files

→ /etc/hosts を優先して名前解決できるようにします。

2.2 /etc/hosts の追記

192.168.56.30  dbserver
192.168.56.10  webserver
192.168.56.20  apserver

3.ファイアウォールの設定

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

4.NTP(時刻同期)設定

/etc/chrony.conf に以下を追記し、時刻の正確性を確保します。

server ntp.nict.jp iburst
server ntp1.jst.mfeed.ad.jp iburst
server time.google.com iburst

allow 192.168.56.0/24

5.MySQLのインストールと初期設定

5.1 リポジトリ追加とインストール

dnf localinstall https://dev.mysql.com/get/mysql84-community-release-el9-1.noa rch.rpm -y
dnf module disable mysql
dnf install mysql-community-server -y

5.2 サービス起動と有効化

``bash
systemctl enable mysqld
systemctl start mysqld

初期パスワード確認
```bash
$ head -n 5 /var/log/mysqld.log
A temporary password is generated for root@localhost: <初期パスワード>

セキュア設定:

mysql_secure_installation

6.MySQL設定変更とタイムゾーン設定

6.1 /etc/my.cnfの一部変更

default_authentication_plugin=mysql_native_password
default_password_lifetime = 0
innodb_buffer_pool_size = 1024M

[mysqld_safe]
timezone = Asia/Tokyo

6.2 タイムゾーンデータの取り込み

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p
systemctl restart mysqld

7.APサーバとの接続設定とSakilaデータ投入

7.1 Sakilaデータベースの導入

cd /tmp
wget https://downloads.mysql.com/docs/sakila-db.tar.gz
tar zxvf sakila-db.tar.gz
mysql -u root -p < sakila-schema.sql
mysql -u root -p < sakila-data.sql

7.2 APサーバからの接続を許可

CREATE USER root@'<APサーバのIPアドレス>' IDENTIFIED BY '<MySQLのパスワード>';
GRANT ALL PRIVILEGES ON sakila.* TO 'root'@'<APサーバのIPアドレス>';
FLUSH PRIVILEGES;

接続権限の確認

mysql> SHOW GRANTS FOR 'root'@'192.168.56.20';
+--------------------------------------------------------------+
| Grants for root@192.168.56.20                                |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO `root`@`192.168.56.20`                 |
| GRANT ALL PRIVILEGES ON `sakila`.* TO `root`@`192.168.56.20` |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)

まとめ

MySQLのセットアップとデータベースの準備が完了しました。これでアプリケーションからデータを読み取れる環境が整ってきました。
次回は、Web/AP/DBの各層を連携させ、画面推移・検索機能を持つ簡易サイトを作成します。

本記事は「AlmaLinuxで構築するWeb3層構造」シリーズの第4弾です。
前回までの内容はこちらからどうぞ:

  • 第1回:仮想環境とAlmaLinux構築
  • 第2回:Webサーバ(httpd)構築
  • 第3回:APサーバ(Tomcat)構築
  • 第5回:Web/AP/DB連携
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?