Make MySQL support emoji

 

http://www.cnblogs.com/suifu/p/5848269.html

 

The company has new requirements. The ios client will be able to use the emoji function in comments. Before MySQL 5.5, UTF-8 encoding only supports 1-3 bytes; starting from MySQL 5.5, it can support 4-byte UTF encoding utf8mb4 , a character can support more character sets and more emojis.

 

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 services, such as emoji expressions in ISO, will set the character set of MySQL database to utf8mb4.

 

 

Look at the problem first:

 

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 comment information, and now the new function is online to store emoji expressions, inserting emoji expressions will report the above error, UTF-8 encoding may be two, three, four bytes. Emoji expressions are 4 bytes, and Mysql's utf8 encoding is up to 3 bytes, so the data cannot be inserted. utf8mb4 is compatible with utf8 and can represent more characters than utf8.

Solution: Convert Mysql encoding from utf8 to utf8mb4.

 

 

Articles on the Internet hold their own opinions. This article records the parameters that are actually available in the production environment.

 

The overall operation process is actually not difficult

 

 

One: First, we modify the my.cnf parameters

 

[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

 

Two: Modify the character set of the database-related tables

Convert database to utf8mb4

mysql> ALTER DATABASE erp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

Convert the already built table to utf8mb4

mysql>ALTER TABLE `erp_comment` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

Type the fields that need to use emoji as:

mysql>ALTER TABLE `erp_comment` MODIFY COLUMN `comment`  varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

Three: Restart the database server to make it take effect

[root@HE3 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.... SUCCESS!

 

 

Four: Log in to the database and check whether it is as follows:

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 note: collation_connection/collation_database/collation_server If it is utf8mb4_general_ci, it does not matter. But must ensure that character_set_client/character_set_connection/character_set_database/character_set_results/character_set_server is utf8mb4.

 

Four: Let the pom configuration on the development side, remove the characterEncoding parameter, and recompile

If you are using java server, upgrade or make sure your mysql connector version is higher than 5.1.13, otherwise you still cannot use utf8mb4

 

Finally, let the front-end application insert emoji expressions, and that's it.

 

 

 

 

some little knowledge

Among them, it is easier to understand that character-set-server and collation-server are set to utf8mb4 character set, that is, to set MySQL database-related character set to utf8mb4;

However, in order to realize that after the client utf8 connects to MySQL, the utf8mb4 character set is also used. In the mysqld configuration, init_connect='SET NAMES utf8mb4' is configured to indicate that the initial connection is set to the utf8mb4 character set, and then a skip-character-set is configured. -client-handshake = true Ignore the client character set setting, no matter what character set the client is, it will be used according to the setting in init_connect, which meets the needs of the application.

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443606&siteId=291194637