Rails + MySQL + utf8mb4

# 目的
MySQLでutf8mb4を使えるようにし、データベースおよびテーブルを作成する。

# テーブル作成コマンド実行 & エラー内容
`rails db:create` が通った後から

```terminal:terminal
$ rails db:migrate
== 20180625084831 DeviseCreateUsers: migrating ================================
-- create_table(:users, {:unsigned=>true})
   -> 0.0301s
-- add_index(:users, :uid, {:unique=>true})
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `index_users_on_uid`  ON `users` (`uid`)
/Users/mosamosapoodle/hoge_app/db/migrate/20180625084831_devise_create_users.rb:25:in `change'
/Users/mosamosapoodle/hoge_app/bin/rails:9:in `<top (required)>'
/Users/mosamosapoodle/hoge_app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `index_users_on_uid`  ON `users` (`uid`)
/Users/mosamosapoodle/hoge_app/db/migrate/20180625084831_devise_create_users.rb:25:in `change'
/Users/mosamosapoodle/hoge_app/bin/rails:9:in `<top (required)>'
/Users/mosamosapoodle/hoge_app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Mysql2::Error: Specified key was too long; max key length is 767 bytes
/Users/mosamosapoodle/hoge_app/db/migrate/20180625084831_devise_create_users.rb:25:in `change'
/Users/mosamosapoodle/hoge_app/bin/rails:9:in `<top (required)>'
/Users/mosamosapoodle/hoge_app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
```

# 解決
/etc/my.cnfを開き（または新規作成し）、以下のように記述します。

```terminal:terminal
$ sudo vim /etc/my.cnf
```

入力：`etc + i`

```/etc/my.cnf
[mysqld]
  character-set-server=utf8mb4

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

入力：`etc + wq!`

その後、以下のコマンドで MySQL サーバーを再起動します。

```terminal:terminal
$ mysql.server restart
```

# 確認 & エラー内容
```terminal:terminal
$ mysql -u root

mysql> SHOW VARIABLES LIKE "chara%";
ERROR 1146 (42S02): Table 'performance_schema.session_variables' doesn't exist
```

mysqlのアップデートをしたことによるエラーの様子。
設定を反映させ、mysqlを再起動する。

```terminal:terminal
$ mysql_upgrade -u root -p --force
$ mysql.server restart
```

再確認。

```terminal:terminal
$ mysql -u root

mysql> SHOW VARIABLES LIKE "chara%";
+--------------------------+------------------------------------------------------+
| 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/5.7.20/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------+
```

# テーブル作成コマンド実行

```terminal:terminal
$ rails db:migrate
```

通った！
以上。

# 参考
http://nekonenene.hatenablog.com/entry/2016/08/02/125424
http://sweep3092.hatenablog.com/entry/2015/11/28/180103