Emoji emoji entered MySQL database error solution

1,查看tomcat后台日志,核心报错信息如下:
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' for column 'CONTENT' 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.execute(PreparedStatement.java:1379)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
... 46 more

[root@mysqlvm2 ~]# mysql -root -p
mysql> use test;
Database changed
mysql> show tables;
Ignoring query to other database
mysql> Ctrl-C -- exit!
Aborted
忘记输入-u参数了,冲洗你输入,OK,如下所示:
[root@mysqlvm2 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.12-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 
mysql> use test;
Database changed
mysql> show tables;
+------------------------+
| Tables_in_test         |
+----------------------------+
| c |
| lubin_test |
| test |
| tt |
+----------- -------------+
10 rows in set (0.00 sec)
This kind of input Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' problem, most They are all character sets. I used to change from latain to gbk, from gbk to utf8, and my CONTENT field is already utf8, so more than utf8, only utf8mb4, so go and modify the character set of the table field.

mysql>


2, first modify the character set of the table field to utf8mb4:
ALTER TABLE UGC_REVIEW_CONTENT MODIFY `CONTENT` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'comment content';
after execution, and then test the app on the mobile phone, the same error is still reported.


3. Go to modify the table character set utf8mb4:
ALTER TABLE UGC_REVIEW_CONTENT CHARSET=utf8mb4 COMMENT='Posting/recommended comment content';
After execution, and then test the app on the mobile phone, the same error is still reported.


4. Then modify the character set utf8mb4 of the database:
vim my.cnf
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4

Restart the mysql database
[root@mysqlvm4 ~]# service mysql restart
Shutting down MySQL... . [OK]
Starting MySQL................................................................ .[OK]..
[root@mysqlvm4 ~]#View

the character set of db
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 |
|
----------------------+---------------------------- -------+
8 rows in set (0.00 sec)
mysql>

Re-comment on the mobile app, enter the emoji, and click Submit to indicate that the comment is successful.

5. Problem analysis and summary:
    (1) Reasons
        Ordinary strings or emoticons occupy 3 bytes, so utf8 is enough, but the emoji on the mobile terminal occupies 4 bytes, and ordinary utf8 is not enough. In order to cope with the opportunities and challenges of wireless Internet , to avoid the problems caused by
    emoji , and to use the utf8mb4 character set in advance for the MySQL database related to
        wireless The library must also be 5.5, the lower version does not support this character set, and the copy error is reported.

Reference article address: http://bbs.csdn.net/topics/390055415

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326605616&siteId=291194637