【Mysql2】Mysql2::Error: Specified key was too long; max key length is 767 bytesの初歩的な解消方法

Mysql2に格納できる文字データは767バイトまで今はMysqlにマイグレーションできない?とのエラーが発生。
ユニコードの変更を行おうと思ったがどこのファイルに記述されているのか忘れてしまったので
備忘録として本記事を作成。

```terminal
○@○-MacBook ------ % rails db:migrate
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0307s
-- add_index(:users, :email, {: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
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/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
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/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/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/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)
````


解決策

```config/datebase.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4 #←ここの記述を変更。
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock
```

```config/datebase.yml
default: &default
  adapter: mysql2
  encoding: utf8 #←これに変更。
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock
```

rails db:rollbackを行い再びrails db:migrate
するとまた別のエラーが発生

```terminal
○@○MacBook ------ % rails db:migrate
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/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)
```

Usersテーブルが既に存在しているというエラーが発生。
この事象の修正に関しては一度テーブルを削除しまた作成を行うと直ると思い
rails db:migrate:resetを実行

```terminal
○@○MacBook ----- % rails db:migrate:reset
Dropped database '-----_development'
Dropped database '-----_test'
Created database '-----_development'
Created database '-----_test'
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0435s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0518s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0427s
== 20211015003423 DeviseCreateUsers: migrated (0.1382s) =======================
```

無事成功！

```terminal
user@USER-no-MacBook ------ % rails db:migrate:status

database: ------_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20211015003423  Devise create users
```

rails db:migrate:statusを行い無事migrateが行われている事も確認
DB上にUsersテーブルも確認できた。

無事解決:ok_hand: