Home > Database > Mysql Tutorial > Emoji表情符号录入MySQL数据库报错的解决方案

Emoji表情符号录入MySQL数据库报错的解决方案

WBOY
Release: 2016-06-07 16:43:17
Original
1062 people have browsed it

前言:手机app应用评论的时候,恢复表情符号,提示失败。1,查看tomcat后台日志,核心报错信息如下: Caused by: java.sql.SQLEx



mysql>


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的了、低版本不支持这个字符集、复制报错。

本文永久更新链接地址:

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template