官方Mysql手册链接

https://dev.mysql.com/doc/connectors/en/connector-j-reference-charsets.html

Notes
For Connector/J 8.0.12 and earlier: In order to use the utf8mb4 character set for the connection, the server MUST be configured with character_set_server=utf8mb4; if that is not the case, when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8, which is an alias for utf8mb3. For Connector/J 8.0.13 and later: When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4. If the connection option connectionCollation is also set alongside characterEncoding and is incompatible with it, characterEncoding will be overridden with the encoding corresponding to connectionCollation. Because there is no Java-style character set name for utfmb3 that you can use with the connection option charaterEncoding, the only way to use utf8mb3 as your connection character set is to use a utf8mb3 collation (for example, utf8_general_ci) for the connection option connectionCollation, which forces a utf8mb3 character set to be used, as explained in the last bullet. Warning
Do not issue the query SET NAMES with Connector/J, as the driver will not detect that the character set has been changed by the query, and will continue to use the character set configured when the connection was first set up.

文档说的很清楚

提示

mysql-connector-java 版本在8.0.12之前的,包括8.0.12,服务端必须设置character_set_server=utf8mb4;如果不是的话,就算设置了characterEncoding=UTF-8,照样会被设置为MYSQL的 utf8字符集,也就是utf8mb3。

对于8.0.13和以后的版本,如果设置了characterEncoding=UTF-8,他会映射到MYSQL的utf8mb4字符集。

如果connectionCollation 也和characterEncoding一起设置了,但是不兼容,characterEncoding会被connectionCollation的设置覆盖掉。

由于没有Java-Style的utfmb3对应的字符集名称可以用在connection选项charaterEncoding上,唯一的设置utf8mb3的方式就是在连接选项设置utf8mb3 collation(例如utf8_general_ci),这会强制使用utf8mb3字符集,正如上文所述。

警告

不要通过Connector发起SET NAMES指令,因为driver不会检测字符集是不是被查询语句改动,并且当连接第一次建立之后,会继续使用当时的字符集设置。

结论

对于网上的设置:

<property name="connectionInitSqls" value="set names utf8mb4;"/>

纯属扯淡。。

jdbc:mysql://localhost:3306/dbnameuseUnicode=true&characterEncoding=utf8

也是扯淡,

characterEncoding 要设置 为UTF-8。

MySQL Character Set Name Java-Style Character Encoding Name
For 8.0.12 and earlier: utf8 UTF-8
For 8.0.13 and later: utf8mb4 UTF-8

Java-Style的字符集是UTF-8,而不是utf8

正确解决方法

改服务器配置吧,或者升级mysql-connector-java 到 8.0.13以后吧

测试情况

jdbc:mysql://localhost:3306/dbnameuseUnicode=true&characterEncoding=utf-8&connectionCollation=utf8mb4_general_ci

这样写不报错,但是无法正常存储。

另外,版本在5.1.13以后的支持自动检测服务器设置,或者指定characterEncoding=utf-8。

但是我自己测试的结果就是 5.1.38 不写connectionCollation的情况下,指定utf-8也报错。

https://dev.mysql.com/doc/relnotes/connector-j/5.1/en/news-5-1-13.html

Connector/J now auto-detects servers configured with character_set_server=utf8mb4 or treats the Java encoding utf-8 passed using characterEncoding=... as utf8mb4 in the SET NAMES= calls it makes when establishing the connection. (Bug #54175)

即便写了connectionCollation,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 | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8mb4_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_bin |
+--------------------------+----------------------------+
11 rows in set (0.04 sec) mysql> select hex(content) from send_message where id = 348;
+------------------------+
| hex(content) |
+------------------------+
| 3C703EF09F988A3C2F703E |
+------------------------+
1 row in set (0.04 sec) #F09F988A//这个是emoji的hex值,通过navicat插入的。就可以。 mysql> select hex(content) from send_message where id = 349;
+------------------------+
| hex(content) |
+------------------------+
| 3C703E3F3F3F3F3C2F703E |
+------------------------+
1 row in set (0.04 sec) #3F3F3F3F//这个是通过jdbc插入的,看样子是无法正确存储了。

Mysql UTF-8mb4字符集的问题的更多相关文章

  1. 为 MySQL 设置默认字符集(UTF-8)避免产生乱码

    环境:Windows 7+Wamp Server+MySQL 5.7.9 查看MySQL默认编码: SHOW VARIABLES LIKE 'character%' character_set_cli ...

  2. MySQL修改默认字符集

    今天朋友在做某个程序项目时,需要修改MySQL修改默认字符集,搞不好找我帮忙.百度了试了好几篇博文中的方法,最后终于成功了.但是感觉那些博文思路有点乱,所以自己总结下,希望可以帮到遇到同样问题的人. ...

  3. Mysql基础之字符集与乱码

    原文:Mysql基础之字符集与乱码 Mysql的字符集设置非常灵活 可以设置服务器默认字符集 数据库默认字符集 表默认字符集 列字符集 如果某一个级别没有指定字符集,则继承上一级. 以表声明utf8为 ...

  4. MySQL开发——【字符集、校对集】

    字符集 查看MySQL中的字符集 基本语法: show character set; 查看MySQL中的默认字符集 基本语法: show variables like ‘character_set%’ ...

  5. 修改mysql数据的字符集校验规则使其区分大小写

    mysql 使用utf8字符集默认的校验规则collate为utf8_general_ci,不区分数据的大小写 测试如下 ::) character set utf8 collate utf8_bin ...

  6. mysql查询表的字符集

    mysql查询表的字符集 SHOW CREATE TABLE user;

  7. mysql中更改字符集为utf8&&mysql中文输入不了问题解决

    写给TT:对不起啦!! 嗯,输入不了中文,大多数问题是mysql的字符集设置的问题,当然,别的问题也有可能, 这里我们用两种方法设置mysql的字符集,图形化工具和命令行的方式(一种操作完即可) 一, ...

  8. mysql 更改默认字符集

    mysql 默认字符集概述 首先,MySQL的字符集问题主要是两个概念: haracter Sets Collations 前者是字符内容及编码,后者是对前者进行比较操作的一些规则.这两个参数集可以在 ...

  9. MacOS中Mysql设置默认字符集

    一.查看字符集 mysql> show variables like 'character%'; +--------------------------+-------------------- ...

  10. (转)修改及查看mysql数据库的字符集

    原文:http://www.cnblogs.com/donqiang/articles/2057972.html Liunx下修改MySQL字符集:1.查找MySQL的cnf文件的位置find / - ...

随机推荐

  1. DW网页代码笔记

    DW网页代码笔记 1.样式.       class  插入类样式  标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术       解决页面动态交互问题<form> ...

  2. 品Spring:负责bean定义注册的两个“排头兵”

    别看Spring现在玩的这么花,其实它的“筹码”就两个,“容器”和“bean定义”. 只有先把bean定义注册到容器里,后续的一切可能才有可能成为可能. 所以在进阶的路上如果要想走的顺畅些,彻底搞清楚 ...

  3. 如何免费使用GPU跑深度学习代码

    从事深度学习的研究者都知道,深度学习代码需要设计海量的数据,需要很大很大很大(重要的事情说三遍)的计算量,以至于CPU算不过来,需要通过GPU帮忙,但这必不意味着CPU的性能没GPU强,CPU是那种综 ...

  4. 数据库系统概论——SQL

    [toc] 一.SQL查询语言概览 视图 从一个或几个基本表导出的表 数据库中只存放视图的定义而不存放视图对应的数据 视图是一个虚表 用户可以在视图上再定义视图 基本表 本身独立存在的表 SQL中一个 ...

  5. MongoDB安装过程中出现service MongoDB failed to start,verify that you have sufficient privileges to start...

    win10系统下,安装MongoDB 64位, service MongoDB failed to start,verify that you have sufficient privileges t ...

  6. 记一次linux Docker网络故障排除经历

    背景: 之前做了一个项目,需要在容器内访问宿主机提供的Redis 服务(这是一个比较常见的应用场景哈), 常规方案: ①   主机网络(docker run --network=host): 完全应用 ...

  7. kotlin系列文章 --- 3.条件控制

    if表达式 一个if语句包含一个布尔表达式和一条或多条语句 // 基础用法 var max = a if (a<b) max = b // 加上else var max: Int if(a> ...

  8. Java 学习笔记之 线程interrupt方法

    线程interrupt方法: interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环. 调用interrupt()其实仅仅是在当前线程中打了一个停止标记, ...

  9. C# ManualResetEvent用法

    ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件) 其构造函数为: public ManualResetEvent (bool in ...

  10. bullet物理引擎与OpenGL结合 导入3D模型进行碰撞检测 以及画三角网格的坑

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11681069.html 一.初始化世界以及模型 /// 冲突配置包含内存的默认设置,冲突设置. ...