How to store emoji in MySQL?

How to store emoji in MySQL?

Problem reduction

When using erlang to store some special strings in MySQL, it cannot be read. After inspection, the binary format of these strings is as follows:

<<240,159,152,134,240,159,152,144>>
See what is the binary unicode?

14> unicode:characters_to_list(<<240,159,152,134,240,159,152,144>>).
[128518,128528]
15> erlang:integer_to_list(128518,16).%%View hex
"1F606"
16> erlang:integer_to_list(168528,).
"1F610"
Clearly, 1F606 and 1F610 exceed the expressive range of the common unicode string 0000-FFFF.

What do 1F606 and 1F610 stand for?

According to the instructions on the unicode official website, this is an emoji (emoji), and the specific display form is related to the manufacturer.

Reasons for storage failure on MySQL

At present , most MySQL is stored in utf8. Note that MySQL's utf8 is a little different from what we call traditional utf8. MySQL's utf8 encoding stipulates that it can only have a maximum of 3 bytes, and the unicode of emoji has exceeded 3 bytes, so MySQL's utf8 cannot recognize and store emoji.

To this end, you can refer to the information on unicode support in MySQL, from which you can know that MySQL supports utf8mb4 after 5.5, this encoding supports 1 to 4 bytes, and this encoding can represent emoji.

In addition, utf8mb4 is backward compatible with utf8. Solution

Change the corresponding fields in MySQL to utf8mb4 encoding: ALTER TABLE t1   DEFAULT CHARACTER SET utf8mb4,   MODIFY col1 CHAR(10)     CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,   MODIFY col2 CHAR(10)     CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL; will The connection code of the connection library is changed to utf8mb4. The following Brothers (www.lampbrother.net) takes the connection library emysql of erlang as an example: emysql:add_pool(pool,[{size,1},{user,"user"},{ password,"password"},{host,"host"},{port,3306},{encoding,utf8mb4}]). %%or emysql:add_pool(pool,1,"user","password","host ",3306,undefined,utf8mb4).















For the convenience of table building, you can modify my.cnf:

# Server character set
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server=utf8mb4

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654648&siteId=291194637