How to enable MySQL to store emoji characters

Source: Internet
Author: User
Tags mysql code
This article mainly introduces how to enable MySQL to store emoji characters. The key is utf8mb4 character set settings. For more information, see MySQL to support emoji versions later than 5.5.3, the character set must be set to utf8mb4.


What is the difference between utf8mb4 and utf8? In the past, mysql utf8 had a maximum of 3 characters, while utf8mb4 was extended to a single character with a maximum of 4 bytes. therefore, more character sets are supported.

Converts the Mysql code from utf8 to utf8mb4.

Required> = MySQL 5.5.3 and slave database must also be 5.5. earlier versions do not support this character set or duplicate error.

Stop the MySQL Server service

Modify my. cnf or mysql. ini

[client] default-character-set = utf8mb4[mysql] default-character-set = utf8mb4[mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4'

Restart MySQL Server and check the character set.

View Server character set settings

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character%' OR Variable_name LIKE 'collation%';

+--------------------------+--------------------+| 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        || collation_connection   | utf8mb4_unicode_ci || collation_database    | utf8mb4_unicode_ci || collation_server     | utf8mb4_unicode_ci |+--------------------------+--------------------+

View database character sets

mysql> select * from SCHEMATA where SCHEMA_NAME='ttlsa';

+--------------+-------------+----------------------------+------------------------+----------+| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |+--------------+-------------+----------------------------+------------------------+----------+| def     | ttlsa   | utf8mb4          | utf8mb4_unicode_ci   | NULL   |+--------------+-------------+----------------------------+------------------------+----------+

View Table character sets

mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.TABLES;

+--------------------+----------------------------------------------------+--------------------+| TABLE_SCHEMA    | TABLE_NAME                     | TABLE_COLLATION  |+--------------------+----------------------------------------------------+--------------------+

View column character sets

mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from COLUMNS;

+--------------------+----------------------------------------------------+--------------------------------------------+--------------------+| TABLE_SCHEMA    | TABLE_NAME                     | COLUMN_NAME                | COLLATION_NAME   |+--------------------+----------------------------------------------------+--------------------------------------------+--------------------+

Character Set conversion statement

use information_schema;SELECT concat("ALTER DATABASE "`,table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schema;SELECT concat("ALTER TABLE "`,table_schema,"`."`,table_name,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schema, table_name;SELECT concat("ALTER TABLE "`,table_schema,"`."`,table_name, "` CHANGE "`,column_name,"` "`,column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('varchar');SELECT concat("ALTER TABLE "`,table_schema,"`."`,table_name, "` CHANGE "`,column_name,"` "`,column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('text','tinytext','mediumtext','long


The above is how to enable MySQL to store emoji characters. tutorial _ MySQL. For more information, see PHP Chinese website (www.php1.cn )!



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.