Rails&AWSで絵文字(utf8mb4)の対応手順
1.Rails側のconbfig/database.ymkで以下のように設定変更。
default: &default
charset: utf8mb4
encoding: utf8mb4
collation: utf8mb4_general_ci
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /tmp/mysql.sock
**以下省略**
2.config/initializers/utf8mb4.rb というファイルを作り以下を記述
module Utf8mb4
def create_table(table_name, options = {})
table_options = options.merge(options: 'ENGINE=InnoDB ROW_FORMAT=DYNAMIC')
super(table_name, table_options) do |td|
yield td if block_given?
end
end
end
ActiveSupport.on_load :active_record do
module ActiveRecord::ConnectionAdapters
class AbstractMysqlAdapter
prepend Utf8mb4
end
end
end
3.サーバー側(AWS)でMySQLの設定変更。/etc/my.cnf に以下を記述。
[client]
default-character-set = utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8mb4
skip-character-set-client-handshake
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init-connect = SET NAMES utf8mb4
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
だれかのためになれば幸いです。
たれみみ(@taremimi_7)