mysql数据库怎么存入emoji表情,更改utf8mb4后为什么出现全是问号
经测试完美解决表情的存储问题
在项目中遇到有人存emoji表情,出现如下错误:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1
解决方法:
第一步:
修改mysql配置文件my.cnf(windows为my.ini,linux为my.cnf)
my.cnf一般在etc/mysql/my.cnf位置。找到后请在以下三部分里添加如下内容:
[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数据库,重新查看字符集
在mysql命令行中输入:SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' 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 |
+--------------------------+--------------------+
---------------------
进测试操作后,虽然最后三项没变动,但是已解决表情存储的问题了
作者:Hank涵
来源:CSDN
原文:https://blog.csdn.net/a445849497/article/details/72973352
版权声明:本文为博主原创文章,转载请附上博文链接!