Common Unicode characters that require the utf8mb4 charset are Emojis.
Utf8mb3 supports only characters in the Basic Multilingual Plane (BMP).
Utf8mb4 Supports BMP and supplementary characters. It requires a maximum of four bytes per multibyte character, this may increase your overall data storage space requirements.
It is recommended to use utf8mb4 for columns that are used to store emojis. Lot of performance improvements were made in MySQL 5.7.x and 8.0.x. It is the default charset starting MySQL8.0.x.
MySQL requires that you use the utf8mb4 charset to hold 4-byte Unicode data.
You will need to modify your schema to use the utf8mb4 charset and alter the LOAD data
command to have CHARACTER SET utf8mb4
.
mysql> show global variables like’character%’;
look at the current character values and change them to utf8mb4.
mysql> set global character_set_client='utf8mb4'
mysql> set global character_set_connection='utf8mb4';
mysql> set global character_set_database='utf8mb4';
mysql> set global character_set_results='utf8mb4';
mysql> set global character_set_server='utf8mb4';
Note: To make sure these changes are permanent, you need to update your MySQL config, both [client]
and [mysqld]
sections.
Possible Issues:
Common Issue while storing emojis in MySQL
1)
Error Code: 1366. Incorrect string value: '\xF0\x9F\x94\x8D' for column.
Change the tables/columns that needs emoji support to use proper character and collation:
ALTER TABLE <tbl_name> CONVERT TO CHARACTER SET utf8mb4 COLLATE collation_name;
Each character set has one or more collations, but each collation is associated with one and only one-character set. So, please refer to the documentation for the specific collation types that matches your charset based on your data needs.
Examples:
utf8mb4_unicode_ci;
utf8mb4_bin;
2)
ERROR 3140 (22032): Invalid JSON text: "Invalid escape character in the string."
While loading JSON data.
The MySQL parser will handle escape codes by default, so if your JSON data contains escapes, you need to disable it from handling the escapes.
mysql> insert into test values ('{"MN1":"\\value", "MN2": 11}');
ERROR 3140 (22032): Invalid JSON text: "Invalid escape character in string." at position 8 in value for column 'test.json'.
In this case, you must enable the NO_BACKSLASH_ESCAPES
SQL MODE so that the parser does not handle the escape codes but allows the JSON parser to process them.
If you are using LOAD DATA
to import JSON, you need to add FIELDS ESCAPED BY ''
to the command so that escape sequence handling is disabled when parsing the import file.
3) LOAD DATA INFILE
is strict in accepting UTF8mb4 strings, Throws Error
1300: "Invalid utf8mb4 character string"
The problem is that the character is being interpreted as the wrong character set. A client/server charset handshake issue.
When inserting the string, MySQL needs to know the originating character set.
We could use the following to specify the character set.
SET NAMES utf8mb4
LOAD DATA INFILE ... CHARACTER SET utf8mb4.
Here are some details about upgrading to MySQL 5.6