LoginSignup
0
3

More than 3 years have passed since last update.

CentOS8.2にMySQL8.0をセットアップする手順

Last updated at Posted at 2020-11-25
../

VPSサーバー(CentOS8.2)にMySQL 8.0をインストールした。MySQLは過去に何度もインストールしたことがあるが、毎回エンコーディングには悩まされてきた。今回は特に躓くこともなく、すんなりセットアップできた。手順をメモしておく。

事前にMariaDBを削除しておくこと、utf8mb4が追加になっていること、rootパスワードの暗号化方式(plugin)がデフォルトでcaching_sha2_passwordに変わったことは、経験的に知っていた。今回インストールしたものは、現時点の安定的なバージョン8.0.21である。新たに分かったこともいくつかある。

MySQLのインストール

MySQL のサイトからyumリポジトリを取得し、それからyum install mysql-serverを実行する流れになる。


# MariaDBがインストールされていたら削除する
$ yum remove mysql

# MySQLサイトからyumのリポジトリを取得する
$ cd Downloads 
$ wget
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql80-community-release-el8-1.noarch.rpm
$ ls
mysql80-community-release-el8-1.noarch.rpm

$ rpm -ivh mysql80-community-release-el8-1.noarch.rpm
$ ls /etc/yum.repo.d/mysql*         # リポジトリの追加を確認
mysql-community.repo
mysql-community-source.repo

# MySQLをインストールする
$ yum install mysql-server
$ mysql --version
mysql  Ver 8.0.21 for Linux on x86_64 (Source distribution)

my.cnfの修正

Javaから利用したいので、エンコーディングはutf8(小文字のハイフンなし)にする。すべての文字を4 バイトで扱うutf8mb4もあるが、処理が重いと想像されるので利用しない。utf8は言語に応じていて、日本語は原則3バイトであり、JIS第4水準の一部がサロゲートペア文字(例えば、吉野家の正しい「𠮷」や高橋さんの「髙」など)が4バイトになる。

以前のMySQLは、設定ファイルはmy.cnfの1つだけだったが、my.cnf.d/ をインクルードするように変更になっている。3つに分割されたことになる。特にmysql-default-authentication-plugin.cnf では、初期値でmysql_native_passwordが設定されている。これは最近の変更のようだ。初期のMySQL8.0では、rootのパスワード暗号化方式(plugin)がデフォルトでcaching_sha2_passwordになっていたが、非常に扱いにくく、多くの方が速攻でmysql_native_passwordに戻して利用していた。それを考慮してくれたのだろう。

$ ls /etc/my*
/etc/my.cnf
/etc/my.cnf.d:
client.cnf  mysql-default-authentication-plugin.cnf  mysql-server.cnf

$ vi /etc/my.cnf.d/client.cnf
[client]
default-character-set=utf8              # 追加する

$ vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
character-set-server=utf8               # 追加する

$ vi /etc/my.cnf.d/mysql-default-authentication-plugin.cnf
[mysqld]
default_authentication_plugin=mysql_native_password     # このままでよい

mysqldの起動とサービス登録

Firewallにmysqlサービスを追加しておく。3306/tcpポートを明示的に追加する必要はないようだ。そして、MySQLのサービス(mysqld)を起動する。またOSの起動時に自動でサービスを開始してくれるように設定しておく。


$ firewall-cmd --add-service=mysql --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client http mysql ssh vnc-server
  ports: 
  protocols: 

$ systemctl enable mysqld
$ systemctl start mysqld 
$ systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-11-25 17:12:59 JST; 19s ago
  Process: 43395 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 43265 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 43239 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 43350 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 24989)
   Memory: 424.9M

rootのパスワード変更

MySQLの簡易なツールmysql_secure_installationが提供されているので使ってみる。rootのパスワード設定も含まれている。途中、パスワード強度を選択する場面があるが、LOWを選択するとよい。


# mysql_secure_installation
...
Please set the password for root here.
New password: ***
Re-enter new password: ***
(※以降、選択項目がいくつかあるが割愛)

mysql_secure_installationを使わずにrootのパスワード設定する方法はいくつかあるが、skip-grant-tablesを利用する手順については、lower_case_table_names=1に変更する手順の「(4)rootのパスワードを設定する」に説明しているので参照のこと。

MySQLコンソールからの確認

MySQLにコンソールから接続して、操作してみる。


# mysql -uroot -p
Enter password: ***
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.21 Source distribution
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> show databases;
mysql> connect mysql;
mysql> show tables;
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | mysql_native_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

rootのpluginが、mysql_native_passwordになっている。OKだ。userテーブルにpasswordのカラムがなくなっている。認証回りがかなりいじられた感じだ。

以下、エンコーディングの確認である。期待どおりにすべてutf8に変更できている。OKだ。ただし、character_set_clientがutf8だと、GNOMEのMySQLコンソールから日本語入力できない問題があるので、注意のこと。(※CentOS8.2でMySQL8.0コンソールから日本語入力できない問題の回避策を参照)


mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

以上

../
0
3
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
3