CentOS 7 に MySQL 5.7 を yum インストールして初期設定までやってみた

CentOS 7.2 上に MySQL 5.7 を yum インストールして、初期設定まで行ったので、その手順を記していきたいと思います。

はじめに

CentOS 7 よりデータベースサーバの MySQLMariaDB に置き換えられました。

もしすでに MariaDB がインストールされている場合はこれからインストールする MySQL と競合を起こさないように削除しましょう。

$ sudo yum remove mariadb-libs
$ sudo rm -rf /var/lib/mysql

これで MariaDB 本体とデータフォルダを削除できました。

yum リポジトリの追加

CentOS 7 に MySQL 公式の yum リポジトリを追加します。

$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

MySQL のインストール

MySQL 公式の yum リポジトリが追加できたので、yum install コマンドでインストールしましょう。

$ sudo yum install mysql-community-server

バージョンを確認します。

$ mysqld --version
mysqld  Ver 5.7.14 for Linux on x86_64 (MySQL Community Server (GPL))

無事 MySQL 5.7 がインストールできました!

MySQL の起動

MySQL がインストールできたので、早速起動させましょう。

$ sudo systemctl start mysqld.service

自動起動の設定も行っておきます。

$ sudo systemctl enable mysqld.service

MySQL の初期パスワード確認

MySQL 5.7 では、初回起動時に初期パスワードが生成されるので、ログファイルから生成されたパスワードを確認する必要があります。

$ $ sudo cat /var/log/mysqld.log | grep password
2016-09-01T13:09:03.337119Z 1 [Note] A temporary password is generated for root@localhost: uhsd!Aid;3Zt

今回の場合、uhsd!Aid;3Zt の部分が初期パスワードになります。

MySQL のセキュリティ設定

mysql_secure_installation コマンドを実行して、MySQL のセキュリティ設定を行っていきます。

対話形式なので、比較的簡単に最低限必要と考えられるセキュリティの設定ができます。

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: # ログファイルから取得した初期パスワードを入力します

The existing password for the user account root has expired. Please set a new password.

New password: # root ユーザの新規パスワードを入力します

Re-enter new password: # 確認用にもう一度入力します
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((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 # ローカルホスト以外からアクセス可能な root アカウントを削除
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 # test データベースの削除
 - 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!

MySQL へのログイン確認

さきほど設定した新しいパスワードでログインできるかどうか確認します。

$ mysql -u root -p
Enter password: # 新しいパスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

設定変更

MySQL 5.7 からパスワードの有効期限がデフォルトで360日になり、360日を経過するとパスワードの変更を促されて MySQL にログインできなくなります。

default_password_lifetime の値を 0 に変更して、この有効期限の設定を無効にしたいと思います。

また、デフォルトの文字コードUTF-8 に変更します。

  • /etc/my.cnf
[mysqld]
.
.
.
character-set-server = utf8
default_password_lifetime = 0

MySQL を再起動させ、設定内容を反映させます。

$ sudo systemctl restart mysqld.service

以上で設定は完了となります。

まとめ

CentOS 7 に MySQL 5.7 を yum インストールし、最低限の設定を行いました。

本番環境で運用する際は、チューニングも含めて、さらに細かい設定が必要になってくると思いますが、サービスに合わせて適切な設定を考えていきたいと思います。

詳解MySQL 5.7 止まらぬ進化に乗り遅れないためのテクニカルガイド (NEXT ONE)

詳解MySQL 5.7 止まらぬ進化に乗り遅れないためのテクニカルガイド (NEXT ONE)

基礎からのMySQL 第3版 (基礎からシリーズ)

基礎からのMySQL 第3版 (基礎からシリーズ)