版本一:
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
2,先去修改表字段字符集为utf8mb4:
ALTER TABLE UGC_REVIEW_CONTENT MODIFY `CONTENT` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '评论内容';
执行完后,再在手机端的app测试,依然报同样的错误。
3,再去修改表字符集utf8mb4:
ALTER TABLE UGC_REVIEW_CONTENT CHARSET=utf8mb4 COMMENT='晒单/推荐的评论内容';
执行完后,再在手机端的app测试,依然报同样的错误。
4,再去修改数据库的字符集utf8mb4:
vim my.cnf
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
重启mysql数据库
[root@mysqlvm4 ~]# service mysql restart
Shutting down MySQL.... [确定]
Starting MySQL.............................................[确定]..
[root@mysqlvm4 ~]#
查看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 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql>
在手机app端重新评论,输入表情符号,点击提交提示评论成功。
5,问题分析总结:
㈠ 原因
普通的字符串或者表情都是占位3个字节,所以utf8足够用了,但是移动端的表情符号占位是4个字节,普通的utf8就不够用了,为了应对无线互联网的机
遇和挑战、避免 emoji 表情符号带来的问题、涉及无线相关的 MySQL 数据库建议都提前采用 utf8mb4
字符集,这必须要作为移动互联网行业的一个技术选型的要点
㈡ 限制
需要 >= MySQL 5.5.3版本、从库也必须是5.5的了、低版本不支持这个字符集、复制报错。
转载:http://blog.itpub.net/26230597/viewspace-1243233/
版本二:
在mysql插入emoji表情,出现错误:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8A' for column 'content' at row
原因是当前mysql的字符集为utf-8,最多3个字节,emoji表情需要4个字节来表示。所以导致insert报错。
一般的方法是将字符集改为utf8mb4
更改字符集
1.my.cnf: 升级后只有/etc/my.cnf.rpmsave,将其重命名为my.cnf,在其中增加,然后service restart mysql:
- [mysqld]
- character-set-server=utf8mb4
- [mysql]
- default-character-set=utf8mb4
2.更改datebase,table,column的字符集(事实上我只用到了后面两个,datavase的没有更改也似乎没有影响):
- # For each database:
- ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
- # For each table:
- ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- # For each column:
- ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3.确认更改是否生效,分别看mysql和字段的字符集:
- mysql>show variables like '%char%'
- mysql>show full columns from tablename;
参考:http://bbs.chinaunix.net/thread-4178484-1-1.html