Some time ago, the project needs to be stored in the database emoji, due to the wrong encoding format, directly lead to database error, and then modify the MySQL code, it solved
Emoji symbols are actually text, not pictures, they are only displayed as pictures
Supported in mysql5.5.3 or later versions
After you have determined that the database supports store emoticons, you can modify the default encoding for the database so that you don't have to consider the problem of saving emoji if you build the database later.
In the MySQL configuration file my.cnf or My.ini configuration file, modify the following:
[ client default - - set = utf8mb4 [ mysqld character - set - server = utf8mb4collation - server Span style= "color: #808080;" >= utf8mb4_general_ci
Restart MySQL, and then use the following command to view the encoding, which should be all utf8mb4
(except Character_set_filesystem and Character_set_system):
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-5.7....|+--------------------------+--------------------------+8Rowsinch Set(0.00Sec
Use the command to view the collation settings, which should all be utf8mb4_general_ci
:
Mysql>Show variables like '%coll%';+----------------------+--------------------+|Variable_name|Value|+----------------------+--------------------+|Collation_connection|Utf8mb4_general_ci||Collation_database|Utf8mb4_general_ci||Collation_server|Utf8mb4_general_ci|+----------------------+--------------------+3Rowsinch Set(0.01Sec
If character_set_database
It is utf8
, you need to restart the server.
Now, MySQL will be able to store the emoji characters correctly.
But what if it's a database that's been built before?
You can use the MySQL command
ALTER table name DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
To change the encoding of existing database tables
Reference Address: https://www.liaoxuefeng.com/article/00145803336427519ae82a6c5b5474682c0c4ba5b47fb33000
MySQL Storage emoji issues