CentOS 8 にMySQLを公式からインストール

さくらのVPSにCentOS 8をインストールした覚書です。

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

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

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

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

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

環境

実施日2020-05-29
サーバさくらのVPS 2G
OSCentOS 8.1
cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

MySQLのインストール

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

# MariaDBがインストールされていないことを確認
dnf list installed | grep mariadb

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

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

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

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

mysql80-community-release-el8-1.noarch.rpm

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

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

# デフォルトの mysql を無効化
dnf module disable mysql

インストール

# パッケージの確認
dnf info mysql-community-server

# インストール
dnf install mysql-community-server

CentOS 8 デフォルトの dnf だと、MySQL8.0.17 でしたが、公式版なので最新の MySQL8.0.20 になります。

mysqld --version
/usr/sbin/mysqld  Ver 8.0.20 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.20

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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

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

mysql> 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     | utf8                           |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)

以前のバージョンでは変更の必要がありましたが、今回は変更しなくても設定済みのようです。デフォルトが変わったのかな。

mysql> quit;

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

設定の変更

vi /etc/my.cnf
# コメントを外して有効化
default-authentication-plugin=mysql_native_password

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

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

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 mysql*

# 自動起動にセット
systemctl enable mysql

# 起動状況の確認
systemctl status mysql

# 起動
systemctl start mysqld

# 再起動
systemctl restart mysqld

# 停止
systemctl stop mysqld

# 設定変更
vi /etc/my.cnf
# 終了
quit;

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

# 文字コードの確認
show variables like 'character_set%';