MySQL added this UTF8MB4 code after version 5.5.3, Mb4 is the most bytes 4 meaning, specifically for compatibility with four-byte Unicode. In fact, UTF8MB4 is a superset of UTF8, theoretically using UTF8, and then modifying the character set to UTF8MB4, will not have any problems with the existing UTF8 encoded reads.
Small program of the project, found to save the user information nickname times wrong, looking for reasons to find that some users nickname is with emoji emoji, and the database table property settings at that time character encoding or UTF8, the database will of course error. After searching for some information on the Internet, we found the complete solution:
1. Modify the Database,table,column character set:
- ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
- ALTER TABLE table_name CONVERT to CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- ALTER TABLE table_name Change column_name VARCHAR (191) CHARACTER SET utf8mb4 COLLATE utf8mb4_ Unicode_ci;
2. Edit the My.cnf file and add the following to the corresponding section:
- [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 the MySQL service to
How to save emoji special characters in the MySQL database