MySQLのインストール

MySQLの最新バージョンを入れたメモ


# 前提条件
|OS・ミドルウェア|バージョン|
|:-----------|:-------|
|CentOS|CentOS Linux release 7.9.2009 (Core)|
|Ubuntu|20.04.3 LTS (Focal Fossa)|
|MySQL|MySQL 8.0.27|

# 手順

## CentOS の場合

1. MySQLの公式RPMパッケージをサーバにダウンロードして配置する

    ```
    # curl -LkvOf https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
    ```

2. 破損がないか確認する

    ```
    # md5sum mysql80-community-release-el7-1.noarch.rpm 
    739dc44566d739c5d7b893de96ee6848  mysql80-community-release-el7-1.noarch.rpm
    ```

3. リポジトリを登録する

    ```
    # rpm -Uvh mysql80-community-release-el7-1.noarch.rpm
    ```

4. MySQLリポジトリを確認する

    ```
    # yum repolist all | grep mysql
    ```

5. 有効になっているMySQLリポジトリを確認する

    ```
    # yum repolist enabled | grep mysql
    mysql-connectors-community/x86_64       MySQL Connectors Community           51
    mysql-tools-community/x86_64            MySQL Tools Community                63
    mysql80-community/x86_64                MySQL 8.0 Community Server           17
    ```

6. MySQLをインストールする

    ```
    # yum -y install mysql-community-server
    ```

7. MySQLサービスを起動する

    ```
    # systemctl start mysqld.service
    ```

8. MySQLサービスが起動していることを確認する

    ```
    # systemctl status mysqld.service
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2018-07-16 04:40:38 UTC; 39s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 3096 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 3164 (mysqld)
       Status: "SERVER_OPERATING"
       CGroup: /system.slice/mysqld.service
               └─3164 /usr/sbin/mysqld

    Jul 16 04:40:32 localhost.localdomain systemd[1]: Starting MySQL Server...
    Jul 16 04:40:38 localhost.localdomain systemd[1]: Started MySQL Server.
    ```

9. 一時パスワードを確認する

    ```
    # grep 'temporary password' /var/log/mysqld.log
    2018-07-16T04:40:34.782261Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <ここに一時パスワードが表示される>
    ```

8. rootユーザでログインする

    ```
    # mysql -u root -p
    Enter password: <先ほど表示された一時パスワードを入力する>
    ```

9. rootユーザのパスワードを変更する

    ```
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '<変更したいパスワード>';
    ```
設定するパスワードの条件は以下
    1. 少なくとも１文字は大文字と小文字、数字、特殊文字を含む
    2. 全体の長さが８文字以上

10. MySQLのセキュアインストール実行

    ```
    # mysql_secure_installation
    
    Securing the MySQL server deployment.
    
    The 'validate_password' component is installed on the server.
    The subsequent steps will run with the existing configuration
of the component.
    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) : ←何も入力せずEnterを押す
    
    ... skipping.
    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!
    ```

##  Ubuntu の場合

1. `apt` コマンドでインストールする

    ```
    $ sudo apt install mysql-server
    ```

2. セキュアインストールスクリプトを実行する

    ```
    $ sudo 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: <パスワードの妥当性チェック用のコンポーネントを有効にするかどうか入力>
    
    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: <パスワード強度を入力>
    Please set the password for root here.
    
    New password: <root ユーザーの新規パスワード>
    
    Re-enter new password: <root ユーザーの新規パスワードを再度入力>
    
    Estimated strength of the password: 100 
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : <root ユーザーの新規パスワードで続行するかどうか>
    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) : <匿名ユーザーを削除するかどうか>
    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) : <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) : <テストデータベースを削除するかどうか>
     - 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) : <テーブル再読み込みするかどうか>
    Success.
    
    All done! 
    ubuntu@wordpress:~$ 
    ```

これでひと段落

# 参考
- [A Quick Guide to Using the MySQL Yum Repository | 公式サイト](https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/)
- [A Quick Guide to Using the MySQL APT Repository | 公式サイト](https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/)
- [MySQL Community Downloads | 公式サイト](https://dev.mysql.com/downloads/)
- [2.10 インストール後のセットアップとテスト | 公式サイト ](https://dev.mysql.com/doc/refman/8.0/ja/postinstallation.html)
- [4.4.2 mysql_secure_installation — Improve MySQL Installation Security | 公式サイト](https://dev.mysql.com/doc/refman/8.0/en/mysql-secure-installation.html)
- [Ubuntu 20.04にMySQLをインストールする方法](https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04-ja)