AlmaLinux9.3にDNFでMySQL8.0.36を公式からインストール

データベースは、MySQL か MariaDB を使われる場合が多いと思います。

で、ひと昔前までは「MySQL も MariaDB も同じ」といっても過言ではなかったのですが、最近はちょっと変わって、それぞれの道を進むような感じになってきています。
詳しくはMariaDBが生まれた経緯とか歴史の授業になるのでここでは省略します。

ただ言えることは、ローカルの開発環境では MySQL、本番では MariaDB でもなんとなくOKだったのが、この先は「異なるデータベース」と考えたほうが良いと思います。

ということで、今回はMySQLのインストールをやっていきたいと思います。

なお本番環境で利用される場合はここにある内容だけを鵜呑みにせずセキュリティ専門家に相談されることをお勧めします。

環境

実施日2024-04-24
サーバさくらのVPS 2G
OSAlmaLinux9.3
cat /etc/redhat-release
AlmaLinux release 9.3 (Shamrock Pampas Cat)

MySQLのインストール

まずはもろもろ確認します。

MariaDBがインストールされていないことを確認

dnf list installed | grep mariadb

dnf のmysqlのバージョンを確認します。
このときは 8.0.36 でした。

dnf info mysql
メタデータの期限切れの最終確認: 2:25:14 前の 2024年04月24日 09時30分03秒 に実施しました。
利用可能なパッケージ
名前         : mysql
バージョン   : 8.0.36
リリース     : 1.el9_3
Arch         : x86_64
サイズ       : 2.7 M
ソース       : mysql-8.0.36-1.el9_3.src.rpm
リポジトリー : appstream
概要         : MySQL client programs and shared libraries
URL          : http://www.mysql.com
ライセンス   : GPLv2 with exceptions and LGPLv2 and BSD
説明         : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
             : client/server implementation consisting of a server daemon (mysqld)
             : and many different client programs and libraries. The base package
             : contains the standard MySQL client programs and generic MySQL files.

公式リポジトリを登録します。

▼の公式ダウンロードページにある「MySQL Yum Repository」でファイル名を確認します。

https://dev.mysql.com/downloads/repo/yum/

mysql80-community-release-el9-5.noarch.rpm

▲お目当てはこちらですが、もしかしたらファイル名が変わっているかもしれません。

リポジトリのインストール

dnf localinstall https://dev.mysql.com/get/mysql80-community-release-el9-5.noarch.rpm

デフォルトの mysql を無効化

dnf module disable mysql

インストール

パッケージの確認

dnf info mysql-community-server

インストール

dnf install mysql-community-server

AlmaLinux9.3デフォルトの dnf だと MySQL8.0.36 です。
公式版でも最新の MySQL8.0.36 になります。

mysqld --version
/usr/sbin/mysqld  Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)

初めての起動

このまま、起動と自動起動設定を行っておきます。

自動起動設定

systemctl enable mysqld

起動

systemctl start mysqld

パスワードを確認

ログの先頭 4行目あたりを見ます。

head -n 5 /var/log/mysqld.log
A temporary password is generated for root@localhost: XXXXXXXX

4行目あたりで ▲ を見つけたら XXXXXXXX に書かれているのが root の初期パスワードです。

mysql に接続

mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

ここではあくまでも接続確認だけにします。

mysql> quit;

▲で、MySQLから抜けます。

MySQLの初期設定

初期設定は対話型で行います。

mysql_secure_installation
# rootパスワードを入力
Enter password for user root:

# rootの新しいパスワード
# 英(大小)数記号が混ざってないとダメ
The existing password for the user account root has expired. Please set a new password.

# パスワードの強度 100
Estimated strength of the password: 100

# root のパスワードを変更するか
# はいなら y いいえなら他のキー
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

# 入力したパスワードでOKか?
# はいなら y いいえなら他のキー
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :

# 匿名ユーザーを削除するか?
# はい(削除する)なら y いいえなら他のキー
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

# リモートからの root へのログインを禁止するか
# はい(禁止する)なら y いいえなら他のキー
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

# テスト用データベースを削除するか
# はい(削除する)なら y いいえなら他のキー
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

# 特権テーブルをリロードするか? ( = 設定を確定するか)
# はい(確定する)なら y いいえなら他のキー
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

mysqlに接続して確認

root で接続します。パスワードは先ほど変更したばかりのものです。

mysql -u root -p

文字コードの設定を確認。

show variables like "chara%";
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb4                        |
| character_set_connection | utf8mb4                        |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb4                        |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8mb3                        |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)
mysql> quit;

確認が終わったら quit; で抜けておきます。

設定の変更

ここで直接編集するのではなくGit管理することをオススメしますがどちらでも大丈夫です。
AlmaLinux9.3の設定管理をGitで行う

vi /etc/my.cnf

変更後は再起動して適用します。

systemctl restart mysqld

パスワード認証の有効化

[mysqld]
# コメントを外して有効化
default-authentication-plugin=mysql_native_password

認証方式がMySQL8.0から変更になっています。
とはいっても、新しい caching_sha2_password では都合が悪いこともあると思います。出始めということもあり。

上記設定で、認証方式のデフォルトを以前までの mysql_native_password に変更します。

パスワードの有効期限を無期限にする

[mysqld]
default_password_lifetime = 0

バッファプールのサイズを設定する

[mysqld]
innodb_buffer_pool_size = 1024M

物理メモリの 60 ~ 80 % が目安
割り当てすぎるとCPU負荷が異常に高くなる

Asia/Tokyo の追加

# 追加
[mysqld_safe]
timezone = Asia/Tokyo

timezone = Asia/Tokyo を追加した場合はこちらを実行

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p

次のような警告(Warning)が出ますが無視して大丈夫です。

Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

rootパスワードを忘れた場合

vi /etc/my.cnf
[mysqld]
skip-grant-tables

mysql を再起動

systemctl restart mysqld

mysqlに入る

mysql -u root
-- skip-grant-tables を無効化する
FLUSH PRIVILEGES;

-- 新しいパスワード
ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootパスワード';

-- native password にした場合
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootパスワード';

quit;
vi /etc/my.cnf

# skip-grant-tables を削除

# mysqlを再起動
systemctl restart mysqld

コマンド集

バージョン

mysql -V

自動起動が有効か確認

systemctl list-unit-files | grep mysqld*

自動起動にセット

systemctl enable mysqld

起動状況の確認

systemctl status mysqld

起動

systemctl start mysqld

再起動

systemctl restart mysqld

停止

systemctl stop mysqld

設定変更

vi /etc/my.cnf

MySQLのコマンド集

終了

quit;

終了

quit;

パスワード変更

ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootパスワード';

文字コードの確認

show variables like 'character_set%';