環境構築からWEBアプリ開発・スマホアプリ開発まで。ときには動画制作やゲームも。

supilog
すぴろぐ

RockyLinuxで初めてのwordpress#05-MySQL8インストール【WebARENA Indigo】

RockyLinuxで初めてのwordpress#05-MySQL8インストール【WebARENA Indigo】

※前回までの手順を実施している前提の手順となっています

いよいよDBのインストールに入ります。今回はMySQL8を採用することとしました。

※とくに記述がない限りは、リモートサーバー内でrootユーザーとして作業するものとします。

インストール

# リポジトリをインストール
$ dnf -y install https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm

# mysql-community-serverをインストール
$ dnf -y install --disablerepo=appstream --enablerepo=mysql80-community mysql-community-server

GPG-KEYでエラーになった場合は、最新のGPG-KEYをインストール

# 最新のGPG-KEYをインストール
$ rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

これを実行後に再度インストールコマンドを実行してください。

初期化処理

$ mysqld --initialize --user=mysql --datadir=/var/lib/mysql

設定ファイル(/etc/my.cnf)

# バックアップ
$ cp -i /etc/my.cnf /etc/my.cnf.org

# 設定
$ vi /etc/my.cnf
※既存設定を削除し、下記内容に書き換える
----------
[mysqld]
datadir=/var/lib/mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/lib/mysql/mysql.sock

character_set_server = utf8mb4
collation_server = utf8mb4_ja_0900_as_cs

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# error log
log-error = /var/log/mysql/error.log

# slow query log
slow_query_log = 0
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 5.0
log_queries_not_using_indexes = 0

# general log
general_log = 0
general_log_file = /var/log/mysql/query.log

[mysql]
default-character-set = utf8mb4

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

ディレクトリ作成

$ mkdir /var/log/mysql
$ chown mysql:mysql /var/log/mysql

起動・自動起動設定

$ systemctl start mysqld.service
$ systemctl enable mysqld.service

mysql_secure_installation実行

こちらは必要最低限のセキュリティ設定をしてくれるコマンドです。案内に従いながら必要な設定をしていきます。

MySQLのrootユーザーの初期パスワードを確認

$ grep password /var/log/mysqld.log

このあと使用するので、メモっておきます。

mysql_secure_installation実行

# 実行
$ mysql_secure_installation
現在のパスワードを入力
【表示】
Enter password for user root:

先程メモしたrootパスワードを入力し、Enterを押します。

新しいrootパスワードを入力
【表示】
New password:

新しいパスワードを入力します。

【表示】
Re-enter new password:

確認のため、新しいパスワードを再度入力します。

「VALIDATE PASSWORD COMPONENT」セットアップ
【表示】
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

こちらは、MySQL接続ユーザー作成の際に、設定されるパスワードをポリシーに従い検証を行ってくれるものである。「y」を入力します。

【表示】
There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                 

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:

パスワードのポリシーを聞かれます。LOW、MEDIUM、STRONGと設定があり、当然STRONGがパスワードに対する規則が一番厳しく安全です。個人的には本番環境であればMEDIUM以上が良いかなと思います。0=LOW, 1=MEDIUM, 2=STRONGを数字で入力して、Enter。

rootのパスワードを変更
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

先程設定済みなので、何も入力せずにEnter。

anonymous users を削除
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

anonymous usersというのは匿名アカウントです。不要なので削除します。「y」を入力。

rootアカウントでのリモートログインを不許可にする
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

y」を入力します。

テストDBの削除
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

テストDBは不要なので削除します。「y」を入力します。

特権テーブルのリロード
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

y」を入力します。

これで設定完了です。

WordPress用DB作成

いよいよDBを作成します。DB名とユーザー名とパスワードは任意に決めて頂いて問題ありません。今回は以下で作成します。

  • DB名「wordpress」
  • DBユーザー名「wordpressuser」
  • パスワード「25399cce5F9f702305ddc184@7e4bc5f」
# rootユーザーでログイン
$ mysql -uroot -p

# 下記のSQLを実行
# mysql> CREATE DATABASE wordpress;
# mysql> CREATE USER wordpressuser@localhost IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '25399cce5F9f702305ddc184@7e4bc5f';
# mysql> GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;
# mysql> exit;

接続確認

先程作成したユーザーでDBに接続できるか試してみましょう。

# 接続確認
mysql -u wordpressuser -p wordpress
→パスワードを入力し、wordpressデータベースに接続できればOK

これでDB作成作業は完了です。

まとめ