Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://suifu.blog.51cto.com/9167728/1847087
The company has new requirements, iOS client to go online comments can use emoji expression function, before MySQL 5.5, UTF-8 encoding only support 1-3 bytes, starting from MySQL 5.5, can support 4 byte UTF encoding utf8mb4, one character can support more character sets , you can also support more emoji.
UTF8MB4 is compatible with UTF8 and can represent more characters than UTF8, which is a superset of the UTF8 character set. So now some new business, such as the emoji expression in ISO, sets the character set of the MySQL database to UTF8MB4.
First look at the question:
caused By:java.sql.SQLException:Incorrect string value: ' \xf6\x9d\x98\x84 ' for column ' comment ' at row 1 at Com.mysql . jdbc. Sqlerror.createsqlexception (sqlerror.java:1074) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4096) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4028) at Com.mysql.jdbc.MysqlIO.sendCommand (Mysqlio.java : 2490) at Com.mysql.jdbc.MysqlIO.sqlQueryDirect (mysqlio.java:2651) at Com.mysql.jdbc.ConnectionImpl.execSQL ( connectionimpl.java:2734) at Com.mysql.jdbc.PreparedStatement.executeInternal (preparedstatement.java:2155) at Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:2458)
If we set the column comment to varchar (100), which is used to store the comment information, and now the new function on the line to store emoji expression, insert emoji expression will report the above error, UTF-8 encoding may be two, three, four bytes. The emoji expression is 4 bytes, while MySQL UTF8 encodes up to 3 bytes, so data cannot be plugged in. UTF8MB4 is compatible with UTF8 and can represent more characters than UTF8.
Solution: Convert the MySQL encoding from UTF8 to UTF8MB4.
Online article uncompromising, this article on the production of real-world parameters to record
The overall operation process is not difficult
One: First we modify the MY.CNF parameter
12345678910111213 |
[client]
default-character-
set
=utf8mb4
[mysql]
default-character-
set
=utf8mb4
[mysqld]
character-
set
-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect =
‘SET NAMES utf8mb4‘
character-
set
-client-handshake =
false
|
Second: Character set modification of database related tables
Convert the database to UTF8MB4
1 |
mysql> ALTER DATABASE erp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
Convert the already built tables to UTF8MB4
1 |
mysql> ALTER TABLE `erp_comment` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
Set the type of field you want to use emoji as:
1 |
mysql> ALTER TABLE `erp_comment` MODIFY COLUMN `comment` varchar (100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
Three: Restart the database server for it to take effect
123 |
[[email protected] ~] # /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.... SUCCESS! |
Four: Login database check whether the following:
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 |
+--------------------------+--------------------+
Rows in Set (0.00 sec)
Special instructions under: Collation_connection/collation_database/collation_server If it is utf8mb4_general_ci, no relationship. But we must ensure that character_set_client/character_set_connection/character_set_database/character_set_results/character_set_ Server is UTF8MB4.
Four: Let the development side of the POM configuration, remove the characterencoding parameter, and recompile
If you are using a Java server, upgrade or make sure that your MySQL connector version is higher than 5.1.13, or you will still not be able to use UTF8MB4
Finally, let the front-end application insert emoji expression, you can.
Some small knowledge points
where Character-set-server and collation-server these settings for the UTF8MB4 character set is relatively easy to understand, that is, the MySQL database related to the character sets are set to UTF8MB4;
However, in order to implement the client UTF8 connection to MySQL, the use of the UTF8MB4 character set, the Mysqld configuration is configured in the init_connect= ' set NAMES utf8mb4 ' means that the initialization connection is set to the UTF8MB4 character set, and then configure a Skip-character-set-client-handshake = True ignores the client character set settings, regardless of the client's character set, and is used in accordance with the settings in Init_connect, thus satisfying the needs of the application.
This article is from the "Age volt" blog, please make sure to keep this source http://suifu.blog.51cto.com/9167728/1847087
Let MySQL support emoji emoticons