时间:2021-05-23
MySQL 需要支持 emoji 表情符号版本需要大于5.5.3,且字符集需要设置为utf8mb4 字符集。
utf8mb4和utf8到底有什么区别呢?原来以往的mysql的utf8一个字符最多3字节,而utf8mb4则扩展到一个字符最多能有4字节,所以能支持更多的字符集。
将Mysql的编码从utf8转换成utf8mb4。
需要 >= MySQL 5.5.3版本、从库也必须是5.5的了、低版本不支持这个字符集、复制报错
停止MySQL Server服务
修改 my.cnf或者mysql.ini
[client] default-character-set = utf8mb4[mysql] default-character-set = utf8mb4[mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4'重启 MySQL Server、检查字符集。
查看服务器字符集设置
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 |+--------------------------+--------------------+查看数据库字符集
mysql> select * from SCHEMATA where SCHEMA_NAME='ttlsa';+--------------+-------------+----------------------------+------------------------+----------+| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |+--------------+-------------+----------------------------+------------------------+----------+| def | ttlsa | utf8mb4 | utf8mb4_unicode_ci | NULL |+--------------+-------------+----------------------------+------------------------+----------+查看表字符集
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.TABLES;+--------------------+----------------------------------------------------+--------------------+| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |+--------------------+----------------------------------------------------+--------------------+查看列字符集
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from COLUMNS;+--------------------+----------------------------------------------------+--------------------------------------------+--------------------+| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |+--------------------+----------------------------------------------------+--------------------------------------------+--------------------+转换字符集语句
use information_schema;SELECT concat("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schema;SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schema, table_name;SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('varchar');SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('text','tinytext','mediumtext','long声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了MySQL无法存储emoji表情解决方案。分享给大家供大家参考,具体如下:今天学习爬虫爬伯乐在线的文章,由于在文章中有emoji表情,导致有emo
本文实例分析了mysql存储emoji表情报错的处理方法。分享给大家供大家参考,具体如下:utf-8编码可能2个字节、3个字节、4个字节的字符,但是MySQL的
做了一些信息提交的操作,输入了Emoji表情时,后台数据库无法存储原因:UTF-8编码有可能是两个、三个、四个字节。Emoji表情是4个字节,而Mysql的ut
Mysql的utf8编码最多3个字节,而Emoji表情或者某些特殊字符是4个字节。因此会导致带有表情的昵称插入数据库时出错。只要修改MySQL的编码即可,解决方
本文实例讲述了MySQL无法存储Emoji表情问题的解决方法。分享给大家供大家参考,具体如下:数据插入的时候报错:1366-Incorrectstringval