Mac OS上に MySQL8.0 をインストールする

## 概要
Mac OS上に MySQL8.0 をインストールします。

##環境
* Mac OS X Sierra 10.12.6
* MySQL 8.0

##インストール手順

Homebrewでインストールします。

###Homebrewをアップデート

```
$ brew update
```

###インストールするMySQLのバージョンを確認

```
$ brew info mysql
mysql: stable 8.0.11 (bottled)
```

バージョン `8.0.11` がインストールされます。


###MySQLのインストール

```
$ brew install mysql

...

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
```

上記のメッセージが表示されます。意味を確認すると

* セキュリティに関する設定をするために、　`mysql_secure_installation` を実行する必要がある
* MySQLの起動の仕方について
    * `launchd` を使用する場合は、 `brew services start mysql` を実行
    * `launchd` を使用しない場合は、 `mysql.server start` を実行


###バージョンの確認

```
$ mysql --version
$ mysql  Ver 8.0.11 for osx10.13 on x86_64 (Homebrew)
```

###MySQLを起動

今回は、 `launchd` は使用しないので、以下のコマンドを実行

```
$ mysql.server start
Starting MySQL
. SUCCESS!
```

###rootユーザでログイン

```
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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>
```

`exit` でログアウトできます。

###セキュリティの設定

```
$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN 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 plugin?

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: 1
Please set the password for root here.

New password:

Re-enter new password:

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) : 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!
```

### パスワードを使って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: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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.
```

###設定ファイルの編集

```
$ vi /usr/local/etc/my.cnf
```

以下の内容を追記します。

```
character-set-server=utf8mb4
collation-server=utf8mb4_bin
skip-character-set-client-handshake
default-storage-engine=InnoDB
innodb_default_row_format=DYNAMIC
default_password_lifetime = 0

[mysql]
auto-rehash
default-character-set = utf8mb4

[mysqldump]
default-character-set = utf8mb4
```

設定内容の詳細については、
[Vagrant Amazon Linux にMySQL5.7をインストールする](https://qiita.com/kobayashi-m42/items/802ca07e3450d4658ae3#%E8%A8%AD%E5%AE%9A%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E7%B7%A8%E9%9B%86)の**設定ファイルの編集**を参照ください。

※MySQL5.7からMySQL 8.0への変更で廃止になったパラメータがあります。
※今回は、slow_queryに関する設定は行っていません。

###MySQLを再起動

```
$ mysql.server restart
Shutting down MySQL
.. SUCCESS!
Starting MySQL
. SUCCESS!
```

###文字コードの設定が反映されているか確認

MySQLにログインし、設定が反映されているか確認します。

```
mysql> show variables like 'character_set%';
+--------------------------+------------------------------------------------------+
| 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/local/Cellar/mysql/8.0.11/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------+
```

###MySQLを停止

```
$ mysql.server stop
Shutting down MySQL
.. SUCCESS!
```