#author("2016-04-19T14:51:28+09:00","default:wikiwriter","wikiwriter") &tag(MySQL/utf8mb4); *目次 [#x04c9e9a] #contents *関連ページ [#t0376644] *参考情報 [#u28e6ad3] *概要 [#vb7f5270] -MySQLで絵文字などを扱うための文字コード。 -[[MySQL :: MySQL 5.6 Reference Manual :: 10.1.10.7 The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding):http://dev.mysql.com/doc/refman/5.6/en/charset-unicode-utf8mb4.html]]によると、utf8プラスαの文字コードで、4バイトUTF-8文字コード以外は同じコード値になっているようだ。 -MySQLで絵文字を使用するにはテーブルの文字コードをutf8mb4に変更して、innodbのオプションも変更する。 *設定 [#m888b7c1] **キーの767バイト制限の回避 [#c10de758] -utf8だと1文字3バイトだけど、utf8mb4だと4バイトなので、主キーの制限767バイトを超えてしまうことがある。 ***my.cnfの変更 [#p8ccda1f] -これを回避する設定をmy.cnfに追加する(か、制限を超えないようテーブル定義を変更することもできる)。[[Rails を utf8mb4 で動かす | してみんとてす:http://3.1415.jp/mgeu6lf5]] #pre{{ innodb_file_per_table = 1 # 5.6.6 innodb_file_format = Barracuda innodb_large_prefix =1 innodb_default_row_format = DYNAMIC # >= 5.7.9 }} -終わったら再起動 ***config/initializers/ar_innodb_row_format.rbを追加。 [#of123370] -テーブル生成時のオプションを指定。 #pre{{ ActiveSupport.on_load :active_record do module ActiveRecord::ConnectionAdapters class AbstractMysqlAdapter def create_table_with_innodb_row_format(table_name, options = {}) table_options = options.merge(:options => 'ENGINE=InnoDB ROW_FORMAT=DYNAMIC') create_table_without_innodb_row_format(table_name, table_options) do |td| yield td if block_given? end end alias_method_chain :create_table, :innodb_row_format end end end }} ***テーブル設定の変更 [#ue7aaaf6] -さらにテーブルごとのROW_FORMATを変更する必要がある。 SELECT row_format from information_schema.tables where table_schema='データベース名'; ALTER TABLE テーブル名 ROW_FORMAT=DYNAMIC; ALTER TABLE テーブル名 CONVERT TO CHARACTER SET utf8mb4;