[RAILS] [MySQL] The database server built with docker may not support Japanese ...

environment

python


$ docker -v
Docker version 19.03.12

$ rails -v
Rails 6.0.3.2

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.29, for osx10.15 (x86_64) using  EditLine wrapper

When I try to register a user name in Japanese in the database, ...

error contents


ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: '\xE3\x83\x8F\xE3\x83\xAA...' for column 'name' at row 1):

Oh, it seems that it is not recognized as Japanese Do you want to check the settings by looking at the charaset of the database?

python


$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
f51c4b953b05        feeder-in_back      "rails server -b 0.0…"   About an hour ago   Up About an hour    0.0.0.0:3000->3000/tcp              back
925b28f3b58d        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Up About an hour    33060/tcp, 0.0.0.0:3308->3306/tcp   database
ab6efeba59a1        feeder-in_web       "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:8080->3000/tcp              web
$ docker exec -it 925b28f3b58d mysql -u root -p

mysql> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.03 sec)

character_set_database | latin1 As far as I can see, it is the default charaset of MySQL, and it does not support Japanese.

Make MySQL database server support Japanese

docker-compose.yml


command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

python


$ docker-compose down

$ docker-compose up -d --build

Database rebuild

database.yml


default: &default
  charset: utf8mb4   #Postscript
  encoding: utf8mb4  

python


$ docker-compose exec back sh
> rake db:drop

> rake db:create
> rails db:migrate

Now you have a database that supports Japanese!

Recommended Posts

[MySQL] The database server built with docker may not support Japanese ...
Easy JSON database experience with Docker ~ Try the latest version of Couchbase Server
[Docker] Connection with MySQL
When the server does not start with rails s
Web application built with docker (1)
If the DB container does not start with Initializing database files
I tried to build the environment of PlantUML Server with Docker
Rails + MySQL environment construction with Docker
Run SQL Server with Docker ToolBox
Read dump file with Docker MySQL
[Environment construction with Docker] Rails 6 & MySQL 8
mysql doesn't start up with docker.
Update MySQL from 5.7 to 8.0 with Docker
I built a rails environment with docker and mysql, but I got stuck
How to interact with a server that does not crash the app
After all I wanted to preview the contents of mysql with Docker ...