【CentOS7】MySQL8のインストール

CentOS7からコマンドも大幅に変更になりましたが、
デフォルトのデーターベースもMySQLからMariaDBへ変更になりましたので、
CentOS7でMySQLを使いたい場合は6以前に比べ、一手間かかります。

MariaDBのアンインストール

MariaDBが入っている場合はアンインストール。
アンインストール
# yum -y remove mariadb-libs
# rm -rf /var/lib/mysql

MySQLのインストール

最新のリポジトリはこちらで確認。
https://dev.mysql.com/downloads/repo/yum/
2019年5月6日現在、mysql80-community-release-el7-3.noarch.rpm が最新の模様。
リポジトリの追加
# rpm -Uvh http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
インストール
# yum -y install --enablerepo=mysql80-community mysql-community-server
バージョンの確認
# mysql --version mysql Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)

MySQLの設定(2020/05/25修正)

MySQL8から文字コードはデフォルトで utf8なので特に設定する必要は無い様です。
とりあえず最低限の設定。
設定ファイルの修正
# vi /etc/my.cnf
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server = utf8 ←最終行へ追加

MySQLの起動

起動と自動起動設定
# systemctl start mysqld
# systemctl enable mysqld
rootのパスワードを確認
# grep 'temporary password' /var/log/mysqld.log
下記の様にrootのパスワードが表示される。

表示されたパスワードを記載してMySQLに接続。
-pの後のパスワードなど、記号などが含まれている文字を設定する際は、 ' (シングルクオーテーション)で囲う。
MySQLの接続
# mysql -uroot -p'h(&qAOAay60a'
こちらでも同じ。
# mysql -uroot -p Enter password: ←パスワード入力
rootユーザのパスワードを「1q2w3e4r」に変更しようとすると、
mysql> ALTER USER root@localhost IDENTIFIED BY '1q2w3e4r'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
となり、パスワードをもっと複雑にしろと怒られるので、今回はローカルの開発環境、テスト環境なので、条件をかなり緩和する。
パスワード設定の条件緩和
mysql> SET GLOBAL validate_password.length=4; Query OK, 0 rows affected (0.00 sec) mysql> SET GLOBAL validate_password.policy=LOW; Query OK, 0 rows affected (0.00 sec)
rootユーザのパスワードを「1q2w3e4r」に再度設定。
パスワードの変更
mysql> ALTER USER root@localhost IDENTIFIED BY '1q2w3e4r'; Query OK, 0 rows affected (0.82 sec)
MySQL5.7以前のバージョンでは
mysql> SET GLOBAL validate_password_length=4; mysql> SET GLOBAL validate_password_policy=LOW;
現在のデーターベースを確認してみる。
データーベースの確認
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (3.40 sec)
ここではユーザ名「xiohei」を、全てのデーターベースにローカルホスト内からパスワード「1q2w」でアクセスできるように作成。
MySQL8からはCREATE USERを使う。
ユーザの追加
mysql> CREATE USER xiohei@localhost IDENTIFIED BY '1q2w'; mysql> GRANT ALL PRIVILEGES ON *.* TO xiohei@localhost WITH GRANT OPTION;
MySQL5.7以前のバージョンでは
GRANT ALL PRIVILEGES ON *.* TO xiohei@localhost IDENTIFIED BY '1q2w';
接続の確認
# mysql -uxiohei -p1q2w mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 46 Server version: 8.0.16 MySQL Community Server - GPL Copyright (c) 2000, 2019, 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>

(2020/05/25追記)
コマンド mysql_secure_installation を使った方が、
rootのパスワード設定は楽そうです。
初期設定
# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: 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                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!