本文主要是介绍mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题
mysql中保存4字节长度的UTF-8字符(例如emoji表情),就需要使用 utf8mb4 字符集。
如果要写入emoji表情(utf8mb4)到mysql,需要应用客户端、客户端到MySQL的连接、以及MySQL实例内部这三者统一,统一使用支持utf8mb4字符集才行。
否则,mysql的jdbc链接驱动mysql-connetoer会抛异常:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84],...' for column 'name' at row 1
mysql server 5.5.3+ 版本才支持utf8mb4。
mysql> select version();
mysql-connector-5.1.13+版本才支持utf8mb4。
mysq配置文件修改,mysql修改字符集:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
#collation-server = utf8mb4_unicode_ci #测试发现可能不用配置#init_connect=’SET NAMES utf8mb4’ #测试发现可能不用配置
sql_mode='NO_ENGINE_SUBSTITUTION'
重启mysql
mysql> show variables like '%charact%';
+--------------------------+----------------------------+| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
必须保证下面几个正确:
character_set_client (客户端来源数据使用的字符集)
character_set_connection (连接层字符集)
character_set_database (当前选中数据库的默认字符集)
character_set_results (查询结果字符集)
character_set_server (默认的内部操作字符集)
客户端链接测试:
#1: 插入后为乱码:3F3F3F3F
set charset utf8;
insert into test_utf8mb4_src values(9,"test", '??'); #??为直接拷贝的表情符号
select hex(url) from test_utf8mb4_src where id=9;
#2: 插入OK, F09F9884
set charset utf8;
insert into test_utf8mb4_src values(9,"test", 0xF09F9884);
select hex(url) from test_utf8mb4_src where id=9;
#3: 插入OK, F09F9884
set charset utf8;
insert into test_utf8mb4_src values(9,"test", UNHEX('F09F9884'));
select hex(url) from test_utf8mb4_src where id=9;
#4: 插入OK, F09F9884
set charset utf8mb4;
insert into test_utf8mb4_src values(9,"test", '??'); #??为直接拷贝的表情符号
select hex(url) from test_utf8mb4_src where id=9;
#5: 插入OK, F09F9884
set charset utf8mb4;
insert into test_utf8mb4_src values(9,"test", 0xF09F9884);
select hex(url) from test_utf8mb4_src where id=9;
#6: 插入OK, F09F9884
set charset utf8mb4;
insert into test_utf8mb4_src values(9,"test", UNHEX('F09F9884'));
select hex(url) from test_utf8mb4_src where id=9;
----------
总结一下:
不同的配置,有不同测试结果。
1.mysql配置如下,写成功&查询hex值正确
#vim /etc/my.conf
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4 #会修改@@character_set_server & @@character_set_database,该参数如果不配置成utf8mb4则无法存储
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 |
...
+--------------------------------------+----------------------------+
2.写入会失败
#vim /etc/my.conf
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
[mysqld]
character-set-server = utf8 #会修改@@character_set_server & @@character_set_database,该参数如果不配置成utf8mb4则无法存储
mysql> show variables like '%char%';
+--------------------------------------+----------------------------+
| Variable_name | Value |
+--------------------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
...
+--------------------------------------+----------------------------+
3.其他的配置,可能会导致写入虽然成功,但写入的值(查看hex)不正确。例如:
default-character-set = utf8mb4
character-set-server=utf8
这篇关于mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!