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