mysql存储emoji表情报错问题
emoji
表情使用mysql的默认字符设置UTF-8会报错:
Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xE6\x88\x91\xE4\xBB\xAC...' for column 'xx' at row 1
这种原因是字符集编码导致的。
1、验证字符集
首先进入mysql:
mysql -u root -p
进入之后输入以下:
show variables like '%character%';
这里是输入命令之后的视图:
mysql> show variables like '%character%';
+--------------------------+--------------------------------------------------------+
| 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 | utf8mb3 |
| character_sets_dir | /usr/local/mysql-8.0.29-macos12-x86_64/share/charsets/ |
+--------------------------+--------------------------------------------------------+
8 rows in set (0.01 sec)
2、进入my.cnf
vim /etc/my.cnf
编辑my.cnf
[client]
default-character-set=utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
[mysql]
default-character-set = utf8mb4
重启数据库服务即可正常存储表情符号,当前版本是: mysql Ver 8.0.29 for macos12 on x86_64 (MySQL Community Server - GPL)