MySQL Database store emoji emoticons

Source: Internet
Author: User

MySQL UTF8 encodes a character up to 3 bytes, but a emoji expression is 4 bytes, so UTF8 does not support storing emoji emoticons. However, the UTF8 superset utf8mb4 can have up to 4 bytes in a character, so it can supportthe storage of e-Moji emoticons .

Minimum supported version is 5.5.3+

Based on most of the tutorials on the web, the encoding format is always saved unsuccessfully.

Finally found a lot of information to solve.

First set up the database format

Server-side

Modify the database configuration file /etc/my.cnf , add the following configuration, and then restart the server:

[Mysqld]character-set-server=utf8mb4collation_server=utf8mb4_unicode_ciinit-connect = "SET NAMES utf8mb4" [MySQL]default-character-set=utf8mb4

After restarting, log in to MySQL and you can show variables like ‘character%‘; see if the encoding has been modified successfully. The success of the modification should look similar to the following:

mysql> show variables like ‘%char%‘;+--------------------------+----------------------------------+| 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/local/mysql/share/charsets/ |+--------------------------+----------------------------------+8 rows in set (0.00 sec)

Then modify the corresponding database table encoding to UTF8MB4:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8mb4;

All settings are still not in the emoji expression.

Finally, when the database is connected, a SQL statement is executed beforehand, and the result can be saved successfully.

I'm a database source.

New Basicdatasource (); ListNew arraylist<string>(); List.add ("SET NAMES ' utf8mb4 '"); DATASOURCE.SETCONNECTIONINITSQLS (list);

MySQL Database store emoji emoticons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.