LoginSignup
0
1

More than 1 year has passed since last update.

[MySQL MariaDB] MySQLの文字コードをutf8mb4に変更しても絵文字を保存できない

Last updated at Posted at 2021-11-16
MySQLのデータベースに絵文字を追加する。

開発環境

Ubuntu 18.04 LTS, WSL 1, MariaDB 10.4

問題

MySQLののデータベースに絵文字を追加できるように文字コードをutf8mb4に変更したにもかかわらずエラーが発生。

エラー

trainingデータベースのpostsテーブルに絵文字を含むレコードを追加したところ、以下のエラーが出た。

MariaDB [training]> INSERT INTO posts (user_id, content) VALUES (4, "😀");
ERROR 1366 (22007): Incorrect string value: '\xF0\x9F\x98\x80' for column `training`.`posts`.`content` at row 1

文字コード(変更前)

文字コードを確認してみる。

MariaDB [(none)]> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

文字コード変更

絵文字を使うためには文字コードを3バイト文字コードutf8から4バイト文字コードutf8mb4にする必要がある。まず、「[MySQL]文字コードの設定を変更してutf8で統一する」を参考に/etc/mysql/mysql.conf.d/mysqld.cnfの最後に以下を追加する。

[mysqld]
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
/etc/mysql/mysql.conf.d/mysqld.cnf(追加後)
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size         = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_open_cache       = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#slow_query_log         = 1
#slow_query_log_file    = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size   = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
#以下追加
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4

文字コード(変更後)

文字コードを再度確認してみる。

MariaDB [training]> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

既存のデータベースは文字コードの変更を手動で行い、再度文字コード確認する。(参考:MySQLの文字コードをutf8mb4に変更)

MariaDB [training]> ALTER DATABASE training CHARACTER SET utf8mb
4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.002 sec)

MariaDB [training]> show variables like '%character%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

utf8mb4に変更できたので、再度絵文字を含むレコードを追加してみる。

MariaDB [training]> INSERT INTO posts (user_id, content) VALUES (4, "😀");
ERROR 1366 (22007): Incorrect string value: '\xF0\x9F\x98\x80' for column `training`.`posts`.`content` at row 1

また同じエラーが...

解決

SHOW CREATE TABLE tbl_nameを実行してみる。
これによって作成済みのテーブル情報を得ることができる。

MariaDB [training]> show create table posts;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table


|
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| posts | CREATE TABLE `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `content` text CHARACTER SET latin1 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

`content` text CHARACTER SET latin1 となっていることが確認できた。そこで、postsテーブルを指定して文字コードを変更する。

MariaDB [training]> ALTER TABLE posts CONVERT TO CHARACTER SET utf8mb4;
Query OK, 3 rows affected (0.121 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [training]> show create table posts;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table

                                                                |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| posts | CREATE TABLE `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `content` mediumtext DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.005 sec)

エラーが消えた。念のため表示してみる。

MariaDB [training]> SELECT * FROM posts;
+----+---------+-----------+
| id | user_id | content   |
+----+---------+-----------+
|  1 |       1 | Hello     |
|  2 |       1 | World     |
|  3 |       2 | I'm Paul. |
|  4 |       4 | 😀         |
+----+---------+-----------+

成功!!

まとめ

全体の文字コードばかりに注視していて、肝心の操作しているテーブルの文字コードの確認を見逃していた。既存のものに全体における設定変更が反映されているかどうかの確認を今後忘れず行おうと思う。データベース、テーブルそしてカラムを個別に文字コードが設定できることが分かっていれば早く解決できたかもしない。
(そもそもカラムのみの文字コード変更でよかったのでは)

参考

[MySQL]文字コードの設定を変更してutf8で統一する
MySQLの文字コードをutf8mb4に変更
SHOW CREATE TABLE 構文

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1